You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
i am doing image processing for red tomato detection.how can I do complete the fruit image and remove unwanted objects in background??
7 views (last 30 days)
Show older comments
i am unable to get good result. i am getting this result.see attach image. i want to remove these unwanted objects in background and need full image of tomato. kindly help me in this regard. i am attaching result image here.kindly help me soon. thanks
2 Comments
Walter Roberson
on 16 Feb 2018
The original image would help.
Question for you: is Ruby Grapefruit a red fruit? How about Blood Orange? Is peach a red fruit (at least some varieties?).
Darn it looks like this phone interface does not allow me to upload pictures, or I would attach a picture of some of the apples that grow on my tree for a decision as to whether they are red fruit or not.
By the way, are you aware that in the United States, tomato is legally classified as a vegetable and specifically not as a fruit? This had to do with a tax ruling because at the time fruit was subject to a tax that vegetables were not subject to.
Muhammad Hammad Malik
on 17 Feb 2018
this is the original image.i am doing red tomato detection. i do not idea about american classification, as i know tomato is known as fruit. so kindly help me in red tomato detection.how i can remove this background and make it complete tomato.thanks
Accepted Answer
Image Analyst
on 17 Feb 2018
Edited: Image Analyst
on 17 Feb 2018
See my demo: https://www.mathworks.com/matlabcentral/fileexchange/26420-simplecolordetection Or use the Color Thresholder app on the Apps tab of the tool ribbon.
26 Comments
Muhammad Hammad Malik
on 17 Feb 2018
thanks. i will try to understand and follow this then let you know is if i face any problem.thanks
Image Analyst
on 17 Feb 2018
Edited: Image Analyst
on 17 Feb 2018
Thank you. I've reproduced the error and am fixing it. It might have something to do with the new version. I haven't run it for a long time -- last time with some ancient version of MATLAB. There is now (with R2017b) an incompatibility with stem() and with the logo creating code. I've fixed those and am attaching the fixed code here.
Muhammad Hammad Malik
on 18 Feb 2018
thanks. now it is working. but even still i am unable to get good result.result is same like i above mention.
Image Analyst
on 18 Feb 2018
You might look at the Color Thresholder on the Apps tab of the MATLAB tool ribbon. You might look at that, or my other demos in my File Exchange, where segmentation is done in a different color space.
Muhammad Hammad Malik
on 30 Mar 2018
kindly tell me that how you choose values for H,S,V thresholds in your demo?
Muhammad Hammad Malik
on 31 Mar 2018
Edited: Walter Roberson
on 31 Mar 2018
thanks.i do not know too much about programming. so please help me. how you calculate these values using histogram and i make this code to detect red fruit. is it right or wrong what do you think?
%Assign the low and high thresholds
hueThresholdLow = 0.17;
hueThresholdHigh = graythresh(hImage);
saturationThresholdLow = graythresh(sImage);
saturationThresholdHigh = 0.45;
valueThresholdLow = graythresh(vImage);
valueThresholdHigh = 0.67;
% Now apply each color band's particular thresholds to the color band
hueMask = (hImage = hueThresholdLow) & (hImage >= hueThresholdHigh);
saturationMask = (sImage <= saturationThresholdLow) & (sImage = saturationThresholdHigh);
valueMask = (vImage = valueThresholdLow) & (vImage >= valueThresholdHigh);
what do you think. guide about it please.
Muhammad Hammad Malik
on 1 Apr 2018
Edited: Image Analyst
on 1 Apr 2018
What can the color thresholder do? I want to do red fruit detection using above mentioned programming not by using color thresholder. i randomly choosed these threshold values for red fruit detection, if someone ask that how you calculated these, then can i say that i selected these using color app, even i did not selected using color app just randomly selected?
Image Analyst
on 1 Apr 2018
Rnadom selection of thresholds won't work. You need a methodical way to pick the hue, saturation, and value. I'd do it in HSV color space. That said, there is a bit of a judgment call in exactly when red stops being red. It's a gradual transition. It's not like you have reds and then suddenly a huge gap in hues and now you have yellows. It's gradual and there is no one, "right" answer. See the gamut below:
And it might vary picture by picture so you'll have to come up with some algorithm whereby you analyze the h, S, and V histograms and pick some thresholds. Or you can use fixed thresholds that don't vary by picture but if you do that you'll have to calibrate your image intensities - a whole different problem.
Muhammad Hammad Malik
on 1 Apr 2018
i am not analyzing histogram.just using thresholding. as when i put my image in color thresholder in app tab then i move h,s,v where i can get maximum image. it has the same value which i am alrady using.is this right to do?thanks. see below image
Walter Roberson
on 1 Apr 2018
No, that is not the right thing to do.
For the particular variant you show the image of, you probably need to concentrate your effort on deciding the boundary between orange (not ripe) and red (ripe). However... https://store.tomatofest.com/category_s/33.htm the following image is of a fully ripe tomato:
"Take 'Black Krim', a brownish Russian variety with "a very salty taste," Hilgenberg says. Like many dark varieties, it comes from the area around the Black Sea. 'Cherokee Purple' is a burgundy tomato with a sweet, complex flavor sometimes described as having smoky undertones. 'Cream Sausage' is a meaty, finger-shaped tomato that is a pale, buttermilk yellow. Imagine it in a fresh salsa with cilantro. 'Dr. Carolyn' starts ivory and turns yellow as it ripens. 'Paul Robeson' is nearly black. 'Green Zebra' is green striped with yellow."
Image Analyst
on 1 Apr 2018
Muhammad, you ARE analyzing the histogram - just by looking at it. The histogram is that bar chart shown to the right of the image. It helps you visually decide WHERE to put the threshold value. As you can see from the hue histogram, there is no clear place where red stops. It just smoothly transitions to orange. So there is no "sensible" or "right" place to put the dividing line between red and orange. It's just whatever you say it is. I mean, who could ever claim that you're wrong? It's just a judgment call.
After you do the color segmentation, you can do further processing, for example use bwareafilt() or bwareaopen() to throw out blobs that are not the correct size or shape.
Muhammad Hammad Malik
on 2 Apr 2018
Edited: Walter Roberson
on 2 Apr 2018
this is also your demo @image analyst. in this you did color detection by hue. here how you selected threshold values for red?
% Red.
% IMPORTANT NOTE FOR RED. Red spans hues both less than 0.1 and more than 0.8.
% We're only getting one range here so we will miss some of the red pixels - those with hue less than around 0.1.
% To properly get all reds, you'd have to get a hue mask that is the result of TWO threshold operations.
hueThresholdLow = 0.80;
hueThresholdHigh = 1;
saturationThresholdLow = 0.58;
saturationThresholdHigh = 1;
valueThresholdLow = 0.55;
valueThresholdHigh = 1.0;
Image Analyst
on 2 Apr 2018
I selected them by looking at the histogram and guessing. Then tweaked the values until I got what I wanted.
Muhammad Hammad Malik
on 2 Apr 2018
then can i also do this thing too? is it right way to do or not?
Walter Roberson
on 2 Apr 2018
The image that you posted above, in https://www.mathworks.com/matlabcentral/answers/382021-i-am-doing-image-processing-for-red-tomato-detection-how-can-i-do-complete-the-fruit-image-and-remo#comment_536266, the top-right tomato appears to be fully ripe. But the tomato to the left of it: is it ripe or does it need another half day to ripen? The one to the right of it below the top one: probably not ripe. But it is going to change color as it ripens. At exactly what point between the color of the top right tomato and the one below it is a tomato to be considered ripe?
Muhammad Hammad Malik
on 3 Apr 2018
Edited: Muhammad Hammad Malik
on 3 Apr 2018
the one below tomato will not consider others are ok and ready to ripe. and how we can say that this tomato is 90% ripen and other is 80%?
Walter Roberson
on 3 Apr 2018
The bottom tomato, which is green, is certainly not ripe.
In my opinion, the tomato in the middle right cannot be said to be ripe. The middle left appears to be slightly more ripe, but I would not consider it ripe either.
But if the question is "Should these tomatoes be picked?" then to answer I would need to know "Picked for what purpose" ? The top right tomato I would not hesitate to serve in a salad in a restaurant. The top left tomato would do fine for cooking, and in a restaurant might be good enough for a toasted tomato sandwich, but I do not know that it would be good enough for a salad in a restaurant the prided itself on ingredients -- but if it were early afternoon when I was looking then I might be happy to pluck it in the evening for a meal. The two other tomatoes that are orange I would not serve to anyone in that form -- not unless the recipe was specifically for unripe tomatoes, such as making a spicy tomato pickle or making some kinds of salsa. But if it were getting late in the year and there was a danger of frost, I would no doubt pick them and put them in a sunny place to further ripen. I would also seriously consider picking them for further ripening if I was having difficulty with animals or insects attacking the plants: they are ripe enough that they will make a decent food if picked and allowed to ripen further. The green one? If frost was being warned, I would cover that green one but leave it on the vine for another couple of days.
Muhammad Hammad Malik
on 4 Apr 2018
Edited: Muhammad Hammad Malik
on 4 Apr 2018
thanks. i will pick up for selling purposes in market. some people wants completely ripe tomatoes and some want even half ripped or you can say green and red both. so i think if a tomato with 50% ripe, will be ok. but the question is that ow we can decide that one tomato is 90% ripe and other is 60% ripe? @Walter
Muhammad Hammad Malik
on 4 Apr 2018
you selected threshold by looking at histogram and guessing, then can i also do this thing too? is it right way to do or not? @Image Analyst ?
Walter Roberson
on 4 Apr 2018
"i will pick up for selling purposes in market"
For whose purposes? Is this like a farmer who is growing only one variety of tomato who wants to know which one to harvest to send to market, knowing that different buyers prefer different states of ripeness? Is this like a vendor in the market who is selling only one variety of tomato who wants to be able to provide a scanner that potential customers can point at a tomato and get back a ripeness reading so they can make a decision as to whether to buy it or not?
"you selected threshold by looking at histogram and guessing, then can i also do this thing too?"
No, not if you are creating a device which is being claimed to be able to meaningfully analyze tomatoes.
To be able to meaningfully analyze tomatoes, you need to first define what it means for a tomato to be ripe. Perhaps you can do that in terms of sugar concentrations; perhaps you can do it in terms of Ethylene emissions. Then you need a device that can non-destructively measure whatever property it is. You can then simultaneously photograph the subject tomato under conditions equivalent to what would be used in practice (remember to allow for both sunlight and overcast if the final device is to be used at an outdoor market). With readings and photographs in hand, you can then attempt to find a correlation between color in some colorspace and ripeness... for that variety. It is a reasonable hypothesis to suspect that you might be able to get within (for example) 15% by analyzing the pictures under good lighting, but you need to actually do the tests to determine that.
Alternately, you could suspect that industry has some method and you could google up some good keywords such as "industrial determination of ripeness of tomato" and see what you come up with.
Walter Roberson
on 21 Nov 2019
Imran Y: Okay, go ahead and write the code you need.
I would, however, recommend to you that you first figure out how to know when a tomato is sufficiently ripe for your desired purpose, taking into account all of the different varieties of tomatoes that you will be dealing with.
it might not turn out to be strictly necessary to measure Ethylene emissions but read that link anyhow because it is relevant.
More Answers (0)
See Also
Products
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 (한국어)