Automated Image Segmentation using imcontour.

3 Ansichten (letzte 30 Tage)
Tim
Tim am 24 Jan. 2023
Kommentiert: Image Analyst am 24 Jan. 2023
Hi!
I have many images like the following:
I'm trying to write MATLAB code that segments out just the bone. (brightest white part) automatically. I've found that the imcontour function (output image available below) is able to differentiate the bone from the other visible structures in the image, but what color it assigns to just the bone varries from case to case. Is there any way to manipulate the function to just select the bone, or does anyone have any other tips for segmenting just the bone out of multiple images such as this automatically? (Or atleast mostly automatically).
Contour example:

Antworten (1)

Image Analyst
Image Analyst am 24 Jan. 2023
Try thresholding:
% Demo by Image Analyst
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 = 16;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'timothy dixon.png';
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
grayImage = 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(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get threshold
threshold = 120;
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
xline(threshold, 'Color', 'r', 'LineWidth', 2)
%--------------------------------------------------------------------------------------------------------
% Display threshold on histogram.
caption = sprintf('Histogram with threshold of %.1f gray levels', threshold);
title(caption, 'FontSize', fontSize)
% Get a mask
mask = grayImage >= threshold;
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
  2 Kommentare
Tim
Tim am 24 Jan. 2023
Thanks! This is a good option. The only issue is the intensity varies from image to image and subject to subject. Maybe I'll try a "color picker" where the user manually selects a pixel and it uses that for the threshold. Anyway, Thanks!
Image Analyst
Image Analyst am 24 Jan. 2023
It might be possible to use an automatic threshold selection function. The attached triangle threshold works very well for skewed histograms such as that. It finds the "corner" just below the hump on one side. So it would probably find one pretty close to the 120 for that image.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by