Problem with imrect and position vector returned.

1 次查看(过去 30 天)
Something very strange is happening. Im using imrect to get a user defined ROI from an image (taken from an axes on a GUIDE GUI).
axes(handles.axes4);
Orig=getimage(handles.axes4);
val=get(handles.checkbox13,'Value');
if val==1
position=getappdata(0,'pos');
rectangle('Position',position,'EdgeColor','y')
drawnow;
else
warndlg('Select a box around single object. Try to centre around the Object','Template Matching','modal')
h=imrect
position=wait(h);
setappdata(0,'pos',position);
position
delete(h)
end
Orig=double(imcrop(Orig,position));
For testing, I have the checkbox13 unchecked so the code executes the "else" part.
The warning dialog appears, the blue imrect feature appears and it does indeed create the cropped image.
However, if the checkbox13 is checked, their is an error that "position" is wrong.
To check this, I removed the semi colon for h=imrect & position in the else part of the loop and unchecked the checkbox so the code executed this part. This is what the output is in the command window
h =
0×0 imrect array with properties:
Deletable
position =
[]
This suggests that "position" hasn't been obtained yet the imcrop works correctly. not sure whats going on.
Thanks for any help. Jason
  1 个评论
Jason
Jason 2017-3-10
编辑:Jason 2017-3-10
The issue goes away when I remove the warndlg code:
% warningString=sprintf('Select a box around single object \n Try to centre around the Object')
% warndlg(warningString,'Template Matching','modal')
h = imrect;
position =
262.5620 280.0704 57.6085 51.9606
Why would this be?

请先登录,再进行评论。

采纳的回答

Adam
Adam 2017-3-10
I'm not really sure which 'wait' function is being called there. I don't see one in the documentation that takes an imrect as argument.
You shouldn't need the wait though. imrect blocks program execution anyway until the user draws the rectangle so
h=imrect;
position = getPosition( h )
should work fine.
  3 个评论
Adam
Adam 2017-3-10
Ah yes, I missed looking in the obvious place, at the methods of imrect itself. It seems to work fine for me without wait, but obviously that method works fine too.
warndlg with a modal window will block command line input, but will not block program execution though so I imagine that its blocking tangles with that of the wait on imrect in some way.
e.g. if you try the following on command line:
warndlg('This is a warning','Template Matching','modal'); disp( 'finished' )
you will notice that 'finished' appears straight away after the warning dialog is created and does not wait for you to close the dialog, even though you cannot interact with the command line until you have closed it.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-3-10
Jason, if you're executing this code:
if val==1
position=getappdata(0,'pos');
then you must have previously loaded pos in to appdata with setappdata(). I'm betting that you have not done this yet. So you first have to do the "else" part before you can do the "if" part or else "pos" will not have been saved and you won't be able to use getappdata() to get it.
  1 个评论
Jason
Jason 2017-3-10
Thanks I A.
I have it so by default the checkbox is unchecked and so saves to positions to appdata on the first iteration.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by