# Image processing - Sobel filter problem (edge detection)

• 2009-07-25, 13:48
sbail
Image processing - Sobel filter problem (edge detection)
Hi everyone,

I'm trying to perform edge detection on a bitmap I get from the camera viewfinder, using a simple sobel operator. It's quite a straight forward task, but somehow the result doesn't look like expected (something like this for the x/y gradients: [url]http://www.dca.fee.unicamp.br/dipcourse/html-dip/c9/s1/retina-sobel-x-y.gif[/url] )

This is my code (both bitmaps are EGray256):
[CODE]

iGrayBitmap->LockHeap(ETrue);
iGrayBitmap->UnlockHeap(ETrue);

TInt x=0, y=0, i=0, dx=0, dy=0;

for(i=0; i<w*h; i++)
{
// if we're at the first column - first pixel of a row:
// increment rows x and reset columns y
if (i==(y+1)*w) { y++; x=0; }
// else increment columns
else { x++; }

// if we're not in the first column or the first/last row (image boundaries)
// apply Sobel filter
if(x!=0 && x!=w-2 && y!=0 && y!=h-2 )
{
dx = gImg[i-w-1] + gImg[i-w+1] + gImg[i-w]*2 - gImg[i+w-1] - gImg[i+w+1] - 2*gImg[i+w];

dy = gImg[i-w-1] + gImg[i+w-1] + gImg[i-1]*2 - gImg[i-w+1] - gImg[i-w+1] - 2*gImg[i+1];

proImg[i] = dx;

}
else
{
proImg[i] = 255;
}

}[/CODE]
As you can see here, the edges are detected, but where does all the white noise come from? Shouldn't it be just plain gray? (the test image is a white triangle on a light gray background, the screenshot is from the horizontal gradient)
[URL="http://tinypic.com/r/dpg1gh/3"]http://tinypic.com/r/dpg1gh/3[/URL]
Original image:
[URL="http://tinypic.com/r/11r7h37/3"]http://tinypic.com/r/11r7h37/3[/URL]

Does anybody see what might be the problem?

Thanks :)
• 2009-07-25, 15:41
serg3d
Re: Image processing - Sobel filter problem (edge detection)
You should show original image if you want meaningful answer. Anyway dy seems wrong.
• 2009-07-25, 16:41
sbail
Re: Image processing - Sobel filter problem (edge detection)
Original image:
[URL="http://tinypic.com/r/11r7h37/3"]http://tinypic.com/r/11r7h37/3[/URL]

As an addition, this is a screenshot from a plain black image (camera lid is shut):
[URL="http://tinypic.com/r/bfgk0w/3"]http://tinypic.com/r/bfgk0w/3[/URL]
• 2009-07-25, 17:26
serg3d
Re: Image processing - Sobel filter problem (edge detection)
Yep, you should get something like first image. Sobel doesn't do smoothing, you should get a lot of noise. You can pass through smoothing filter before edge detection filter. And don't forget fix error in dy - your code is not a sobel now.
• 2009-07-25, 18:22
sbail
Re: Image processing - Sobel filter problem (edge detection)
Okay, I've fixed the error in dy, well spotted, thanks :)

So you say the only reason why the filter produces so much noise is because there is no smoothing, but the sobel itself is okay?
• 2009-07-25, 21:13
sbail
Re: Image processing - Sobel filter problem (edge detection)
Okay, problem solved!
dx caused some problems when trying trying to convert it into a TUint8 (I expected it to work without explicitly specifying anything - but it didn't)
I use TUint8 (Abs(dx)) now and (TUint8) (Abs(dx) + Abs(dy)) for the gradient magnitude. Looks normal now, as expected.

Thanks for checking the code.