clc;
clear all;
close all;
workspace;
format long g;
format compact;
fontSize = 16;
baseFileName = 'bone_image.jpeg';
folder = pwd;
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, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
grayImage = grayImage(5:645, 6:647);
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram of gray scale image', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage = grayImage > 50;
binaryImage = bwareafilt(imfill(binaryImage, 'holes'), 1);
subplot(2, 2, 3);
imshow(binaryImage, []);
impixelinfo;
axis on;
title('Original Boundary', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
boundaries = bwboundaries(binaryImage);
x = boundaries{1}(:, 2);
y = boundaries{1}(:, 1);
hold on;
plot(x, y, 'r-', 'LineWidth', 3);
b = boundary(x, y, 0.9);
subplot(2, 2, 4);
imshow(binaryImage);
hold on;
plot(x(b), y(b), 'r-', 'LineWidth', 3);
axis image;
caption = sprintf('Boundary without Gap.');
title(caption, 'FontSize', fontSize);
Best Answer