imrect not visible after updating an image

Refer to the script below:
f = figure;
im = image(rand(200, 100, 3));
roi = imrect(gca, [5, 10, 40, 30]);
title('ROI visible');
pause;
hold('on');
image(rand(200, 100, 3));
title('ROI not visible, how to make it visible again?');
At this point, roi still exists but is no longer visible. You can still run get(roi), for instance. How to make it visible again?
Thanks!

 Accepted Answer

uistack(roi, 'top')

3 Comments

This caused an error message in my environment (MATLAB Version: 9.2.0.556344 (R2017a)):
Conversion to double from imrect is not possible.
Error in uistack>getNewOrder (line 149)
SameType = double(SameType);
Error in uistack (line 114)
NewOrder = getNewOrder(SameType, Children, StackOpt, Step);
Thanks.
Hmmm.
old_warning_state = warning('MATLAB:structOnObject', 'off');
roi_struct = struct(roi);
warning(old_warning_state);
uistack(roi_struct.h_group, 'top');
This worked, with a minor change: old_warning_state = warning('off', 'MATLAB:structOnObject');
Thanks!

Sign in to comment.

More Answers (1)

You can get the coordinates and then use plot() to plot the box whenever you want. See this demo:
im = image(rand(200, 100, 3));
axis on;
hBox = imrect;
roiPosition = wait(hBox);
roiPosition % Echo coordinates to the command window.
xCoords = [roiPosition(1), roiPosition(1)+roiPosition(3), roiPosition(1)+roiPosition(3), roiPosition(1), roiPosition(1)];
yCoords = [roiPosition(2), roiPosition(2), roiPosition(2)+roiPosition(4), roiPosition(2)+roiPosition(4), roiPosition(2)];
% Plot the mask as an outline over the image.
hold on;
plot(xCoords, yCoords, 'linewidth', 2);
title('ROI visible');
% Update image
hold off;
grayImage = imread('moon.tif'); % Note, this is a different size.
imshow(grayImage);
hold on;
axis on;
plot(xCoords, yCoords, 'linewidth', 2); % Plots with original units and size.
title('ROI visible on updated image');

5 Comments

This does make the ROI visible again, but it's no longer functional (e.g. you cannot drag or resize it). Thanks.
No. If you want to do that again, call imrect again, or else don't double click out of it to finish it. Why are you trying to update your image in between when you ask the user to draw a box and before the user gets done specifying the box?
The box is only used to select a region. There's a series of underlying images (like a movie), and I'd like to analyze a small region of the movie selected using imrect. However, I also would like to be able to see different frames of the movie, while still maintaining the capability of moving and resizing the region of interest. Sorry I did not make this clear initially.
I didn't know that imrect would constantly send out position information. I thought you had to double click inside it to learn the position. How did you get it to send out instantaneous position information as you both change images and change box size and location?
imrect has a getPosition method.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!