# Thread: Image processing - Sobel filter problem (edge detection)

1. 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: http://www.dca.fee.unicamp.br/dipcou...-sobel-x-y.gif )

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;
}

}```
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)
http://tinypic.com/r/dpg1gh/3
Original image:
http://tinypic.com/r/11r7h37/3

Does anybody see what might be the problem?

Thanks

2. You should show original image if you want meaningful answer. Anyway dy seems wrong.

Original image:
http://tinypic.com/r/11r7h37/3

As an addition, this is a screenshot from a plain black image (camera lid is shut):
http://tinypic.com/r/bfgk0w/3

4. 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.

5. 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?

6. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.