This is the 26th day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article shows you how to use OpenCV in Python3 to convert images to sketches.

above

An RGB color image in Opencv can be used to generate a sketch through the following four steps:

  • 1. TheRGBThe graph is transformed into grayscale graph.
  • 2. Reverse color operation for grayscale map.
  • 3. Blur the image in Step 2 with Gaussian blurGaussian blur.
  • 4. Mix the grayscale image in Step 1 with the fuzzy invert image in Step 3, which uses lighting (Dodging) and darkening (burning).

The implementation process

The original image

Step 1: Transform the image into a grayscale image

Implementation code:

import cv2
img_rgb = cv2.imread('example.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
cv2.imshow('original', img_gray)
cv2.waitKey(0)
Copy the code

The output is:

Step 2: Gray color reversal operation

Implementation code:

img_gray_inv = 255 - img_gray
Copy the code

The output is:

Step 3: Gaussian blur

Gaussian blur can effectively reduce the noise in the image and make the image smoother, which is mathematically equivalent to using Gaussian kernel to convolute the image.

Implementation code:

img_blur = cv2.GaussianBlur(img_gray_inv, ksize=(21.21), sigmaX=0, sigmaY=0)
Copy the code

The output is:

Step 4: Fusion of grayscale image and Gaussian blur film

Implementation code:

tmp = (image[col, row] << 8)/(255 - mask)
Copy the code

The output is:

A series of articles will follow this month,

Wonderful article, please pay attention.