ROI and images

6 views (last 30 days)
Jason
Jason on 3 Aug 2011
Hello. I need to create a ROI that takes a portion of an image on a GUI and draws only the portion within the ROI on another axes on the GUI.
h = imrect;
pos = h.getPosition();
%addNewPositionCallback(h,@(p)assignin('base','pos',p));
addNewPositionCallback(h,@(p)set(handles.edit1, 'string',uint16(pos(1))));
The line rem'd out works, but instead of assigning it to the workspace I wanted to assign a value to an edit box. But it doesn't work.
Also can I create a callback that actually redraws the ROI (knowing the coordinates of the ROI) image as the mouse moves the ROI box on the 1st full image?

Answers (1)

Image Analyst
Image Analyst on 3 Aug 2011
Does it really have to be another GUI? If you're willing to have it all on the same GUI, just different axes on that GUI, then you can adapt this demo I already have. Otherwise you'll have to have GUI1 call GUI2 and send over some variables to GUI2.
clc; % Clear command window.
% clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 3, 1);
imshow(monochromeImage);
title('Original Image', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay', 'FontSize', fontSize);
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 3, 2);
hEllipse = imellipse(gca,[70 15 90 140]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
maskImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 3, 3);
imshow(maskImage);
title('Binary mask of the ellipse', 'FontSize', fontSize);
% Mask the image with the ellipse.
maskedImage = monochromeImage .* cast(maskImage, class(monochromeImage));
% Display the image with the "burned in" ellipse.
subplot(2, 3, 4);
imshow(maskedImage);
title('New image masked by the ellipse', 'FontSize', fontSize);
% Find the bounding box
column1 = find(sum(maskImage, 1), 1, 'first')
column2 = find(sum(maskImage, 1), 1, 'last')
row1 = find(sum(maskImage, 2), 1, 'first')
row2 = find(sum(maskImage, 2), 1, 'last')
croppedImage = maskedImage(row1:row2, column1:column2);
subplot(2, 3, 5);
imshow(croppedImage);
title('Ellipse portion cropped out', 'FontSize', fontSize);
  1 Comment
Jason
Jason on 3 Aug 2011
Hi, absolutely yes I only need the two images (raw and ROI one0 on the same GUI but on different axes. I don't see from your code how manually moving the ROI box with the mouse automatically updates the ROI image on the 2nd axes?
Jason

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!