0

I have this code for calculating roundess

 I = imread('http://energyfanatics.com/wp-content/uploads/2008/05/banana.jpg');
%I = imread('http://www.nyapplecountry.com/images/photosvarieties/redrome04.jpg');
pic = rgb2gray(I);
threshold = graythresh(pic);
bw = im2bw(pic,threshold);
fbw = ones(size(bw))-imfill(ones(size(bw))-bw); 
invImg = ~fbw;
imshow(invImg);
f = bwlabel(invImg);
S = regionprops(f,'Area','Perimeter','centroid');
res = (min(sqrt([S.Area]),[S.Perimeter]/4)./(max(sqrt([S.Area]), [S.Perimeter]/4))).^2;

My problem is when i use the picture of banana i have multiple values in score, but when i use the other picture of an apple i get only one value. The problem lies in noise that is left behind. I was trying to use some edge detection methodes like sobel but I can't combine the edge detection with the black&white image.

So my question is how can i detect edge of an fruit so i get the whole fruit and how to get rid of noise. I just want to get the roundess of the fruit.

2thecore
  • 44
  • 1
  • 9
  • The coplete code can be found : http://stackoverflow.com/questions/12493320/determine-a-regions-average-in-matlab – 2thecore Feb 07 '13 at 11:41

3 Answers3

2

This is only a partial answer. The simplest edge detector is a high-pass filter. Filter your picture with one, and then use some sort of threshold to determine where the edges are. I'm too rusty in Matlab to remember how to do this well, but it goes something like this:

hp_filt = [0 1 0; -1 0 1; 0 -1 0];

filtered_pic = conv2(orig_pic, hp_filt);

edges = (filtered_pic > thresh);

You'll have to work out the threshold yourself.

Unfortunately, I don't have Matlab on hand to see if this works.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
1

fspecial in Matlab has predefined Sobel and Prewitt filters for edge detection. Simpler yet, use edge function. Of course, it should be done on a grayscale image.

You may also want to look at Hough transform for circular shape detection after detecting edges in image. hough seems to detect only straight lines, so you'll have to figure out how to adopt it for circular shapes. Or there are existing solutions at Matlab central (1, 2)

P.S. For your particular approach you may just filter objects returned by regionprops based on area threshold, say like this: Thr=100; S=S([S.Area]>Thr);

Victor K
  • 529
  • 2
  • 16
0

Complementing the answer of @Nathan Fellman and @Victor K, you can use the Canny Edge Detector. This method is less likely than the others to be fooled by noise, and more likely to detect true weak edges. MATLAB function edge also has an implementation:

BW = edge(I,'canny')
BW = edge(I,'canny',thresh)
BW = edge(I,'canny',thresh,sigma)
[BW,threshold] = edge(I,'canny',...)
Community
  • 1
  • 1
Yamaneko
  • 3,433
  • 2
  • 38
  • 57