DEV Community

Cover image for Creating more filters with OpenCV and Python
Ethan
Ethan

Posted on

6

Creating more filters with OpenCV and Python

Introduction

Hello! πŸ˜€

In this tutorial I will be showcasing some more filters using OpenCV and Python!
This is a continuation of my previous example which can be found here:
https://dev.to/ethand91/creating-various-filters-with-opencvpython-3077

I've already discussed how to create the virtual environment in previous tutorials so I will skip that part.

Well lets get started creating some more filters! πŸ₯³


Vignette Filter

First we will create the vignette filter. The vignette filter is achieved by creating a broad 2D Gaussian kernel.

def vignette(image, level = 2): 
    height, width = image.shape[:2]

    x_resultant_kernel = cv2.getGaussianKernel(width, width/level)
    y_resultant_kernel = cv2.getGaussianKernel(height, height/level)

    kernel = y_resultant_kernel * x_resultant_kernel.T
    mask = kernel / kernel.max()

    image_vignette = np.copy(image)

    for i in range(3):
        image_vignette[:,:,i] = image_vignette[:,:,i] * mask

    return image_vignette
Enter fullscreen mode Exit fullscreen mode

Here we generate the vignette mask using Gaussian kernels, we then generate the result matrix and then apply the mask to each of the image's color channels.

Vignette Filter


Embossed Filter

The next filter is the embossed filter:

def embossed_edges(image):

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

    image_emboss = cv2.filter2D(image, -1, kernel = kernel)

    return image_emboss
Enter fullscreen mode Exit fullscreen mode

Here we create an array for each of the channels and then apply it to the image via filter2D.

Embossed Filter


Outline Filter

The next filter is the outline filter:

def outline(image, k = 9): 
    k = max(k, 9)
    kernel = np.array([[-1, -1, -1], [-1, k, -1], [-1, -1, -1]])

    image_outline = cv2.filter2D(image, ddepth = -1, kernel = kernel)

    return image_outline
Enter fullscreen mode Exit fullscreen mode

Similar to the embossed filter but this time we increase the quality of the outlines.

Outline Filter


Style Filter

The final filter is one of my personal favorites, the style filter.

def style(image):
    image_blur = cv2.GaussianBlur(image, (5, 5), 0, 0)
    image_style = cv2.stylization(image_blur, sigma_s = 40, sigma_r = 0.1)

    return image_style
Enter fullscreen mode Exit fullscreen mode

This filter is really cool IMO.
Before calling stylization it's best to blur the image a bit for better results.

Style Filter


Conclusion

Here I have shown how to create more various filters with opencv/python. I hope this tutorial was useful to you.

If you have any cool filters please share them. 😎

The source code and the original image can be found via:
https://github.com/ethand91/python-opencv-filters


Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

β€œBuy Me A Coffee”

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (2)

Collapse
 
roshanabdullah profile image
Abdullah Roshan β€’

Interesting. Very informative!

Collapse
 
ethand91 profile image
Ethan β€’

Thank you :)

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

πŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ❀️ or leave a quick comment to share your thoughts!

Okay