Ask Your Question

How does the Sobel operator handle gradients over 255?

asked 2020-05-09 06:05:31 -0500

cohadruid gravatar image

updated 2020-10-12 07:48:08 -0500


I'm currently trying to implement my own Sobel operator and I'm using the OpenCV operator as reference. From what I can see, most edges I find are the same, but some of them go over 255 which causes an overflow of the uint8 type. I've tried setting those gradients to 255 and scaling the values to 0-255, but the result still isn't there.

An example below:

original image

OpenCV Sobel

My Sobel operator

My Sobel operator with overflowing gradients set to 255

My Sobel operator with values scaled to 0-255

I would be very grateful for any tips and tricks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-05-09 09:55:14 -0500

HYPEREGO gravatar image

You need to take care about it. From the documentation of Sobel function you can see that the third argument is ddepth that is the depth of the output image, try to put here CV_16U. Basically here you should provide a depth that avoid overflow. In the docs is well explained by the way, take a look and try it using different depth

edit flag offensive delete link more



Sorry for not replying before. I tried this but the problem actually wasn't in handling the magnitude, but handling Gx and Gy. I found that OpenCV automatically sets all Gx and Gy values over 255 to 255. That way, when using the sum of absolutes formula, the magnitude can never overshoot 255. With that adjustion I've been able to get an almost perfect replica, with some pixels differing by 1.

cohadruid gravatar imagecohadruid ( 2020-06-01 03:30:41 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-05-09 06:05:31 -0500

Seen: 97 times

Last updated: May 09