Applying free hand crop to TWO images simultaneously
2 views (last 30 days)
Show older comments
Hello, I have two nearly identical images and I'd like to use imfreehand on ONE of the images, but save the boundaries of the drawn freehand on BOTH images to crop and save this section on BOTH images.
Here's a screen cap of what I have so far
Can you please help with this code? Code adapted from Image Analyst.
% Read image file
grayImage = imread(rotlegsegmentFilename);
grayImageFluor = imread(rotlegsegmentFilenameFluor);
subplot(1,2,1)
imshow(grayImageFluor, []);
subplot(1,2,2)
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();
%
% Create a binary image ("mask")
binaryImage = hFH.createMask();
xy = hFH.getPosition;
%
% Get coordinates of the boundary of the freehand drawn region.
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
drawnow; % Force it to draw immediately.
%
% Will keep only the part of the image that's inside the mask, zero outside mask.
blackMaskedImage = grayImage;
blackMaskedImage(~binaryImage) = 0;
%
% Crop the image.
leftColumn = min(x);
rightColumn = max(x);
topLine = min(y);
bottomLine = max(y);
width = rightColumn - leftColumn + 1;
height = bottomLine - topLine + 1;
cropImage = imcrop(blackMaskedImage, [leftColumn, topLine, width, height]);
Many thanks!
0 Comments
Accepted Answer
Image Analyst
on 11 Jan 2014
You forgot to attach your images. So what's the problem? Just call imcrop() on each image.
More Answers (0)
See Also
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!