Skip to content
Advertisement

How to create a Python convolution kernel?

I’m trying to create a convolution kernel, and the middle is going to be 1.5. Unfortunately I keep running in to ideas on how to do that. I’m trying to create something similar to this

JavaScript

Advertisement

Answer

Since OpenCV uses Numpy to display images, you can simply create a convolution kernel using Numpy.

JavaScript

Here’s the kernel. Note the type is <class 'numpy.ndarray'>

JavaScript

To convolve a kernel with an image, you can use cv2.filter2D(). Something like this

JavaScript

For more information about kernel construction, look at this. Here are some common kernels and the result after convolving. Using this input image:

enter image description here

Sharpen kernel

JavaScript

enter image description here

Laplacian kernel

JavaScript

enter image description here

Emboss kernel

JavaScript

enter image description here

Outline kernel

JavaScript

enter image description here

Bottom sobel

JavaScript

enter image description here

Left sobel

JavaScript

enter image description here

Right sobel

JavaScript

enter image description here

Top sobel

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement