JYOTHI P — July 8, 2021
Advanced Computer Vision Image Image Analysis Libraries Programming Python Regression Technique Unsupervised

This article was published as a part of the Data Science Blogathon

Table of content

  1. What is OpenCV and its Uses
  2. Project Explanation
  3. Required libraries
  4. Code Explanation
  5. References
  6. Summary

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:

  1. Object detection
  2. Face Recognition
  3. Identify the objects
  4. Detecting Human activities
  5. Stitching the images to produce the highest resolution
  6. Recognize the scenery
  7. Recognizing images stored in the Database
  8. Remove Red Eye
  9. 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.

The non-programmers are also fallen in love with python as it is very simple to understand, read and write. Python is often compared with many programming languages such as Java, JavaScript, Perl, Tcl, or Small talk, c++, Common Lisp, and Scheme. The python program language is a combination of real-world constraints such as cost, availability, training. It is readily available to learn and evolve.

Project Explanation

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

import cv2

load the image

OpenCV has the largest libraries of which three of them they to read, write, and display.

  1. imread() to read an image
  2. imshow() to display the image
  3. 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")
Path of the image Photo Sketch using opencv
https://asknature.org/strategy/hummingbird-wrist-joints-rotate-to-maintain-hover/

‘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)
grayscale Photo Sketch using opencv

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
image invert Photo Sketch using opencv

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
image smoothing

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.

#final sketch

def dodgeV2(x,y):
return cv2.divide(x,255-y,scale=256)
final_img = dodgeV2(img_gray,img_smoothing)
final image

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

cv2.imwrite("final_image.jpg",final_img)

# 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.

 

References

For learning further Opencv please follow these links to gain more information

https://learnopencv.com/category/opencv-tutorials/

https://www.pyimagesearch.com/category/opencv/

 

Summary

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.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

About the Author

JYOTHI P

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *