How can I replace the centre of imagesc with zero?

16 views (last 30 days)
I have an imagesc 128*2000 and I want to replace the centre (60-76 in the x-direction and 900-1150 in the y-direction) with zero.
Left image is what I currently have and right image is what needs to be done.
  3 Comments
parslee
parslee on 16 Feb 2022
I need to detect the centre based on the location of the maximum value and replace those values with 0.
DGM
DGM on 16 Feb 2022
1: What steps are you using to plot this data? I note it's complex, and the array is smaller and more square than the highly asymmetric axis scales shown above. Depending on the approach, knowing your existing technique may be important if the circle is to appear as a circle instead of an ellipse.
2: Do you want the circle to merely be part of the display, or do you actually want to blank part of the array?

Sign in to comment.

Answers (5)

Walter Roberson
Walter Roberson on 16 Feb 2022
Way 1:
Create a double precision matrix the same size as the image. Set it to 0 for all those center locations, and set the other locations to be 1. Now, when you imagesc() pass in 'AlphaData', and that matrix. The image will be transparent where the zeros are, showing through whatever is underneath -- which you would have arranged to be black (or whatever is appropriate.) This is like cutting out the center of the color matrix to see black you have put underneath.
Way 2:
Similar to above, but create an all-zero RGB matrix that is the same size as your image, and a second all-zero matrix the is the same size as your matrix except that it has 1's in the locations that are to be block. Use imagesc() to display the all-zero matrix exactly on top of the original image, with 'AlphaData' being the matrix that has 1s where the black is to be. The all-zero RGB matrix will be black, but the zeros section of the AlphaData matrix will make it transparent except where the center 1's are, which will show the black. So this is like a black mask on top of the color image.
Way 3:
Use viscircles() to draw a filled black circle on top of the imagesc()
These different methods have different consequences for data cursor readings.

DGM
DGM on 16 Feb 2022
Edited: DGM on 16 Feb 2022
This is one example using a marker. You'll likely have to adjust the marker size to suit your needs. If you want the marker size to be tied to exact pixel widths with respect to the data, one of Walter's suggestions may be more appropriate.
A = peaks(100); % example data
imagesc(A); hold on;
scatter(size(A,2)/2,size(A,1)/2,1000,'k','filled')
  1 Comment
Walter Roberson
Walter Roberson on 16 Feb 2022
Caution: when you make a marker sufficiently large, you often start to see distorations. Circle markers are not internally defined to be "as circular as the graphics hardware permits": circle markers are internally polygons, and when you make them large enough the straight edges start to show.

Sign in to comment.


Image Analyst
Image Analyst on 17 Feb 2022
" imagesc 128*2000 and I want to replace the centre (60-76 in the x-direction and 900-1150 in the y-direction"
So did you try
yourImage(900:1500, 60:76) = 0;
If those aren't pixel values then you'll have to use the known spatial calibration to convert those to pixels so that you're erasing actual rows and columns.

DGM
DGM on 17 Feb 2022
A more complex example:
% say this is the data size
s = [200 200]; % [y x]
% and these are the coordinate vectors
x = linspace(0,250,s(2));
y = linspace(0,2000,s(1));
% create fake data
ploc = round(s/2 + 10*randn(1,2))
ploc = 1×2
98 104
z = zeros(s);
z(ploc(1),ploc(2)) = 1E3;
fk1 = fspecial('gaussian',401,10);
fk2 = fspecial('gaussian',401,50);
fk = 0.3*fk1 + 0.7*fk2;
z = imfilter(z,fk);
z = z + 0.08*randn(s);
imagesc(x,y,z)
cmap = parula;
axis image
set(gca,'DataAspectRatio',[range(x)*s(1)/s(2) range(y) 1])
colormap(cmap)
% smooth the data
zs = imfilter(z,fspecial('gaussian',21,5));
imagesc(zs)
axis image
% find the peak
mx = max(zs,[],'all');
[mxr mxc] = find(zs==mx,1,'first');
[mxr mxc] % matches ploc
ans = 1×2
98 104
% create mask
radius = 15; % in pixels
mask = (((1:s(1))-mxr).'.^2 + ((1:s(2))-mxc).^2) <= radius^2;
% set masked pixels to zero
zmasked = z;
zmasked(mask) = 0;
figure
imagesc(x,y,zmasked)
axis image
set(gca,'DataAspectRatio',[range(x)*s(1)/s(2) range(y) 1])
% set masked pixels to actual black
zcolor = ind2rgb(round(mat2gray(z)*(size(cmap,1)-1)+1),cmap);
zcolor(repmat(mask,[1 1 3])) = 0;
figure
image(x,y,zcolor)
axis image
set(gca,'DataAspectRatio',[range(x)*s(1)/s(2) range(y) 1])
Note that setting the data to zero doesn't set the plot to black unless the colormap starts at black. If you actually want black, you can either do that by creating an image (as above) or by doing it using layered graphics objects in the figure itself.

Image Analyst
Image Analyst on 17 Feb 2022
Using plot(), scatter(), or viscircles() will put a spot in the overlay over the image, and not change the actual pixel values in your variable. Is that what you want?
Otherwise you can actually assign certain indexed (rows and columns) to zero, which will change pixel values.
If you want to just erase pixels in your image brighter than some value are set to zero, you can use masking:
mask = yourMatrix > someValue;
yourMatrix(mask) = 0; % Or whatever other value you want, like min(yourArray(:));

Categories

Find more on Visual Exploration in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!