Using roipoly on plots

Is the roipoly command only usable on image files? I need to select a region of interest (possibly several) on a contourf plot of data. After trying to use the roipoly command on one of my plots it generates an error saying an image must be present in the current figure. If this is the case and there is no way to use roipoly on plots, is there an alternate command for selecting region of interest? I had previously used data cursor but roipoly then seemed like a preferable option.

8 Comments

roipoly only works on image matrix data - i.e. numeric types. If there is no underlying matrix then it cannot work. A contour plot doesn't have the same type of structure that can be indexed into as you would an image.
You can use
doc imroi
to draw on an axes, though to be useful you still need to be able to interpret it in terms of the thing you drew it on so it is also usually used with images where you can turn it into a logical mask.
I have a contourf plot of a matrix. I need to select a region of interest on this plot, create a logical mask and then apply this mask to the other matrices in a cell array so that I can calculate the sum inside the roi for each matrix. I was looking at maybe using datacursor to select the region of interest and then create a mask but this is fairly unreliable in terms of repeatability
imroi
should work fine then I would think (or one of its sister functions like imrect if you wanted something more constrained).
Aaron Smith
Aaron Smith on 7 Jul 2017
Edited: Aaron Smith on 7 Jul 2017
On the imroi mathworks page, there is no command information. There are no examples or really any descriptions of how to use the command. Is this just because imroi is a kind of a collection of other commands like imrect? I'm just not 100% sure how to use it.
Is there any way to adapt imrect and imroi or roipoly to file types other than images? If not I will likely need to use datacursor and then try to create a mask using the coordinate data selected with data cursor.
Actually imroi is the abstract base class. impoly is the one to actually create an instance of, but you can do it on a blank axes if you wish, it doesn't need an image.
I'm not sure what you mean by 'file types' though. Files should not have anything to do with the process as you are working on an axes.
figure; hAxes = gca
hR = impoly( hAxes )
will allow you to simply draw a polygon on an axes (or, as far as I am aware, on top of anything that happens to be on those axes).
I used imrect to draw a rectangle (h) on the contourf plot of my matrix. I then used createMask(h) to create a mask and got the error:
Error using imroi/parseInputsForCreateMask (line 91)
createMask expects a current figure containing an image.
Error in imroi/createMask (line 258)
[obj,h_im] = parseInputsForCreateMask(varargin{:});
You can by-pass createMask, which is basically just a wrapper function and go straight to the function that it calls,
doc poly2mask
by using the points defined my imrect or impoly. This function needs to be given an image size (to create the mask of that size), but does not need an image itself e.g.
hPoly = impoly( hAxes );
...
polyPos = getPosition( hPoly );
x = polyPos(:,1); y = polyPos(:,2);
imSize = [100 200];
binaryMask = poly2mask( x, y, imSize(1), imSize(2) );

Sign in to comment.

Answers (0)

Products

Asked:

on 7 Jul 2017

Commented:

on 11 Jul 2017

Community Treasure Hunt

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

Start Hunting!