PiLab-CAU / ComputerVision-2401

Computer Vision Course 2024-01
Apache License 2.0
9 stars 3 forks source link

[Lecture3][0409] Question of Sobel fileter in lecture 3 #11

Closed HJH0303 closed 4 months ago

HJH0303 commented 7 months ago

@yjyoo3312 I have a questions in lecture 3 In lecture slide, Sobel filter of Sx is $\begin{bmatrix}-1&0&1\\-2&0&2\\ -1&0&1 \end{bmatrix}$ and Sy is $\begin{bmatrix}1&2&1\\0&0&0\\ -1&-2&-1 \end{bmatrix}$ But in the source code of lecture 3, you used filter2D without filpping the Sobel kernel. I heard that cv2.filter2D does actually compute correlation, not the convolution. image OpenCV doc: https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#filter2d

h_x = (1/8)*np.array([[-1.0, 0.0, 1.0],
                      [-2.0, 0.0, 2.0],
                      [-1.0, 0.0, 1.0]])

h_y = (1/8)*np.array([[1.0, 2.0, 1.0],
                      [0.0, 0.0, 0.0],
                      [-1.0, -2.0, -1.0]]) 

lenna_grad_x = cv2.filter2D(lenna_gaussian, -1, h_x, borderType=cv2.BORDER_CONSTANT)
lenna_grad_y = cv2.filter2D(lenna_gaussian, -1, h_y, borderType=cv2.BORDER_CONSTANT)

So I was wondering if the kernel of the filter doesn't have to flip?

yjyoo3312 commented 7 months ago

@HJH0303 Thank you for pointing out the good point!

As you mentioned, the filter2D function applies a correlation using the specified filter.

I understand that correlation and convolution are often used interchangeably in this field, so I'll make sure to specify which one it is in exams to prevent any misunderstandings :) However, to keep things clear, I always point out the original filtering operation we need to use.

For instance, in this scenario, to calculate the gradient x, we subtract the pixel intensity on the right from that on the left. Similarly, for the gradient y, we subtract the top pixel intensity from the bottom. (so, flipping the filter is not necessary in this case).

However, if I use the other specified function 'convolution' which performs convolutional operation,

lenna_grad_x = convolution(lenna_gaussia, h_x)

then, to obtain the gradient_x, you need to flip the original kernel to achieve the expected outcome.

Thank you

HJH0303 commented 7 months ago

@yjyoo3312 Thank you for your detailed answer :)