Clear Filters
Clear Filters

Segmentation with the help of projection on axes

2 views (last 30 days)
I was trying to segment a breast image into left and right part exactly as i have a set of images for segmentation
How to take integral project on x axis of the image to detect the center of the breast for segmentation

Accepted Answer

Image Analyst
Image Analyst on 9 Mar 2019
Not hard at all. Simply use sum() to get the horizontal profile, then use min() and max() to find the middle. Then use indexing to extract the right and left half. You've probably solved it by now, but anyway, here's my code - compare it to your own:
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;
baseFileName = 'seg.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);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original RGB Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% 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(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the histogram of the image.
subplot(2, 2, 2);
[counts, binLocations] = imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%------------------------------------------------------------------------------
% 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 > 128;
% Crop off the white frame
allWhiteRows = all(binaryImage, 2);
binaryImage = binaryImage(~allWhiteRows, :);
allWhiteColumns = all(binaryImage, 1);
binaryImage = binaryImage(:, ~allWhiteColumns);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Cropped Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Get the horizontal count profile
horizontalProfile = sum(binaryImage, 1);
% Display the plot.
subplot(2, 3, 4);
plot(horizontalProfile, 'b-', 'LineWidth', 2);
grid on;
title('Horizontal Profile', 'FontSize', fontSize, 'Interpreter', 'None');
% Find the max in the left half.
midColumn = round(columns/2)
[~, leftMaxIndex] = max(horizontalProfile(1:midColumn))
% Find the max in the right half.
[~, rightMaxIndex] = max(horizontalProfile(midColumn+1:end));
rightMaxIndex = rightMaxIndex + midColumn
% Draw lines at peaks.
hold on;
line([leftMaxIndex, leftMaxIndex], ylim, 'Color', 'r', 'LineWidth', 2);
line([rightMaxIndex, rightMaxIndex], ylim, 'Color', 'r', 'LineWidth', 2);
% Now find the minimum in between the max:
% Find the max in the right half.
[~, minIndex] = min(horizontalProfile(leftMaxIndex+1:rightMaxIndex-1));
minIndex = minIndex + leftMaxIndex
line([minIndex, minIndex], ylim, 'Color', 'r', 'LineWidth', 2);
% Now crop the image into right and left halves:
leftImage = binaryImage(:, 1:minIndex);
rightImage = binaryImage(:, minIndex+1:end);
% Display the images.
subplot(2, 3, 5);
imshow(leftImage);
title('Left Half Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
subplot(2, 3, 6);
imshow(rightImage);
title('Right Half Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
0000 Screenshot.png
  1 Comment
Pravita Lekshmanan
Pravita Lekshmanan on 11 Mar 2019
Thankyou sir for your quick response;
But i am having issues in running this code
As my original image is a thermogram image as attached in the attachment
Using its ground truth (o_g.jpg) i have got the segemted image attached herein as segg.jpg
How can i get the segmentation into two halves of the breasts from that i cannot understand as it is already in gray levels sir.
Please help me with your valuable suggestions

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!