Build Sketches from Photographs using OpenCV
This article was published as a part of the Data Science Blogathon
Table of content
- What is OpenCV and its Uses
- Project Explanation
- Required libraries
- Code Explanation
Whats is Opencv and its Uses?
OpenCV stands for Open Source stands for Computer Vision, it is developed by Intel and Willow Garage, It is Officially launched in 1999 and later will Garage in 2000. OpenCV binds with Python to solve real-world problems. It supports languages like Python, Java, and MATLAB
The Opencv applications are as follows:
- Object detection
- Face Recognition
- Identify the objects
- Detecting Human activities
- Stitching the images to produce the highest resolution
- Recognize the scenery
- Recognizing images stored in the Database
- Remove Red Eye
- Follow the eye movement and many other real-time applications
The OpenCV is growing very rapidly because of its uses which are greatly applied in many areas. Many MNCs, Government Organizations, and research groups.
It is an Open Source and it is having many user communities and exceeding 18 million downloads.
It is optimized with 2500 algorithms to solve Machine learning and Deep learning Models, It is mainly developed to give acceleration to the machine products just like robots.
I feel more to come in the OpenCV Technology which plays a vital role in technological development. As technology grows, OpenCV applications are also growing very Extensively.
Introduction to Python:
Python programming language is an open Source, and it is simple and easy to learn and syntax emphasizes and supports many modules and packages.
It is having a major user community that is useful to new developers to proceed and clarify their doubts and as well as to contribute to the community.
In this project, we will learn to convert an image into a sketch, Though it is very interesting to learn.
Requirements are here: Python programming language and OpenCV
Let’s get started: Convert Normal image/photo to a pencil sketch using OpenCV
Importing the libraries
load the image
OpenCV has the largest libraries of which three of them they to read, write, and display.
- imread() to read an image
- imshow() to display the image
- imwrite() write an image into the local drive
Here Just I will explain the reading and writing images in the Opencv
The first argument is the image name and the location of the image(path of the image) and the second the image is not mandatory in many applications. Default syntax to read the images.
There are three types of flags which are mentioned below.
cv2.imread("file name",flags) # this is the syntax to read the image cv2.imread("Path of image",-1)# cv2.imread("Path of image",0) cv2.imread("Path of image",1)
img = cv2.imread("Path of the image")
‘imread’ is used to read the image, and we provided a path of the image.
convert the image into grayscale
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cvtColor(): This function converts an input image from one color space to another. By converting it into grayscale we can have a black and white image of our output
img_invert = cv2.bitwise_not(img_gray) #gray scale to inversion of the image
The function bitwise_not() calculates per-element bit-wise inversion of the input, which makes brighter regions into lighter and lighter regions into brighter, so we can find edges of the image very easily can converting the image creating the sketch.
img_smoothing = cv2.GaussianBlur(img_invert,(21,21),sigmaX=0,sigmaY=0) #smooting the inverted image
GaussianBlur(): This function reduces any noises of the images and blurring smoothens the source image with the specified Gaussian kernel. we have supplied convolution kernel and degree of blur.
They are three types of Blur techniques:
1.Averaging 2.Gaussing blurring 3. Median Blurring 4.Bilateral filtering
Averaging with a box filter simply takes the average of all pixels. This is done by the function by using cv. blur() or cv.boxFilter().syntax of the Averaging is cv.blur()
2.Gaussian Blurring is to add positive and odd, standard deviation X and Y directions, SigmaX and SigmaY respectively, It is highly effective to remove the noise from the image.
3. Median Blurring: Takes the median of all the pixels under the kernel area as the central element. It is replaced by the median value.
4.Bilateral filtering: Bilatering Filtering is highly effective in noise removal while keeping edges with no change. It does not take into consideration similar pixels’ differences.
def dodgeV2(x,y): return cv2.divide(x,255-y,scale=256) final_img = dodgeV2(img_gray,img_smoothing)
dodgeV2(): dividing the grayscale image by the inverse of the blurred image, remains with the highlights the boldest edges. This technique is commonly used by photographers to take print from the reel.
#dispalying the images
cv2.imshow('img',img) # displaying the original image cv2.imshow("Gray Image",img_gray) cv2.imshow("Inverted Image",img_invert) cv2.imshow("Smooth Image",img_smoothing) cv2.imshow("Final Photo",final_img)
Saving the image in your local drive
# destroy all windows
cv2.waitKey(0) #waits until the pressing any key. cv2.destroyAllWindows() # this function destroy all windows of images after clicking any button.
The waitKey() reacts to the keyboard functions,
It takes a single argument, and it’s a time(milliseconds) for which window will be displayed.
If the user passes the argument as 0 then the display will wait until the keystroke on the keyboard and can also set according to the user’s definite time.
destroyAllWindows() destroy all we created. It will clear the image and storage of the images which are displayed while execution from the main memory.
Have fun using these simple lines of code that can convert any number of images, just run the code and save any photo/image.
Simply deployment on any platform can also be used as App which converts the images to Sketches.
For learning further Opencv please follow these links to gain more information
we have learned imread(),imshow(),and imwrite() functions to read ,display and write images
cvtColor(), to convert the images into the grayscale
WaitKey() and destroyAllWindows(), close the application on keypress and clear everything from.