Pencil sketch any image using OpenCV
Since I love computer vision the most, usually I come up with various new concepts each day.
This post focuses on how one can convert any image(even your own photo) to a crayon pencil sketch.
What's the best partπ-
1)No graphic tools are required to learn
2) A few lines of codes
3)only OpenCV
How does this work? To convert any image to a crayon pencil sketch you need to first get hard edges and then highlight them over the original image. You can even manage to control the shades of colours.
Below are the parameters that you give in for AdaptiveThreshold in OpenCV-
1) Src(Source) - Your input image(any image)
2) MaxValue β A variable of double/integer type representing the value that is to be given if the pixel value is more than the threshold value.
3) adaptiveMethod β How to perform the thresholding, below are 2 ways you can pick and experiment
ADAPTIVE_THRESH_MEAN_C β threshold value is the mean of the neighbourhood area.
ADAPTIVE_THRESH_GAUSSIAN_C β threshold value is the weighted sum of neighbourhood values where weights are a Gaussian window.
4) thresholdType β A variable of integer type representing the type of threshold to be used. Eg. THRESH_BINARY
5) blockSize β A variable of the integer type representing the size of the pixel neighbourhood used to calculate the threshold value.
6) C β A variable of double type representing the constant used in both methods (subtracted from the mean or weighted mean).
AdaptiveThresholding which takes the 3 steps in the process is the major step and applying changes there will bring the most changes to the output
Now the Code -
import cv2
image = cv2.imread(image_file_path)
image = cv2.resize(image, (360, 360))
im3 = cv2.imread(image_file_path, 0)
im3 = cv2.resize(im3, (360, 360))
t = cv2.adaptiveThreshold(im3, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199, 10)
t1 = cv2.cvtColor(t, cv2.COLOR_GRAY2BGR)
add = cv2.addWeighted(image, 0.5, t1, 0.5, 0)
cv2.imshow('something', add)
cv2.waitKey(0)
Snaps of the results are below, one thing try out changing the numbers and some values and see the variations.
23 likes