clc;
close all;
imtool close all;
workspace;
fontSize = 14;
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
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, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Image Analysis Demo','numbertitle','off')
subplot(2, 3, 2);
hEllipse = imellipse(gca,[70 15 90 140]);
maskImage = hEllipse.createMask();
subplot(2, 3, 3);
imshow(maskImage);
title('Binary mask of the ellipse', 'FontSize', fontSize);
maskedImage = monochromeImage .* cast(maskImage, class(monochromeImage));
subplot(2, 3, 4);
imshow(maskedImage);
title('New image masked by the ellipse', 'FontSize', fontSize);
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);
Best Answer