How can i segment a color image with region growing?
19 views (last 30 days)
Show older comments
Hi,
For a color Image Segmentation using Region Growing, how can i:
-Select seed pixels within the image
-Measure the color similarity
thanks
0 Comments
Answers (1)
Image Analyst
on 10 Nov 2012
Edited: Image Analyst
on 10 Nov 2012
Why does it have to be region growing? There are other methods. As you can tell from my icon, I do color image analysis all the time and I don't think I've ever needed to do region growing. The vast majority of the time standard methods like thresholding are fine. Perhaps you'd consider Delta E region growing since you pick a region and then it finds all regions like that in the image, though they don't need to be connected to the region you picked, like region growing requires. If you did want to exclude regions not connected, you could easily do it with ismember() or even morphological reconstruction (imreconstruct()). Please see my File Exchange for several color segmentation methods. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
I do have a magic wand demo, which is like the magic wand in Photoshop, which is a region growing method. I've only used it on gray scale images, though it could be adapted to work on the R, G, B, H, S, or V channel of a color image. One of those channels would be the gray scale image that you operate on.
% Demo to do similar to what the Magic Wand does in Photoshop.
% Only tested on gray scale images.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
uiwait(msgbox('Click a point'));
[x, y] = ginput(1);
row = max([int32(y) 1]);
column = max([int32(x) 1]);
% Get the gray level of the point they clicked on.
grayLevel = grayImage(row, column);
tolerance = 10;
% Construct abinary image within the gray level tolerance
% of where they clicked.
lowGL = grayLevel - tolerance;
highGL = grayLevel + tolerance;
binaryImage = grayImage >= lowGL & grayImage <= highGL;
subplot(2, 2, 2);
imshow(binaryImage, []);
caption = sprintf('Binary Image within \n+/- %d Gray Levels of %d', ...
tolerance, grayLevel);
title(caption, 'FontSize', fontSize);
% Get a marker image to reconstruct just the connected region
% and not all the other disconnected regions.
binaryMarkerImage = logical(zeros(rows, columns));
binaryMarkerImage(row, column) = true;
outputImage = imreconstruct(binaryMarkerImage, binaryImage);
subplot(2, 2, 3);
imshow(outputImage, []);
title('Magic Wand Reconstructed Binary Image', 'FontSize', fontSize);
% Get the masked image - the original image in the region.
maskedImage = zeros(rows, columns, 'uint8');
maskedImage(outputImage) = grayImage(outputImage);
subplot(2, 2, 4);
imshow(maskedImage, [0 255]);
title('Magic Wand Grayscale Image', 'FontSize', fontSize);
msgbox('Done with demo');
10 Comments
farah piani
on 1 Nov 2017
Image Analyst, I can not run your code for some of my file. Those are RGB files and i turned them into grayscale files. But still could not run them . Would you mind checking on them? I have attached the images here . Thanks you.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!