clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 16;
folder = pwd;
baseFileName = 'Rabiulawal 1439.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
imshow(grayImage);
axis on;
impixelinfo;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
g = gcf;
g.WindowState = 'maximized';
g.NumberTitle = 'off';
g.Name = 'Demo by Image Analyst'
drawnow;
uiwait(msgbox('Click and drag out a box. Double click inside it to accept it.'));
[grayImage, rect] = imcrop();
hold on;
rectangle('Position', rect, 'EdgeColor', 'r', 'LineWidth', 2);
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = grayImage(:, :, 1);
end
hFig = figure;
subplot(2, 3, 1);
imshow(grayImage);
axis on;
impixelinfo;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
g = gcf;
g.WindowState = 'maximized';
g.NumberTitle = 'off';
g.Name = 'Demo by Image Analyst'
drawnow;
subplot(2, 3, 2:3);
imhist(grayImage);
grid on;
title('Cropped Image Histogram', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
threshold = 70;
xline(threshold, 'Color', 'r', 'LineWidth', 2);
mask = grayImage >= threshold;
subplot(2, 3, 4);
imshow(mask);
title('Initial Mask', 'FontSize', fontSize);
drawnow;
mask = bwareaopen(mask, 4);
se = true(1, 7);
mask = imclose(mask, se);
mask = bwareafilt(mask, 1);
subplot(2, 3, 5);
imshow(mask);
title('Final Mask', 'FontSize', fontSize);
drawnow;
[y, x] = find(mask);
[xCenter, yCenter, radius, a] = circfit(x, y);
subplot(2, 3, 6);
imshow(grayImage);
axis on;
impixelinfo;
caption = sprintf('(x, y) = (%.1f, %.1f). Radius = %.1f', xCenter, yCenter, radius);
title(caption, 'FontSize', fontSize);
impixelinfo;
hold on;
viscircles([xCenter, yCenter], radius);
if xCenter >= 1 && yCenter >= 1
plot(xCenter, yCenter, 'r+', 'LineWidth', 2, 'MarkerSize', 50);
end
msgbox('Done! Thank you Image Analyst!');
function [xc,yc,R,a] = circfit(x,y)
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];
a=A\B;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
0 件のコメント
サインインしてコメントする。