How to create ROI object handle?
6 次查看(过去 30 天)
显示 更早的评论
h = figure;
imshow(imread('pout.tif'));
imrect();
hg = findobj(h,'Type','hggroup')
% How can I "create" a ROI object so that I can call createMask()?
I'm trying to get ROI object handle, but it seems that it can not be retrieved after creation. Through property inspector, I found hggroup might be related to it. However, it is not the same as the handle returned by imrect().
Can I use hg to create a handle so that I can call createMask()?
Edit:
The main problem is that I have to get the ROI object after its creation. In my application, the ROI creation is done in a callback, thus I can not get an output from it. So I need to find out information about ROI object from figure handle.
0 个评论
回答(3 个)
Eric Delgado
2022-9-30
Replace imrect for images.roi.Rectangle. See the first example of the doc - https://www.mathworks.com/help/images/ref/images.roi.rectangle.html
figure;
imshow(imread('pout.tif'));
% Directly draw your rectangle using your mouse:
roi = drawrectangle;
% Draw your rectangle programmatically:
% roi = images.roi.Rectangle(gca,'Position',[x,y,W,H]);
4 个评论
Eric Delgado
2022-10-3
The handle for a uirect() has just one public property ("Deletable"). If you use struct to see its implementation, you will get a few more...
fig = figure;
ax1 = axes(fig);
imshow(imread('pout.tif'));
h = imrect()
% h =
% imrect with properties:
% Deletable: 1
h = struct(h)
% h =
% struct with fields:
% api: [1×1 struct]
% h_group: [1×1 Group]
% draw_api: [1×1 struct]
% graphicsDeletedListener: [1×1 event.listener]
% Deletable: 1
% hDeleteContextItem: [1×1 Menu]
isequal(findobj('Type', 'hggroup'), h.h_group)
% ans =
% logical
% 1
Image Analyst
2022-10-3
Try this:
h = figure;
imshow(imread('pout.tif'));
uiwait(helpdlg('Drag out a box'))
rRect = imrect();
pos = rRect.getPosition
mask = rRect.createMask;
figure;
imshow(mask)
7 个评论
Image Analyst
2022-10-4
Well then you'll need to save them. What Simon showed you only works as long as the graphical roi object is still there in the axes. If you ever cleared the axes, like by displaying a different image, then it will be gone and there's no way to get it back. So if you ever plan on displaying an image, drawing an roi, and then displaying another image, you'll have to have the roi saved into a variable, rather than an object in the axes container. I don't recommend Simon's solution in the more general case of using one or more ROIs on one or more images, which is what it sounds like what you want to do.
Simon Chan
2022-10-3
You may try the following:
h = figure;
imshow(imread('pout.tif'));
imrect(); % not use the output of imrect()
ax=gca;
hAx=findobj(ax.Children,'-regexp','Tag','corner marker');
x = cat(1,hAx.XData);
y = cat(1,hAx.YData);
roi = [min(x) min(y), max(x)-min(x) max(y)-min(y)]
3 个评论
Simon Chan
2022-10-3
You may need to tackle them differently:
h = figure;
imshow(imread('pout.tif'));
drawpolygon
ax = gca;
hAx = findobj(ax.Children,'-property','Position');
hAx.Position % Gives you the vertices for drawpolygon
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!