MATLAB: How to close the gaps

contoursimage processingImage Processing Toolboxinterpolationsegmentation

As you can see from the picture I've uploaded,
there are some gaps in the contour segmentation, because at this point the soft tissue ultrasound image lacks of signal and the gray color becomes black, so the contour gets messed up.
I don't want this to happen. I want a seamless, continuous contour to segment the whole area without getting messed up.
My thinking was either
  1. to interpolate the gray pixels and fill the black gaps among the soft tissue borders, OR
  2. to find a 2d spline to interpolate the active contour.
Any idea of yours?

Best Answer

Stelios, since you didn't provide me with the original image I had to take a screenshot and try to fix it up in Photoshop. Then I thresholded, blurred, rethresholded, scanned for the top row, and overlaid it on the original image. Code is below. Let me know if you like it.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'sofar.jpg';
% Get the full filename, with path prepended.
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
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.



subplot(4, 1, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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;
% Binarize the image
binaryImage = grayImage >= 3;
% Extract the largest blob only
binaryImage = bwareafilt(binaryImage, 1);
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(4, 1, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;
% Blur the image
windowWidth = 25;
blurredImage = conv2(double(binaryImage), ones(windowWidth)/windowWidth^2, 'same');
% Threshold again.
binaryImage = blurredImage > 0.5;
% Display the image.
subplot(4, 1, 3);
imshow(binaryImage, []);
title('Smoothed Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;
% Scan the image looking for the top most pixel.
topRows = rows * ones(1, columns); % Preallocate.
for col = 1 : columns
thisTopRow = find(binaryImage(:, col), 1, 'first');
if ~isempty(thisTopRow)
topRows(col) = thisTopRow;
end
end
% Display the image.
subplot(4, 1, 4);
imshow(grayImage, []);
title('Original Image with top rows overlaid', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Show top line over it.
hold on;
plot(topRows, 'r-', 'LineWidth', 2);