How to make the ROI show on top of the image after drawing ROI with roipoly?

37 views (last 30 days)
Hi, Guys,
I draw an ROI with roipoly(). After drawing, the blue ROI will disappear. How can I display this ROI on top of the image after drawing it? Please see the following example.
im = imread('cameraman.tif');
figure(1)
imagesc(im);
axis image off; colormap gray;
title('Please Draw ROI');
% This is the image
% Draw ROI
roi=roipoly;
% During drawing ROI
% After drawing ROI (by double clicking the first point), the blue ROI disappears
My question is : How to make the ROI still display on top of the image after drawing? Many thanks!

Accepted Answer

Image Analyst
Image Analyst on 2 Sep 2014
Try this:
im = imread('cameraman.tif');
imagesc(im);
axis image off;
colormap gray;
title('Please Draw ROI');
% Draw ROI
[binaryMask, x, y] = roipoly;
hold on;
plot(x, y, 'r.-', 'MarkerSize', 15);
  7 Comments
Louis-Philippe Guinard
Louis-Philippe Guinard on 1 Apr 2021
As far as delimiting and displaying the ROIs go, your code works wonders. However, for what I need, it might not work as well.
Going back to the line where I define ROIs, I wrote:
ROI(:,:,1,NbROI) = roipoly;
This was meant to have a variety of ROIs that I could calculate stuff upon, and later come back to display the images with said ROIs outlined. Your code seems to overwrite them at every next ROI I define. That said, I could theoretically fit the rest of my code in-between, but this would make for a very awkward way of making my code work. Ideally I'd like this to be a function that serves the only purpose of defining the ROIs, applying the ROI mask on my image and save the outlines for later display.
That said, here is my full function as was written before, if it might help you understand where I'm going:
function [imaROI,imaROItot] = GenerateROImasks(NbROI,cond,imaDisplay,imaCorrected)
% Function from part 5. Generates the ROI masks to be analyzed separately.
figure(1)
imshow(imaDisplay,[])
while cond == true
NbROI = NbROI+1;
ROI(:,:,1,NbROI) = roipoly;
choice = menu('Do you want to define and analyze more ROIs?','Yes','No');
if choice == 2 || choice == 0
break
end
end
imaROItot = zeros(size(ROI));
for ROIiter = 1:size(ROI,4)
imaROI(:,:,:,ROIiter) = ROI(:,:,1,ROIiter).*imaCorrected;
imaROItot = sum(imaROI,3);
end
end
...from there onwards I do calculations using the output of my function, in which each ROI is to be analyzed separately. At the end, I display various graphs for each ROI, but I'd want to be included in these subplots the starting image with the ROI corresponding to these graphs. I would then add the xPerimeter and yPerimeter to my outputs.
Sorry if my initial question wasn't precise enough! Also, thanks for your patience :)
Image Analyst
Image Analyst on 2 Apr 2021
Not sure exactly what you're asking. The ROI coordinates are saved at each iteration. They're not overwritten. I don't store all the binary images individually, but if you want to store those too, you can simply do
masks{numberOfROIsDrawn} = thisBinaryImage;
Feel free to modify my code to get it to do exactly what you want.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 1 Apr 2021
Try this:
% Read in gray scale image.
grayImage = imread('cameraman.tif');
% Display it.
imshow(grayImage);
axis('image', 'on');
colormap gray;
g = gcf;
g.WindowState = 'maximized';
drawnow;
% Give user instructions for how to draw the ROI:
caption = sprintf('When you see the crosshairs, left click the vertices. Double-click the last one.\nThen adjust points if needed. Double-click inside when all done');
title(caption);
message = sprintf('When you see the crosshairs, left click the vertices.\nDouble-click the last one.\nThen adjust points if needed.\nDouble-click inside when all done');
uiwait(helpdlg(message));
% Draw ROI. Be sure to accept the 2nd and 3rd output arguments, which are the coordinates you clicked on.
[binaryImage, xPerimeter, yPerimeter] = roipoly;
% The ROI disappeared, so draw perimeter onto original image using xPerimeter and yPerimeter.
hold on
plot(xPerimeter, yPerimeter, 'b.-', 'LineWidth', 2, 'MarkerSize', 15);
fprintf('Done running %s.m\n', mfilename);

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!