Which threshold method will work for my image?

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');

 採用された回答

Sean de Wolski
Sean de Wolski 2014 年 3 月 28 日

4 投票

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 件のコメント

Sabanam
Sabanam 2014 年 3 月 28 日
編集済み: Sabanam 2014 年 3 月 28 日
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
Image Analyst 2014 年 3 月 28 日
Sean de Wolski
Sean de Wolski 2014 年 3 月 28 日
New in 14a :)
Sabanam
Sabanam 2014 年 3 月 29 日
From above pic can you give me the segmentation code in Lab color space as i dont have 2014a...
Image Analyst
Image Analyst 2014 年 3 月 29 日
編集済み: Image Analyst 2014 年 3 月 29 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 3 月 28 日

2 投票

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 件のコメント

Sabanam
Sabanam 2014 年 3 月 28 日
編集済み: Sabanam 2014 年 3 月 28 日
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
Image Analyst 2014 年 3 月 28 日
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
Salaheddin Hosseinzadeh 2014 年 3 月 28 日
@ 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
Image Analyst 2014 年 3 月 28 日
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
Salaheddin Hosseinzadeh 2014 年 3 月 28 日
@ 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.
Sabanam
Sabanam 2014 年 3 月 29 日
編集済み: Sabanam 2014 年 3 月 29 日
@ Image analyst
What is the meaning of imshow(mask,[]) in your code 'test.m'...i am not getting about "'[]'" this syntax in imshow...
Image Analyst
Image Analyst 2014 年 3 月 29 日
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
Sabanam 2014 年 3 月 29 日
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
Image Analyst 2014 年 3 月 29 日
編集済み: Image Analyst 2014 年 3 月 29 日
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
Sabanam 2014 年 3 月 29 日
thanks sir...got your point and fix the bug...
Image Analyst
Image Analyst 2014 年 3 月 29 日
If we're done then please mark it as "Accepted". Thanks.
Sabanam
Sabanam 2014 年 3 月 29 日
Sir,I have try ur HSV based segmentation for below green
image but its not as accurate as above images...Please tell me which thresold will be better for saturation including below and above images...
Sabanam
Sabanam 2014 年 3 月 29 日
Please reply...Its urgent
Image Analyst
Image Analyst 2014 年 3 月 29 日
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
Sabanam 2014 年 3 月 29 日
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....
Sabanam
Sabanam 2014 年 3 月 29 日
Shows the error in your last updated code...
Undefined function 'eml_assert_all_constant' for input arguments of type 'double'.
Image Analyst
Image Analyst 2014 年 3 月 29 日
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
Sabanam 2014 年 3 月 29 日
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
Image Analyst 2014 年 3 月 29 日
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
Sabanam 2014 年 3 月 29 日
Ya..actually m not displaying all the things as i need only segmentation...
Sabanam
Sabanam 2014 年 3 月 29 日
I just noticed that even m running your test.m file it shows above error...Don't y happening actually?
Image Analyst
Image Analyst 2014 年 3 月 29 日
I ran test.m on it and it ran fine. No errors whatsoever. Here's a screenshot as proof.
Sabanam
Sabanam 2014 年 3 月 29 日
編集済み: Sabanam 2014 年 3 月 29 日
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
Image Analyst 2014 年 3 月 29 日
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
Sabanam 2014 年 3 月 30 日
its working now...I have uninstalled matlab and reinstall the matlab...Now its working. ..
cheers the results.vote of thanks to you

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2014 年 3 月 27 日

コメント済み:

2014 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by