[PYTHON] I tried to process the image in "sketch style" with OpenCV

Introduction

I think there are many people who share photos on SNS, including Instagram. At that time, I think that the image may be processed using the application. It has various functions such as adjusting brightness and color, retouching to make the skin look beautiful, and processing photos in a sketch style.

This time, I tried simple sketch-like image processing using OpenCV.

Image processing in "sketch style" with OpenCV

environment

The environment uses Google Colaboratory. The Python version is below.

import platform
print("python " + platform.python_version())
# python 3.6.9

Image display

Now let's write the code. First, import and set the library required to display the image.

import cv2
import matplotlib.pyplot as plt
import matplotlib

%matplotlib inline
matplotlib.rcParams['image.cmap'] = 'gray'

Prepare a sample image as well. This time, we will use the free image from Pixabay.

Now, let's display the prepared sample image.

image = cv2.imread(input_file) # input_file is the path of the image

plt.figure(figsize=[10,10])
plt.axis('off')
plt.imshow(image[:,:,::-1])

image.png

Conclusion

Now let's use OpenCV to process this image like a sketch. The code is below.

def sketch(image):
    pencilSketch_img = pencilSketch(image)
    bilateral_img = cv2.bilateralFilter(image, 75, 100, 100)
    sketch_img = cv2.bitwise_and(bilateral_img, pencilSketch_img)
    return sketch_img

def pencilSketch(image):
    gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    median_img = cv2.medianBlur(gray_img, 5)
    laplacian_img = cv2.Laplacian(median_img, cv2.CV_8U, ksize=5)
    _, thresh_img = cv2.threshold(laplacian_img, 100, 255, cv2.THRESH_BINARY_INV)
    pencilSketch_img = cv2.cvtColor(thresh_img, cv2.COLOR_GRAY2BGR)
    return pencilSketch_img

sketch = sketch(image)

plt.figure(figsize=[20,10])
plt.subplot(121);plt.imshow(image[:,:,::-1]);plt.axis('off')
plt.title("original image")
plt.subplot(122);plt.imshow(sketch[:,:,::-1]);plt.axis('off')
plt.title("sketch image")

image.png

The process of processing like a sketch is defined as a sketch function. The processing flow of the sketch function is as follows.

--Pencil-style image creation (pencilSketch function) --Smoothing (cv2.bilateralFilter) --Mask processing (cv2.bitwise_and)

The order is to draw a line with a pencil and paint it.

Description of each process

Each process will be explained below.

Pencil-style image creation (pencilSketch function)

The process of processing like a pencil drawing is defined as the pencilSketch function. The processing flow of the pencilSketch function is as follows.

I will display the image.

image = cv2.imread(input_file) #Loading the original image
pencilSketch_img = pencilSketch(image) #Pencil style image

plt.figure(figsize=[20,10])
plt.subplot(121);plt.axis('off');plt.imshow(image[:,:,::-1])
plt.subplot(122);plt.axis('off');plt.imshow(pencilSketch_img[:,:,::-1])[

image.png

For details on the pencilSketch function, refer to here.

Smoothing (cv2.bilateralFilter)

** Smoothing **, or smoothing, is simply ** blurring an image **. Blurring an image can also be said to smooth out changes in pixel values. Noise and edges are sudden changes in pixel values. Smoothing can eliminate or obscure noise and edges.

There are several methods of smoothing, one of which is the bilateral Filter. Bilateral means "both", but this filter can leave edges nicely. The smoothing filter blurs the image, including peaks such as edges. You can use the ** bilateral filter ** to blur the image while leaving ** edges **.

For more information on smoothing, please refer to here.

The code for bilateralFilter is below.

bilateral_img = cv2.bilateralFilter(image, 75, 100, 100)

plt.figure(figsize=[20,10])
plt.subplot(121);plt.axis('off');plt.imshow(image[:,:,::-1])
plt.subplot(122);plt.axis('off');plt.imshow(bilateral_img[:,:,::-1])

image.png

You can see that the image is blurry.

I used cv2.bilateralFilter to smooth the image. The usage of cv2.bilateralFilter is as follows.

Mask processing (cv2.bitwise_and)

Finally, use masking to combine the color image with the pencil-style sketch image. Use cv2.bitwise_and for masking. cv2.bitwise_and is a function that performs bitwise AND processing of two images.

The masking code is below.

merged_img = cv2.bitwise_and(bilateral_img, pencilSketch_img)

plt.figure(figsize=[20,10])
plt.subplot(121);plt.axis('off');plt.imshow(bilateral_img[:,:,::-1])
plt.subplot(122);plt.axis('off');plt.imshow(merged_img[:,:,::-1])

image.png

I was able to successfully put a pencil sketch on a color image.

Summary

How was that.

This time, I tried sketch-like image processing using OpenCV. Let's review the processing flow.

--Pencil-style image creation (pencilSketch function) --Smoothing (cv2.bilateralFilter) --Mask processing (cv2.bitwise_and)

I think it would be interesting to output an image by changing various parameters.

Recommended Posts

I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
Python OpenCV tried to display the image in text.
I tried to find the entropy of the image with python
I tried playing with the image with Pillow
I tried to describe the traffic in real time with WebSocket
I tried to make an image similarity function with Python + OpenCV
I tried to save the data with discord
I tried to integrate with Keras in TFv1.1
I tried to correct the keystone of the image
I tried using the image filter of OpenCV
I tried to learn the sin function with chainer
I tried to extract features with SIFT of OpenCV
I tried to detect the iris from the camera image
Former image processing engineer tried to solve Saizeriya's spot the difference with OpenCV
I tried to touch the CSV file with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to solve the soma cube with python
I tried to predict the horses that will be in the top 3 with LightGBM
Convert the image in .zip to PDF with Python
I tried to process and transform the image and expand the data for machine learning
I tried to solve the problem with Python Vol.1
I tried to compress the image using machine learning
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried to summarize the code often used in Pandas
I tried to illustrate the time and time in C language
I tried to summarize the commands often used in business
I tried to implement the mail sending function in Python
I can't log in to the admin page with Django3
I tried to divide the file into folders with Python
I tried to create an article in Wiki.js with SQLAlchemy
I tried trimming efficiently with OpenCV
Try blurring the image with opencv2
I tried to move the ball
I tried face recognition with OpenCV
I tried to estimate the interval.
I also tried to imitate the function monad and State monad with a generator in Python
I tried to find the affine matrix in image alignment (feature point matching) using affine transformation.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to solve the ant book beginner's edition with python
I tried to automate the watering of the planter with Raspberry Pi
I tried to build the SD boot image of LicheePi Nano
I tried to digitize the stamp stamped on paper using OpenCV
I tried to display the video playback time (OpenCV: Python version)
I tried to make a periodical process with Selenium and Python
I tried to get started with Bitcoin Systre on the weekend
I tried to expand the size of the logical volume with LVM
I tried to cut out a still image from the video
I want to check the position of my face with OpenCV!
I tried to improve the efficiency of daily work with Python
I tried to log in to twitter automatically with selenium (RPA, scraping)
I got an error when I tried to process luigi in parallel on windows, but the solution
I tried to implement PLSA in Python
I want to detect objects with OpenCV