2

I have a to make an application that recognizes fruits. So far i have made that you can crop the image and get the color of the fruit you want. Now i am trying to get roundness of the fruit but i need the fruit to be black and the background to be white so i can find area and roundness value. This is my code so far for that part :

    crop_temp = rgb2gray(crop);
    threshold = graythresh(crop_temp);
    bw = im2bw(crop_temp,threshold);
    imshow(bw)

Crop i get passed when i crop the image. The problem gets when the fruit has a camera flash and that part stays white.

An example image is this lemon picture:

enter image description here

The problem is the white area in the lemon stays white after the code but i want it so that the whole lemon is black. But not just the lemon, but for other fruits to.

The problem is the white area in the lemon stays white after the code but i want it so that the whole lemon is black. But not just the lemon, but for other fruits to.

Yeah and how can you make so that the fruit is white and the background is black. I am new to image processing so don't jump on me. I just can't find specific stuff for this.

Maurits
  • 2,082
  • 3
  • 28
  • 32

3 Answers3

0

A brute force approach would be to check every white pixel in your image, and see if it is boxed in by black pixels in both the X and Y directions, turning it black if this is the case. This would take care of blobs inside your fruit, and shouldn't give you too many false-positives unless your fruit are strangely shaped, or you have a lot of noise around the edges of your image.

Isaac
  • 3,586
  • 1
  • 18
  • 20
0

Try this one:

fbw = ones(size(bw))-imfill(ones(size(bw))-bw);
imshow(fbw)
chaohuang
  • 3,965
  • 4
  • 27
  • 35
0

You can start by practicing with this Matlab demo, segmenting (and counting) rice in an image. In particular the part where the background is estimated.

Also helpful will be reading on Otsu's method and these two questions on background/foreground estimation on SO and DSP which take local statistics into account.

Community
  • 1
  • 1
Maurits
  • 2,082
  • 3
  • 28
  • 32