clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'image.jpeg';
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 = rgb2gray(grayImage);
end
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
verticalProfile = all(grayImage == 255, 2);
row1 = find(~verticalProfile, 1, 'first');
row2 = find(~verticalProfile, 1, 'last');
horizontalProfile = all(grayImage == 255, 1);
col1 = find(~horizontalProfile, 1, 'first');
col2 = find(~horizontalProfile, 1, 'last');
grayImage = grayImage(row1:row2, col1:col2);
subplot(2, 3, 2);
imshow(grayImage, []);
axis('on', 'image');
title('Cropped Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
[rows, columns, numberOfColorChannels] = size(grayImage);
subplot(2, 3, 3);
imhist(grayImage);
grid on;
title('Histogram of gray image', 'FontSize', fontSize);
mask = grayImage < 25;
subplot(2, 3, 4);
imshow(mask, []);
impixelinfo;
title('Initial Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
imageSizeX = columns;
imageSizeY = rows;
[columnsInImage, rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
centerX = columns/2;
centerY = rows/2;
radius = 450;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
mask = mask & circlePixels;
se = strel('disk', 2, 0);
mask = imopen(mask, se);
mask = bwareafilt(mask, 1);
mask = imfill(mask, 'holes');
windowSize = 17;
kernel = ones(windowSize, windowSize) / windowSize ^ 2;
mask = imfilter(mask, kernel) > 0.5;
subplot(2, 3, 5);
imshow(mask, []);
impixelinfo;
title('Final Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
boundaries = bwboundaries(mask);
subplot(2, 3, 6);
imshow(grayImage);
hold on;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
plot(x, y, 'r-', 'LineWidth', 2);
end
title('Image With Boundaries', 'FontSize', fontSize, 'Interpreter', 'None');
2 Comments
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/663563-how-to-trace-the-boundray-of-object-in-an-image-using-matlab#comment_1162683
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/663563-how-to-trace-the-boundray-of-object-in-an-image-using-matlab#comment_1162683
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/663563-how-to-trace-the-boundray-of-object-in-an-image-using-matlab#comment_1162698
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/663563-how-to-trace-the-boundray-of-object-in-an-image-using-matlab#comment_1162698
Sign in to comment.