This article was published as a part of the Data Science Blogathon.
In this article, we will be working on the application which will be capable enough to change the image to its watercolor art form, that we will be using just computer vision operations i.e. none of the machine learning techniques will be involved just refined image processing techniques will be enough to perform this problem statement.
So before discussing the approach and jumping on the code part let’s first discuss how the techniques used in this project help in the real-world scenario.
In this section of the article, we will list down all the valid steps/phases in developing the application that will convert our original image to watercolor effect art using computer vision operations.
import cv2 import numpy as np import matplotlib.pyplot as plt
Here we will first read and resize our sample image and for a better experience, we will use two sample images just to double-check our results.
image = cv2.imread('sample.jpg') plt.figure(figsize = [10, 10]) plt.title("Image");plt.axis('off');plt.imshow(image[:,:,::-1]);plt.show()
Output:
image = cv2.imread('sample2.jpg') plt.figure(figsize = [10, 10]) plt.title("Image");plt.axis('off');plt.imshow(image[:,:,::-1]);plt.show()
Output
Code breakdown:
image_resized = cv2.resize(image, None, fx=0.5, fy=0.5)
About Resize function
Here we are resizing the image so that kernel of a specific size (say 3×3) will give the same results irrespective of the distribution of the image. Keep a close eye on the parameters that we have given in the resize function.
image_cleared = cv2.medianBlur(image_resized, 3) image_cleared = cv2.medianBlur(image_cleared, 3) image_cleared = cv2.medianBlur(image_cleared, 3) image_cleared = cv2.edgePreservingFilter(image_cleared, sigma_s=5)
Code breakdown
image_filtered = cv2.bilateralFilter(image_cleared, 3, 10, 5) for i in range(2): image_filtered = cv2.bilateralFilter(image_filtered, 3, 20, 10) for i in range(3): image_filtered = cv2.bilateralFilter(image_filtered, 5, 30, 10)
Code breakdown
Before breaking down the code we need to understand why we are using loops for bilateral filtering? Answer: Because if we will increase the parameter value all at once then instead of watercolor art we will get the cartoony image which is not our goal hence we are applying the filtering in loops and gradually increasing the value of the sigma space.
Parameters of Bilateral filtering
:
So when we used to apply bilateral filtering it used to blur the image though it preserved the corner of the image and convert it into blur form so to remove that and get a clearer output we used to sharpen the image.
gaussian_mask= cv2.GaussianBlur(image_filtered, (7,7), 2) image_sharp = cv2.addWeighted(image_filtered, 1.5, gaussian_mask, -0.5, 0) image_sharp = cv2.addWeighted(image_sharp, 1.4, gaussian_mask, -0.2, 10)
Code breakdown:
plt.figure(figsize=[30,30])
plt.subplot(131);plt.imshow(image_sharp[:,:,::-1]);plt.title("Final Image");plt.axis('off');
plt.subplot(132);plt.imshow(image_cleared[:,:,::-1]);plt.title("Clear Impurities");plt.axis('off');
plt.subplot(133);plt.imshow(image[:,:,::-1]);plt.title("Original");plt.axis('off');
Output 1:
Output 2:
Original Image Source: Dreamstime
Code breakdown
As we have already discussed plotting the image previously in the article so can refer to that. There is only one point to be noted in this part which is a subplot that is dividing the section of the image into three parts:
So, from the above output, you can see that the image of Rober Downey Jr. has been converted to watercolor art you can convert any image you want by using the same process and image processing technique just to make sure the image quality is good enough to apply the filters. Do you think computer vision is an exciting concept to convert such images?
Here’s the repo link to this article. Hope you liked my article on the Convert your image to watercolor art using computer vision. If you have any opinions or questions, then comment below.
Connect with me on LinkedIn for further discussion. Read more articles on computer vision here.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Thank you very much, great Article!