How to close the gaps?

13 views (last 30 days)
Stelios Fanourakis
Stelios Fanourakis on 7 Sep 2018
Commented: Image Analyst on 9 Sep 2018
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?
  6 Comments
Stelios Fanourakis
Stelios Fanourakis on 8 Sep 2018
This is the most recent pic with the results so far. I managed to achieve a seamless with no gaps contour but as you can see from the pic, it has peaks and I need a method to smooth it.
Any ideas?
Image Analyst
Image Analyst on 8 Sep 2018
Attach your original gray scale images so we can try things with it.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 9 Sep 2018
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);
  8 Comments
Stelios Fanourakis
Stelios Fanourakis on 9 Sep 2018
What I want now is to calculate the area above soft tissue. All this black region starting from top of the image 'till the beginning of the tissue. Can you suggest a way to do it?
It must be some kind of a 2d integral?
Image Analyst
Image Analyst on 9 Sep 2018
I just went over column by column using find() to find the first row where the binary image was "true" for that column.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!