### Author Topic: Doubt in edge detection  (Read 1380 times)

0 Members and 1 Guest are viewing this topic.

#### macbook

• Jr. Member
• Posts: 45
##### Doubt in edge detection
« on: June 25, 2009, 11:44:01 PM »
Hey,
I have written the MATLAB code according to the algo given in he tutorials for edge detection.

"Edge Detection
Edge detection is a technique to locate the edges of objects in the scene. This can be useful for locating the horizon, the corner of an object, white line following, or for determing the shape of an object. The algorithm is quite simple:

sort through the image matrix pixel by pixel
for each pixel, analyze each of the 8 pixels surrounding it

record the value of the darkest pixel, and the lightest pixel

if (darkest_pixel_value - lightest_pixel_value) > threshold)
then rewrite that pixel as 1;
else rewrite that pixel as 0;

What the algorithm does is detect sudden changes in color or lighting, representing the edge of an object."

I want to know how to get the threshold value for best results.
If I calculate the threshold according to the original method of finding the mean of all the elements of image matrix, then its too big a value.

See, in this algo we find the difference between the largest and smallest neighbour of a matrix element. Now for a grayscale image, this difference is not too big, not more than 50 at extreme points and generally around 20-30. But as the pixel value is almost same for all pixels in a grayscale image and hence the threshold calculated by the normal method is always greater than the difference and the resulting image is completely black.

I ran the code on a 640x480 grayscale image. It gave the best result at threshold = 20 whereas the threshold calculated by computing the mean was coming out to be = 115.
Now how to calculate the accurate threshold?

• Supreme Robot
• Posts: 11,658
##### Re: Doubt in edge detection
« Reply #1 on: June 27, 2009, 04:20:47 PM »
Determining the threshold is kinda tricky. For example, if your image is very noisy, it'll find edges everywhere. If its very blurry, it might not find any edges at all. Depends on the image, too.

What I recommend is finding an optimal threshold for many different images that your robot (is this for a robot?) will expect to see. Then use the average threshold.

Algorithmically, what you can also do is find a mean variation of pixel values between pixels. An edge would be something above that mean, while non-edges would be below.

#### paulstreats

• Supreme Robot
• Posts: 1,381
##### Re: Doubt in edge detection
« Reply #2 on: June 27, 2009, 04:28:04 PM »
in noisy images, you might want to try and run a despeckle algorithm to remove dots and blotches below a certain size. then maybe increasing sharpness (or reducing colour count) to make better defined edges first.

There are many ways but edge detection on a raw image will always produce varied results unless you find a way to prepare the image better or have a studio quality camera

• Supreme Robot
• Posts: 11,658
##### Re: Doubt in edge detection
« Reply #3 on: June 27, 2009, 04:32:05 PM »
in noisy images, you might want to try and run a despeckle algorithm to remove dots and blotches below a certain size. then maybe increasing sharpness (or reducing colour count) to make better defined edges first.
Some people run Gaussian transforms on noisy images, but it only makes things worse if you suddenly get non-noisy images . . .

The leading method is after finding edges to do a second run and remove any 'short' edges. Short edges are probably noise, where long edges wouldn't be. But then that's yet another threshold . . .

#### macbook

• Jr. Member
• Posts: 45
##### Re: Doubt in edge detection
« Reply #4 on: June 28, 2009, 05:32:10 AM »
Mean variation as in standard deviation???
It wud be really helpful if u can provide the algo to do that.

• Supreme Robot
• Posts: 11,658
##### Re: Doubt in edge detection
« Reply #5 on: June 28, 2009, 07:07:36 AM »
nope I meant mean deviation . . .

http://mathworld.wolfram.com/MeanDeviation.html
http://en.wikipedia.org/wiki/Absolute_deviation

The mean deviation (also called the mean absolute deviation) is the mean of the absolute deviations of a set of data about the data's mean. For a sample size N, the mean deviation is defined by

http://mathworld.wolfram.com/images/equations/MeanDeviation/NumberedEquation1.gif

where x^_ is the mean of the distribution.