You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Which threshold method will work for my image?
2 views (last 30 days)
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
Sean de Wolski
on 28 Mar 2014
I'd just use R2014a's color thresholder app. You can pick your colorspace, make your selections until you like the result and then generate code to repeat the process on other images. Here it is using b*
5 Comments
Sabanam
on 28 Mar 2014
Edited: Sabanam
on 28 Mar 2014
Is it available in matlab2013b?..Where can i find it?Actully,I have 200 image database and i have to remove background but images have unequal distribution of background which is very harder to remove.I have to work on this real data set...So please provide some code which can help to subtract background..as soon as possible..
Image Analyst
on 28 Mar 2014
I don't believe so but Brett's tool is similar: http://www.mathworks.com/matlabcentral/fileexchange/38484-segmenttool-an-interactive-gui-for-segmenting-images
Image Analyst
on 29 Mar 2014
Edited: Image Analyst
on 29 Mar 2014
You can see from the bottom plot that if b is less than 20, it's background, and if b is > 20 it's mango. However if you do that and have reddish mangos, you won't get them. You're going to have to use both a and b to calculate the saturation (chroma) like I showed you in my answer. Be sure to show the older comments so you see mine.
More Answers (1)
Image Analyst
on 28 Mar 2014
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
Sabanam
on 28 Mar 2014
Edited: Sabanam
on 28 Mar 2014
Sir,you have used HSV color space for color image segmentation but in my reaserch work i have focused on CIEL*a*b* color model and its advantage using image segmentation.So can you show same thing using CIEL*a*b* color model?Can it possible?
If yes then How?..Please reply as i need very urgent for Presentation
Thank in advance...
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
One more thing i want to ask that i clearly understand how in hsv you have taken threshold in saturation but not getting how to take threshold in CIELab color model..you had explained how to convert to Lab and all but not getting in which channel i should take threshold..So please kindly send me function of Lab based segmentation for my image which i can directly put and analyse my code..because m not practically getting about Lab model for segmentation..Please reply as soon as possible...
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.
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
Sir...i didnt know which answer i should accept as i am new in this mathworks..So i just pressed it...I have implemented your's one using Lab and HSV as you have given idea but in HSV for above image it didnt work..so i ll just try it and acknowledge hows it giving results...
Vote of thanks to you sir....
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
Actually i m running just your test file but dont know y its happening..
if true
% filepath='D:\mt-prc\Mango db\size_fuzzy\large\totapuri\1.jpg';
rgb=imread(filepath);
%%RGB to gray
J = rgb2gray(rgb);
%%Segmentation using HSV
BW=hsv2seg(rgb);
end
< <https://www.dropbox.com/s/b1wjca4nx5mrgo8/error1.JPG> >
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.
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.
Sabanam
on 29 Mar 2014
Edited: Sabanam
on 29 Mar 2014
Ya..but in my matlab it shows like
Undefined function 'eml_assert_all_constant' for input arguments of type 'double'.
I will fix the bug.Don't you show a proof..I think it may have internal toolbox error..So i ll tell if not short out this bug...Thank you for the help....It means a lot to giving a time for this session..
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)