Applying Mask (elliptical shape) on contourf plots.

19 vues (au cours des 30 derniers jours)
Bilal Khalid
Bilal Khalid le 4 Jan 2023
Commenté : Bilal Khalid le 4 Jan 2023
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 commentaires
Jiri Hajek
Jiri Hajek le 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)
Bilal Khalid
Bilal Khalid le 4 Jan 2023
Hi,
Thanks for your reply, yes i did noticed and it doesn't help in what i wanted to do. I think I should make it more clear.
Let assume the attached image is contour filled 2D plot, all i wanted to do is to plot only the predefined elliptical mask and make the rest 0.
In other words masking the eliptical region and and visualising the eliptical region only.
Many Thanks.

Connectez-vous pour commenter.

Réponse acceptée

DGM
DGM le 4 Jan 2023
Modifié(e) : DGM le 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;
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);
  3 commentaires
DGM
DGM le 4 Jan 2023
Modifié(e) : DGM le 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.
Bilal Khalid
Bilal Khalid le 4 Jan 2023
Thanks @DGM this really helps alot really appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by