clc;
fprintf('Beginning to run %s.m ...\n', mfilename);
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
folder = pwd;
baseFileName = 'ZoomIn_100f.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = grayImage(:, :, 1);
end
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
title('Red Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
lowThreshold = 91;
highThreshold = 255;
mask = grayImage > lowThreshold & grayImage < highThreshold;
mask = imclearborder(mask);
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
caption = sprintf('Mask of White SemiCircles');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
props = regionprops(mask, 'Centroid', 'Orientation')
xCenter = props.Centroid(1);
yCenter = props.Centroid(2);
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 40, 'LineWidth', 2);
subplot(2, 2, 4);
overlayImage = imoverlay(grayImage, mask, 'r');
imshow(overlayImage, []);
impixelinfo;
caption = sprintf('Masked Image. Angle = %.2f degrees.', props.Orientation);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
slope = tan(props.Orientation)
x = 1 : columns;
y = slope * (x - xCenter) + yCenter;
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
fprintf('Done running %s.m ...\n', mfilename);
Best Answer