Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize OpenCV Sobel filter given kernel size

Tags:

python

opencv

How to invoke OpenCV Sobel function to compute actual derivative of image given some kernel size? I.e. considering that image is a function of f(x, y) and x, y spans from [0, 0] to [width, height] in pixels?

dimg_x = cv2.Sobel(img, ddepth=-1, dx=1, dy=0, ksize=ksize)

Probably some scaling needs to be applied to gradient image, but I can't figure out how to compute it given kernel size.

like image 281
DikobrAz Avatar asked Dec 21 '25 22:12

DikobrAz


1 Answers

You can use the scale argument of Sobel:

cv2.Sobel(img, dx=dx, dy=dy, ksize=ksize, scale=2**(2+dx+dy-ksize*2), ddepth=CV_32F)

When you scale the output that way, you typically do not want to have ddepth=-1 but rather get the result as floats or doubles.

like image 167
P-Gn Avatar answered Dec 23 '25 11:12

P-Gn