Using the handle of a rectangle to first delete and then re-add to an image

3 views (last 30 days)
Hello. I superimpose a rectangle on a loaded image displayed on an axes component. I need the rectangle central, and size determined by a variable 'delta' as below.
img=imread('c:\Image03.tif');
axes(handles.axes1)
imageHandle=imshow(img,[]);
[rows,cols]=size(img)
rm=round(rows/2)
cm=round(cols/2)
delta=200
x=cm-delta
y=rm-delta
hold on;
h1=rectangle('Position',[x,y,delta,delta], 'FaceColor','none', 'EdgeColor','r')
hold off;
I then save this rectangle to the handles structure, so I can use it later.
handles.ROIrect=h1
guidata(hObject, handles);
I then use another button's callback to delete the rectangle fromt he image through:
axes(handles.axes1)
delete( findobj(gca, 'type', 'rectangle') );
I then want to have another pushbutton with its callback to redraw the rectangle on the image. I thought the following would work but it doesn't.
axes(handles.axes1)
h1=handles.ROIrect
Have i made an obvious mistake>?
Thanks
  1 Comment
Jason
Jason on 24 Nov 2015
Edited: Jason on 24 Nov 2015
adding... that the code to redraw the rectangle that has been saved to the handles structure doesn't work.
axes(handles.axes1)
h1=handles.ROIrect

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Nov 2015
This cannot be done. delete() is defined as meaning to track down all the actual object information and remove the object from the graphics system, destroying everything about it. delete() is like shredding a piece of paper rather than putting it in the recycle bin.
If you want a graphics object to stop being visible, then set its Visible property to off, or move it to a different parent that is not visible.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!