Skip to content Skip to sidebar Skip to footer

In Opencv, What Data Types Does Cv2.filter2d() Expect?

I'm teaching myself about edge detectors, and I'm trying to use OpenCV's filter2D to implement my own gradient calculator, similar to cv2.Sobel(). In the Python interface to OpenCV

Solution 1:

The coefficients of the convolution kernel should always be floating- point numbers. This means that you should use CV_32FC1 when allocating that matrix. In this particular example, try:

horizontalSobelMtx = [[-1,0,1],[-2,0,2],[-1,0,1]]
horizontalSobelMtx = numpy.asanyarray(horizontalSobelMtx, np.float32)

Solution 2:

I had the same problem but I believe I have a partial answer. Basically, you need to weight your kernel. The way the filter algorithm works is it takes your filter, multiplies it, then adds all the values together and uses them as the new value. The key is the ADDING part. Adding 8 different values (some negative, some positive) will usually result in a large number, and displayed, appears black (or in my case, all white). So you have to compensate for the addition. Divide all the values in your kernel by the size/area of the kernel. See the example here and note the line

kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);

They divide everything by kernel_size squared.

Myself, I did this, and it helped (something showed) but it was washed out, so I found my perfect weight by adding this to my code:

int d2weight = 1;
 //... start loop, make your Mat kernel
 kernel = kernel / d2weight;  //put right before your filter2d() callcreateTrackbar("kernel", "kernel", &d2weight, 255, NULL);

and fiddled with the bar and right around d2weight = 140 my picture 'popped' into view.

Post a Comment for "In Opencv, What Data Types Does Cv2.filter2d() Expect?"