Colour Segmentation in LAB colour space

Please help. I need some help to do yellow segmentation from my retina images. The goal is to use the LAB colour space, and then by means of distance calculation, obtain the new image with everything that is yellow become white and the background black.
Here is my code. I used gtool to select the yellow, and then it must be that yellow that the user has clicked on to determine which are the nearest distance to that colour. What must I further do?
RGB2=im2double(OriginalRGB);
[X Y]=ginput(2);
x=round(X);
y=round(Y);
punt=[x y];
cform = makecform('srgb2lab');
lab = applycform(OriginalRGB,cform);
l=lab(:,:,1);
a=lab(:,:,2);
b=lab(:,:,3);
pixel=double(lab(x,y,:));
[d1 d2]=size(lab);
for i=1:d1
for j=1:d2
euclidean_distance = norm(((pixel(:,:,2)-a).^2+ (pixel(:,:,3)-b).^2).^0.5);
end
end I need to obtain the segmented image?

 채택된 답변

Image Analyst
Image Analyst 2013년 7월 27일

0 개 추천

I have several color segmentation demos in my File Exchange. I doubt that LAB would be the best color space for finding yellow, though it could be depending on what your image's gamut looks like. But most likely, you're better off using hsv color space. Here's one demo of mine that finds yellow: http://www.mathworks.com/matlabcentral/fileexchange/28512-simple-color-detection-by-hue Check out the other segmentation tutorials I've uploaded also.

댓글 수: 6

Image Analyst
Image Analyst 2013년 7월 27일
Actually looking over what you attempted to do, find yellow by computing delta E (color difference), this can be done with my delta E demo in my File Exchange. The difference is that the hue segmentation demo uses predefined sector of the gamut for yellow, while the delta E demo required that you give some standard/reference color (by hand-drawing some area and computing the mean color within that) so it can determine the delta E between all the pixels and that standard/reference color.
Valeska Pearson
Valeska Pearson 2013년 7월 27일
편집: Image Analyst 2013년 7월 28일
Hello Image Analyst,
I went through your code, and it is really good , thanks. But can you please help me on a basic level how to understand the following: I've got an RGB image, in that image I obtain xy coordinate by gtool. Then the image is converted to lab colourspace. In LAB image I use xy coordinate to obtain a template in cie lab image. pixel=(lab(x,y,:)). Then I really struggle to get the following: I need to substract every pixel from this one (the template), find the minimum distance, which show that it is the most likely to that colour (template pixel). Then a binary image must be the result with smallest distance pixel(highest intensities) white and the others masked out; thus segmentation as a colour filter. I started with two for loops, so that iteration is over rows and columns. But what must happen inside for loops. I can't subtract template from a* and template from b* in the lab space? Double and uint8 mix up? Dimensions are not the same? Please help.
[X Y]=ginput(2);
cform = makecform('srgb2lab');
lab = applycform(OriginalRGB,cform);
subplot(2,2,2),imshow(lab)
l=lab(:,:,1);
a=lab(:,:,2);
b=lab(:,:,3);
LMean = mean2(l(y, x))
aMean = mean2(a(y, x))
bMean = mean2(b(y, x))
LStandard = LMean * ones(d1, d2);
aStandard = aMean * ones(d1, d2);
bStandard = bMean * ones(d1, d2);
deltaL = double(l) - LStandard;
deltaa = double(a) - aStandard;
deltab = double(b) - bStandard;
deltaE = sqrt(deltaL .^ 2 + deltaa .^ 2 + deltab .^ 2)
[d1 d2]=size(a);
for i=1:d1
for j=1:d2
binaryImage = deltaE(i,j,:) <= double(pixel);
end
end
figure;
imshow(uint8(binaryImage), [])
That won't work unless d1 and d2 are already defined. Plus you don't need mean2() and you don't need 2 for loops. And binaryImage is not a 3D image - it's a 2D logical image. Try it like this:
[X Y]=ginput(1);
x = int32(X);
y = int32(Y);
cform = makecform('srgb2lab');
lab = applycform(OriginalRGB,cform);
subplot(2,2,2);
imshow(OriginalRGB);
title('Original RGB Image');
subplot(2,2,2);
imshow(lab); % Will look really strange since it's displaying an lab image as rgb.
title('LAB image, displayed as RGB');
l=lab(:,:,1);
a=lab(:,:,2);
b=lab(:,:,3);
[d1 d2]=size(a);
LMean = l(y, x)
aMean = a(y, x)
bMean = b(y, x)
LStandard = LMean * ones(d1, d2);
aStandard = aMean * ones(d1, d2);
bStandard = bMean * ones(d1, d2);
deltaL = double(l) - LStandard;
deltaa = double(a) - aStandard;
deltab = double(b) - bStandard;
deltaE = sqrt(deltaL .^ 2 + deltaa .^ 2 + deltab .^ 2)
subplot(2,2,3);
imshow(deltaE , [])
title('Delta E Image');
binaryImage = deltaE <= 2; % Or whatever number you want.
subplot(2,2,4);
imshow(binaryImage, [])
thank you Image analyst, I appreciate it very much. May I ask other question.
Look at the following implementation, why does MATLAB have a problem with norm?
using lab from above code.
punt=uint8(lab(x,y,:));
d1=size(RGB3,1);
d2=size(RGB3,2);
binaryImage=zeros(d1,d2);
punt=punt./256;
for i=1:d1
for j=1:d2
pixel=lab(i,j,:);
afstand=(bsxfun(@minus,uint8(pixel),punt));
%afstand=double(afstand);
%afstand=norm(afstand);
binaryImage(i,j) = afstand;
end
end
Image Analyst
Image Analyst 2013년 7월 30일
I don't know, but like I said, you don't want to do it that way anyway, unless you want your program to be inefficient and slow.
thank you

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by