Can you threshold it? If that gives an OK boundary except for that gap, then use the boundary() function.
Try this:
clc; % Clear the command window.
clear all;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'bone_image.jpeg';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display image.
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 up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Crop off white frame surrounding the image.
grayImage = grayImage(5:645, 6:647);
%===================================================================================
% Get a histogram
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram of gray scale image', 'FontSize', fontSize, 'Interpreter', 'None');
% Convert to binary
% binaryImage = imbinarize(grayImage);
binaryImage = grayImage > 50;
% Fill blob and take the largest one.
binaryImage = bwareafilt(imfill(binaryImage, 'holes'), 1);
% Display image.
subplot(2, 2, 3);
imshow(binaryImage, []);
impixelinfo;
axis on;
title('Original Boundary', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get outline
boundaries = bwboundaries(binaryImage);
x = boundaries{1}(:, 2);
y = boundaries{1}(:, 1);
hold on;
plot(x, y, 'r-', 'LineWidth', 3);
% Get the boundary
b = boundary(x, y, 0.9);
subplot(2, 2, 4);
imshow(binaryImage);
hold on;
plot(x(b), y(b), 'r-', 'LineWidth', 3);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Boundary without Gap.');
title(caption, 'FontSize', fontSize);