![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206566/0000%20Screenshot.png)
Fill a gap on an image surface
1 view (last 30 days)
Show older comments
Andrey Gizdov
on 2 Mar 2019
Answered: Image Analyst
on 2 Mar 2019
Hello,
I have an image as follows:
![bone_image.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206561/bone_image.jpeg)
I am using activecontour to detect the edges of the bone.
function [bw] = segmentBone(imageFile)
%Function for segmenting the bone from the background
imageFile = imageFile(:,:,1);
mask_width = 1;
border_width = 2;
imageFile = padarray(imageFile, [border_width, border_width], 0);
mask = zeros(size(imageFile));
mask(mask_width:end-mask_width,mask_width:end-mask_width) = 1;
small_image_itter = 651;
bw = activecontour(imageFile, mask, small_image_itter, 'Chan-Vese', 'SmoothFactor', 0.95);
end
This is the result I get:
![segmented_bone.jpeg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206562/segmented_bone.jpeg)
The issue is that crack in the middle of the bone. I want to flatten it, so that the lower surface looks smooth. This is an example of what I want to achieve:
![segmented_bone_flattened.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206563/segmented_bone_flattened.jpeg)
It is really important that the boundaries of the bone stay the same and do not get thicker!
Any ideas on how to solve this task?
Thanks in advance!
0 Comments
Accepted Answer
Image Analyst
on 2 Mar 2019
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);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206566/0000%20Screenshot.png)
0 Comments
More Answers (0)
See Also
Categories
Find more on 3-D Volumetric Image Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!