2
$\begingroup$

When I have to sharpen an image using opencv, I use:

#Create our shapening kernel    
kernel_sharpening = np.array([[0,-1,0], 
                              [-1, 5,-1],
                              [0,-1,0]])# applying the sharpening kernel to the input image & displaying it.

sharpened = cv2.filter2D(image, -1, kernel_sharpening)

In above code sharpened is our resultant image. As you can see in above code I used opencv function named filter2D to perform convolution of input image with the kernel, and as a result I got sharpened image.

Recently I went through this link regarding image Super-Resolution (link)

And found out Keras has something similar to filter2D and Keras calls it Conv2D.

Its syntax is as follows:

dis2 = Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(dis1)

My question is what is the difference between opencv filter2D, and Keras Conv2D ?

(I assume both do the same role of convolution of image with a kernel, I may be wrong pls correct)

$\endgroup$

1 Answer 1

2
$\begingroup$

So from an architectural viewpoint you are right, both are 2D Convolutional Kernels with size of (3,3).

But there are some major differences. While cv2.filter2D(image, -1, kernel_sharpening) directly convolute the image dis2 = Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(dis1) only constructs a Conv2D Layer which is part of the Graph ( Neural Network). So Keras Conv2D is no operation for directly convolute an image.

Also the weights are different. In the cv2 Part [0,-1,0], [-1, 5,-1], [0,-1,0] are your weights.

dis2 = Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(dis1)

has standard weight initializer which is glorot uniform. so the weights would not even match.

Additionally the weights of a Conv2D Layer which represents the Keras way, will be learned during training stage of the neural network.

However if you neural network would have only this convolution layer and yields the same weights as the cv2 convolution, the result should be exactly the same.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.