Applying Mask (elliptical shape) on contourf plots.
17 views (last 30 days)
Show older comments
Hi,
I am ploting the contourf plots and wanted to apply the mask with elliptical paramters for pointing out area of interest (in contourf plots i am ploting a matrix of dimension 11x181).
Could you please advise me on how to generate a mask to highlight the area of interest and making rest 0?
Your help and time will be highly appreciated.
2 Comments
Jiri Hajek
on 4 Jan 2023
Hi, did you notice this example in the documentation? Seems like it could answer your needs... (https://www.mathworks.com/help/matlab/ref/contourf.html#mw_df7cc9a2-a98d-405c-83e3-85d9a195b5a5)
Accepted Answer
DGM
on 4 Jan 2023
Edited: DGM
on 4 Jan 2023
One way would be to use ROI tools if you have those.
% some arbitrary surface data
[x y z] = peaks(100);
% show the contour plot
contourf(x,y,z,20,'edgecolor','none'); hold on
colormap(jet)
colorbar
% overlay a black image that has as much resolution as your mask
blackimg = zeros(1000,1000,3); % RGB
hi = imagesc(blackimg,'xdata',xlim,'ydata',ylim); % show it
hi.AlphaData = 0; % make it invisible
% interactively create an elliptical mask using ROI tools
ROI = drawellipse(gca);
mask = ROI.createMask;
delete(ROI) % get rid of them once the mask is generated
% alter the alphadata of the overlaid black image
hi.AlphaData = ~mask;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1251787/image.png)
Note that here, the masked area is black, since that's the overlaid image. Alternatively, the mask can be made semitransparent.
hi.AlphaData = 0.5*double(~mask);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1251792/image.png)
3 Comments
DGM
on 4 Jan 2023
Edited: DGM
on 4 Jan 2023
Those are part of the Image Processing Toolbox.
It's not strictly necessary though; it's just a convenience in that it's interactive. The only thing that it's used for is creating a logical mask that describes the region. A mask could be created in other ways.
% some arbitrary surface data
[x y z] = peaks(100);
% show the contour plot
contourf(x,y,z,20,'edgecolor','none'); hold on
colormap(jet)
colorbar
% overlay a black image that has as much resolution as your mask
blackimg = zeros(1000,1000,3); % RGB
hi = imagesc(blackimg,'xdata',xlim,'ydata',ylim); % show it
% programmatically create a mask somehow
% this is just a plain circle
mask = drawcircle([1000 1000],[500 500],400);
% alter the alphadata of the overlaid black image
% in this case, the mask is antialiased (not logical)
% so the handling is slightly different
hi.AlphaData = 1-mask;
% draw smooth circle
% sz: size of mask (px) [y x]
% c: center of circle (px) [y x]
% r: radius of circle (px)
function circ = drawcircle(sz,c,r)
xx = 1:sz(2);
yy = (1:sz(1)).';
circ = sqrt((xx-c(2)).^2 + (yy-c(1)).^2);
circ = min(max(-(circ-r)/2,0),1);
end
That's just one example. Drawing an appropriately located general ellipse is going to be more cumbersome than that though. You'll have to deal with the translation between the image coordinates and the coordinates in your underlying contour plot. By setting the xdata and ydata properties of the imagesc object to xlim and ylim, the image extents cover the contourf extents, regardless of how many points are in either. Since the contour data is only 100x100, I chose to make my image have more resolution so that it appeared smooth.
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!