Creating a pencil sketch image from colored image using OpenCV
This project is a task I created for my Data Science intern at LetsGrowMore.
Table of Contents
- What is OpenCV?
- Step 1: Reading the Image
- Step 2: Convert image to grayscale
- Step 3: Convert gray image to inverted grayscale
- Step 4: Blurring the negative image
- Step 5: Inverting the blurred image
- Step 6: Mixing up grayscale with inverted blurred image
- Step 7: Displaying it with original image using Matplotlib
- Step 8: Saving the picture
- What I’ve learnt
- Things to try it out next!
- References
What is OpenCV?
OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.
It provides a wide range of features, including object detection, face recognition, and tracking.
So here, we’ll convert a colored image to pencil sketch using Open CV.
Here I’ve used Google Colab as my IDE.
Step 1: Reading the Image
import cv2
import matplotlib.pyplot as plt
#imported cv2_imshow specific for google colab
from google.colab.patches import cv2_imshow
#to read the image
image= cv2.imread('desktop.png')
#to display the image
cv2_imshow(image)
OpenCV uses BGR color scheme, so here there is no need to change the color. But when we use Matplotlib we need to convert as it uses RGB color scheme
Step 2: Convert image to grayscale
#converting the image to grayscale
gray= cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2_imshow(gray)
Step 3: Convert gray image to inverted grayscale
Inverted grayscale image is also known as Negative image. This is done to enhance the details of the image.
#converting image to inverted grayscale
inv_gray= 255-gray
cv2_imshow(inv_gray)
Step 4: Blurring the negative image
Find the kernel size using trial and error; For larger images, kernel size should be larger.
For smaller images, it should be in small number like 3x3 or 5x5. Giving large kernel value in small image will remove the actual structures of the image.
blur_img=cv2.GaussianBlur(inv_gray,(101,101),0)
cv2_imshow(blur_img)
Step 5: Inverting the blurred image
#inverting the blurred image
inv_blur=255-blur_img
cv2_imshow(inv_blur)
Step 6: Mixing up grayscale with inverted blurred image
To finalize our pencil sketch we need to mix up grayscale with inverted blurred image.
We’ll be using CV2 divide method to perform that action
CV2 divide performs per-element division of two arrays or a scalar by an array.
sketch_img= cv2.divide(gray,inv_blur,scale=255.0)
cv2_imshow(sketch_img)
We did it!!
Step 7: Displaying it with original image using Matplotlib
As I have already mentioned, since we are using Matplotlib here to display the image side by side, we need to convert the original image as well as sketched image.
It is because, OpenCV uses BGR color scheme and matplotlib uses RGB color scheme. Same for sketch picture also.
plt.figure(figsize=(14,7))
plt.subplot(1,2,1)
plt.title('Actual image in Matplotlib')
plt.imshow(image)
plt.axis('off')
plt.subplot(1,2,2)
plt.title('Color converted image in Matplotlib')
im = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(im)
plt.axis('off')
plt.show()
plt.figure(figsize=(14,7))
plt.subplot(1,2,1)
plt.title('Actual image in Matplotlib')
plt.imshow(sketch_img)
plt.axis('off')
plt.subplot(1,2,2)
plt.title('Color converted image in Matplotlib')
rgb_sketch=cv2.cvtColor(sketch_img, cv2.COLOR_BGR2RGB)
plt.imshow(rgb_sketch)
plt.axis('off')
plt.show()
#displaying original image and pencil sketch image side by side
plt.figure(figsize=(14,7))
plt.subplot(1,2,1)
plt.imshow(im)
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(rgb_sketch)
plt.axis('off')
plt.show()
Step 8: Saving the picture
Finally we have reached the final step. Saving the picture. imwrite is used to save the picture.
# saving the picture
# Filename
filename = 'sketch.png'
cv2.imwrite(filename, sketch_img)
You can checkout my GitHub code here!!
What I’ve learnt
- OpenCV is a great source for computer vison knowledge for beginners
- Matplotlib always display image as BGR and open cv displays image in RGB color scheme
- Kernel size in Gaussian Blur should be used based on image size. If image size is larger, kernel size should have high value
Things to try it out next!
- Pencil sketching multiple images at a same time
- Trying out median blur instead of Gaussian Blur in blurring images
- Using concat method in Numpy to display the images side by side instead of Matplotlib
You can also check out the references for different approaches!!
References
- https://towardsdatascience.com/generate-pencil-sketch-from-photo-in-python-7c56802d8acb
- https://stackoverflow.com/questions/19580102/inverting-image-in-python-with-opencv
- https://www.askpython.com/python/examples/images-to-pencil-sketch
- https://pyimagesearch.com/2021/04/28/opencv-smoothing-and-blurring/#:~:text=To%20average%20blur%20an%20image,image%20with%20increasing%20sizes%20kernels.
Checkout my other Medium articles here!! Thank you! Happy Learning!!