Heisenberg
Heisenberg

Reputation: 21

The direction of Gradient Filter in openCV

thanks for taking a look.

The question pops up when I read concepts help on this website

When applying a gradient convolution filter, with a different given direction, different edges are given depending on light intensity, see following:

filter result with positive direction

filter result with negative direction

Then I tried to implement the same in opencv with cv2.Sobel in y axis.

cv2.Sobel(img,cv2.CV_8U,0,1)  

The code only shows detected edges in one direction. I didn't manage to change the direction to show the edge detected with another intensity.

I am aware that with CV_64, one can detect both, but I wish to have them separately, exactly same as the example showed previously.

I found a trick to invert b/w of the image img=255-img, afterwards applying the same filter cv2.Sobel(img,cv2.CV_8U,0,1) cloud show the other edge as I expected.

I was wondering if it is possible just by cv2.Sobel function or any other opencv filter to control this filter direction without the need of inverting b/w of image .

Upvotes: 2

Views: 4910

Answers (1)

api55
api55

Reputation: 11420

OpenCV lets you apply the Sobel filter only in the y and in the x direction. You can check the documentation to see more info about the kernels applied.

For the x direction it is:

cv2.Sobel(img,cv2.CV_8U,1,0)  

And for the y direction:

cv2.Sobel(img,cv2.CV_8U,0,1)  

This last 2 numbers mean the order of the derivative of the image in x and y respectively. You can have more complicated ones as in both directions at the same time:

cv2.Sobel(img,cv2.CV_8U,1,1)  

Also, I see that in your images you have another operator (diagonal). I am not sure if you can do it with the sobel function directly, but certainly you can do it "manually" using filter2D.

cv2.filter2D(img, cv2.CV_8U, kernel)

where kernel is your kernel matrix that appears in your image.

kernel = np.array([[0, -1, -1],
                   [1,  0, -1],
                   [1,  1,  0]])

Upvotes: 3

Related Questions