1

I have an image (cv::Mat, type CV_32F) representing grid-sampled height function. The grid has constant raster (dx,dy) per pixel.

I would like to estimate its gradient magnitude. Using OpenCV's Sobel filter, I approximate derivatives like this:

dfdx=zz.Sobel(zz,cv2.CV_32F,1,0,ksize=3,scale=?)
dfdy=zz.Sobel(zz,cv2.CV_32F,0,1,ksize=3,scale=?)
gradMag=np.sqrt(dfdx**2+dfdy**2)

The scale parameter is barely documented, but looking into the source, it is used to multiply derivative kernels, i.e. the (-1,0,1) for finite differences. Using the 3x3 Sobel kernel, I assumed the scale should then be 1/2*dx or 1/2*dy (finite differences scehme) to obtain derivatives in true scale, but that does not seem to be the case: I was testing this on a synthetic image of hemisphere with different raster but not getting consistent results.

How is scale supposed to be used to incorporate raster dimensions, thus getting real derivative estimates?

eudoxos
  • 18,545
  • 10
  • 61
  • 110

1 Answers1

1

Scale must be equal 0.25, from here: OpenCV's Sobel filter - why does it look so bad, especially compared to Gimp?

The normalization divisor for kernels can be calculated by the following fomula:

enter code heref = max(abs(sumNegative), abs(sumPositive))

where sumNegative is the sum of negative values in the kernel and sumPositive the sum of positive values in the kernel.

Nuzhny
  • 1,869
  • 1
  • 7
  • 13