I need to segment the color blue on this image.
It seems easy, but the software doesn't recognize the blue from some other structures in the image - which are clearly not blue.
Can anyone help?

 Akzeptierte Antwort

Juderb
Juderb am 8 Mai 2014

0 Stimmen

Most color images are stored in RGB format. In other words, they are 3D matrices where the first, second, and third matrix represents the intensity of red, green, and blue respectively.
In your case, the simplest solution may be to isolate the blue-channel and perform segmenting on that image:
imageRaw = imread("source");
imageBlue = imageRaw(:,:,3);
imageBlueThresh = graythresh(imageBlue);
imageBlueBw = im2bw(imageBlue,imageBlueThresh);
What would you like to do with the segmented regions though? Remove everything else from the image? Outline them? Quantify something within those regions?

8 Kommentare

Haimon
Haimon am 8 Mai 2014
I need to measure the area of the blue regions in this image.
Ok, this code isn't perfect. But give it a try. It relies on converting from RGB, to HSL, and segmenting based on hue. There are a couple issues, but I think it gets the result you want.
imageRaw = imread("imageLocation");
imageRaw = double(image);
imageRaw = imageRaw./255;
% Determine max, min, and delta.
[imageMax,imageMaxIndex] = max(imageRaw,[],3);
imageMin = min(imageRaw,[],3);
imageDelta = imageMax - imageMin;
% Calculate the lightness.
imageLightness = (imageMax + imageMin)./2;
% Calculate the saturation.
imageLightLow = imageLightness<0.5;
imageSaturation = zeros(size(imageRaw(:,:,1)));
imageSaturation(imageLightLow) = (imageMax(imageLightLow)-imageMax(imageLightLow))./(imageMax(imageLightLow)+imageMax(imageLightLow));
imageSaturation(~imageLightLow) = (imageMax(~imageLightLow)-imageMax(~imageLightLow))./(2 - imageMax(~imageLightLow)+imageMax(~imageLightLow));
% Calculate the hue.
imageRed = imageRaw(:,:,1);
imageGreen = imageRaw(:,:,2);
imageBlue = imageRaw(:,:,3);
imageMaxRed = imageMaxIndex==1;
imageMaxGreen = imageMaxIndex==2;
imageMaxBlue = imageMaxIndex==3;
% Calculate the hue
imageHue = zeros(size(imageRaw(:,:,1)));
imageHue(imageMaxRed) = ((imageGreen(imageMaxRed)-imageBlue(imageMaxRed)))./(imageDelta(imageMaxRed))./6;
imageHue(imageMaxGreen) = (2+(imageBlue(imageMaxGreen)-imageRed(imageMaxGreen)))./(imageDelta(imageMaxGreen))./6;
imageHue(imageMaxBlue) = (4+(imageRed(imageMaxBlue)-imageGreen(imageMaxBlue)))./(imageDelta(imageMaxBlue))./6;
% Perform thresholding on the hue image.
imageHueThresh = graythresh(imageHue);
imageHueBw = im2bw(imageHue,imageHueThresh);
% Calculate the blue area.
areaBlue = sum(sum(imageHueBw));
Juderb
Juderb am 8 Mai 2014
The above code should result in the below segmented image. Visual comparison confirms the white regions are blue in the above image.
Haimon
Haimon am 8 Mai 2014
Juderb, thanks for your help.
I tried your code and showed:
Index exceeds matrix dimensions.
Error in blue (line 17) imageGreen = imageRaw(:,:,2);
Haimon
Haimon am 8 Mai 2014
When I disable the imageRaw = double(image); I can go all the way to the end. However I'm still struggling to save an image like yours.
Haimon
Haimon am 8 Mai 2014
Nope, my mistake. I forgot to substitute image for the imageRaw
tashu Dabariya
tashu Dabariya am 9 Jun. 2019
show this error Indexing cannot yield multiple results.
error in (line 6) [imageMax,imageMaxIndex] = max(imageRaw,[],3);
You're right - this code doesn't work. Not sure why it was accepted when it's wrong.
There may be other problems, but at least these two lines need to be fixed, and here they are:
imageRaw = imread('IMG_0860.JPG');
imageRaw = double(imageRaw); % NOT double(image) !!!!!!!!!!!!
If you continue to have problem, see my answer below or post your code and image in a new question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 8 Mai 2014

0 Stimmen

There are several ways to do it, and I show some of them in my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Or, if you have the stats toolbox, you can use kmeans: http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html

2 Kommentare

Juderb
Juderb am 8 Mai 2014
It looks like your DeltaE file would be perfect for this.
Image Analyst
Image Analyst am 8 Mai 2014
It could be. I'd have to look at the color gamut in 3D. Depending on the shape of the gamut, there are different methods that would be best. Delta E basically carves out a ball in LAB color space. Sometimes you need to carve out a sector using thresholding on the hue channel, and sometimes you need to also consider the saturation and value channels.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by