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

Rik
Rik on 7 Sep 2018
I think you should try a spline instead of tampering with your image.
Rik can you pass me an idea of how to do it?
Why not just lower your threshold to get the darker parts of the tissue?
If you need more help, read this link and improve your post, which would include attaching the original image in a .png file with the green and brown frame icon.
How do I convert the smallest gray value to a bright sharp white contrast. I used several methods to increase the contrast
background = imopen(im,strel('disk',115));
background2 = imopen(L,strel('disk',115));
background3 = imopen(L>1,strel('disk',115));
im1= im-background
im2= L-background2
im3= (L>1)-background3
I3 = imadjust(im1)
I4 = imadjust(im2)
I5 = imadjust(im3)
bw = imbinarize(I3)
bw2 = imbinarize(I4)
bw3 = imbinarize(I5)
bq = bwareaopen(bw, 5);
bq2 = bwareaopen(bw2, 5);
bq3 = bwareaopen(bw3, 5);
But the slight dim gray tones are eliminated. they become black. I want this little information to be bright enough.
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?
Attach your original gray scale images so we can try things with it.

Sign in to comment.

 Accepted 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);

8 Comments

Thank you so much Image Analyst. Your solution is more than perfect match to what I wanted. I attach the original pic. It is not much different to what you got.
I'd also like to ask you whether you have implemented any filter to smooth out the bumps that may occur and a way to isolate the final segmented pic from the rest of the plots. So, I need a figure just the segmented one.
Thank you so much ImageAnalyst.
As you can see in the code, I used conv2() to blur/smooth the binary image. Actually, why do you want to smooth it? What's wrong with keeping the more accurate boundary?
I managed to get the individual figure with the final segmented image.
I now am in search of a smoothing method so no peaks and bumps occur
Because I need to calibrate the image in essence of distance in pixels from the top of the image to the boundaries of the soft tissue, where the contour lies.
So, I'd like to have a smooth curve contour for the distance in pixels to be more accurately integrated. I will also ask my professor tomorrow and he may wants to keep it as it is.
Image Analyst
Image Analyst on 9 Sep 2018
Edited: Image Analyst on 9 Sep 2018
You're basically saying you want to smooth it so that the boundary will be more accurate, which is not necessarily true. Why do you think the actual boundary is inaccurate? True there may be some noise, and some variation in that shape of the blob from image to image. That's why you do replicates (multiple images) and look at distributions of the measurements. A smoother shape does not necessarily follow the actual boundary more accurately unless you have a ton of noise, which I'm not seeing in your image. Actually the contrary is true: a smoothed boundary follows the actual boundary LESS accurately.
Image Analyst. Can you please explain me more the technique you used to scan the image and trace the top most pixel? (topRows). I guess this is the contour line. I didn't know that MaTlab had the option to find the first pixel in rows and columns.
Is there any documentation for this algorithm?
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?
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

Community Treasure Hunt

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

Start Hunting!