Which threshold method will work for my image?
Show older comments
I have implemented thresolding manually but it didn't separate foreground to background..plz help me to finalize threshold..need color information of mango part and want to remove background
rgb=imread(filepath);
% figure,imshow(rgb),title('Orignal Mango');
%% RGB to gray
% J = rgb2gray(rgb);
rr = rgb(:,:,1);
gg = rgb(:,:,2);
bb = rgb(:,:,3);
% figure,imshow(J),title('Gray image');
%% Threshold Image to black and white
[r1,c1]=size(rr);
rr1=zeros(r1,c1);
gg1=zeros(r1,c1);
bb1=zeros(r1,c1);
for i=1:r1
for j=1:c1
if(rr(i,j)>170)
rr1(i,j)=1;
end
if(gg(i,j)>170)
gg1(i,j)=1;
end
if(bb(i,j)>170)
bb1(i,j)=1;
end
end
end img = ~im2bw(cat(3,rr1,gg1,bb1));
figure,imshow(img),title('orignal Mango');
Accepted Answer
More Answers (1)
Image Analyst
on 28 Mar 2014
2 votes
Try the attached script, which uses color segmentation in the HSV color space. Basically your background has saturation values less than 0.1. So I found the background by thresholding at 0.1 and using that binary image to mask your original color images. The script produces this:

25 Comments
Image Analyst
on 28 Mar 2014
Try the new utility Sean is showing you. There is the ability to export the code. Otherwise you can convert to lab with makecform()
% Convert image from RGB colorspace to lab color space.
cform = makecform('srgb2lab');
lab_Image = applycform(im2double(rgbImage),cform);
% Extract out the color bands from the original image
% into 3 separate 2D arrays, one for each color component.
LChannel = lab_Image(:, :, 1);
aChannel = lab_Image(:, :, 2);
bChannel = lab_Image(:, :, 3);
but to segment out neutral colored pixels you'll have to compute the saturation anyway (sqrt(a^2+b^2))
mask = sqrt(aChannel .^2 + aChannel .^ 2) > 0.1;
or you'll have to threshold out a box in ab space. That might be okay if you have a big difference between your background and your mangos. Like
mask = abs(aChannel)<aThresh & abs(bChannel) < bThresh;
Salaheddin Hosseinzadeh
on 28 Mar 2014
@ Image Analyst
I'm wondering if you have some sort of tutorial for image processing beginners. I thought of reading a book, but then I realized I need to know the principles first. So I read about Mathematical Morpolog and ...
I really appreciate if you share your experience.
Tnx
Image Analyst
on 28 Mar 2014
Steve Eddins http://blogs.mathworks.com/steve/ has a book. All I have is my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 which gives a basic tutorial on image segmentation (find coins on a uniform background) and color segmentation (red red or yellow objects). Plus I have about 130 other demos that I upload one at a time every now and then. Someday I'll group them all together into a gray bag of image processing demos and upload them.
Salaheddin Hosseinzadeh
on 28 Mar 2014
@ Image Analyst
Thanks for your help.
Series of demos would be awesome, I already checked your file exchange, those examples are beyond my basic level. I'll be waiting for the demos!
Thanks in advanec.
Image Analyst
on 29 Mar 2014
It means to just scale the image from min to max for display. I always do it, though you don't really need it for logical images or RGB images, only for grayscale images. Basically it shows the min gray level, whatever it is, as 0, and the max gray level, whatever it may be, as 255.
Sabanam
on 29 Mar 2014
Image Analyst
on 29 Mar 2014
Edited: Image Analyst
on 29 Mar 2014
Please fix your post so we can read it: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Also, you read an image into the variable rgb, so it's a 3D uint8 image variable, but then you pass it to HSV_segment which expects a string (a filename) because you call imread again inside it. You can't call imread on a 3d uint8 numerical array, it needs a filename string.
Sabanam
on 29 Mar 2014
Image Analyst
on 29 Mar 2014
If we're done then please mark it as "Accepted". Thanks.
Sabanam
on 29 Mar 2014
Sabanam
on 29 Mar 2014
Image Analyst
on 29 Mar 2014
It worked pretty good for those. Just a few small noise particles that I got rid of using bwareaopen(). See attached script. Anyway, you accepted Sean's answer so I thought you were using that method instead.
Sabanam
on 29 Mar 2014
Sabanam
on 29 Mar 2014
Image Analyst
on 29 Mar 2014
I just ran my code again and it worked fine. You must have changed it. You did not let me know the change you made. Please post your code (attach your m-file with paper clip icon) if you want me to find out how you broke the code.
Sabanam
on 29 Mar 2014
Image Analyst
on 29 Mar 2014
What is "size_using_fuzzy"? That's not in my code. The vast majority of my code you commented out. Also, you did not attach 1.jpg.
Sabanam
on 29 Mar 2014
Sabanam
on 29 Mar 2014
Image Analyst
on 29 Mar 2014
I ran test.m on it and it ran fine. No errors whatsoever. Here's a screenshot as proof.

Image Analyst
on 29 Mar 2014
I actually ran your code too, and it ran with no errors. I just added an imshow(BW) so I could see the results. It's attached.
Sabanam
on 30 Mar 2014
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

