DEV Community

Cover image for Creating an Image Sketch with OpenCV (10 Lines of Code).
Ogunbiyi Ibrahim
Ogunbiyi Ibrahim

Posted on

Creating an Image Sketch with OpenCV (10 Lines of Code).

Introduction

Have you ever considered utilizing a computer language to create a rough sketch of an image?
I'll show you how to make a sketch of an image without having any programming skills in this article. Hold on to your seat and take out your pen, because this is going to be enlightening and comprehensive.

Note: To follow along with this guide, you simply need a basic understanding of how computers function.

If you are curious about trying this out, here is the 10 lines of code below πŸ‘‡

    import cv2
    path = r'C:\Users\folksconnect\Pictures\2020-07'
    image_path = cv2.imread(path)
    grey_image = cv2.cvtColor(image_path,  cv2.COLOR_BGR2GRAY
    cv2.imshow('Image', grey_image)
    invert = cv2.bitwise_not(grey_img)
    blur = cv2.GaussianBlur(invert, (30, 30), cv2.BORDER_DEFAULT)
    invertedblur = cv2.bitwise_not(blur)
    sketch = cv2.divide(grey_img, invertedblur, scale =256.0)
    cv2.imwrite('sketch.png', sketch)
Enter fullscreen mode Exit fullscreen mode

But if you are not in a hurry, this article explains everything you need to know about creating an Image Sketch with OpenCV.

What is OpenCV?

OpenCV is a big open-source library for computer vision, machine learning, and image processing that is presently playing an important role in real-time operations, which are vital in today's systems. It can recognize objects, people, and even human handwriting in photographs and videos.
To summarize, we will utilize OpenCV to process a photo, i.e. to build an image sketch, in this guide.

What is Python?

Python is a programming language that is widely used to construct websites and apps, automate tasks, and analyze data. Python is a general-purpose programming language, which means it can be used to create a large variety of applications and is not specialized for any specific problem.

To be able to use OpenCV for image processing we have to import it in Python programming language, but before we start coding, let’s set up our environment so we can work efficiently.

Setting up your Python Environment

The first thing to do is to make sure you have a Python interpreter on your computer(PC), else here is a link to install Python on your Windows PC and MacOS.

Setting up the OpenCV Environment

The next step is to install the OpenCV library on your PC after you've successfully installed python on your computer.

To install OpenCV launch command prompt on your computer and run this command.

    pip install opencv-python
Enter fullscreen mode Exit fullscreen mode

Please make sure you have an internet connection whilst typing this command, as it will be downloaded from the internet. Once that has been downloaded successfully, you can now launch the Python idle installed on your PC. Just type python idle on the search bar of your computer (PC).

Implementing our Code

Note: Any text (variable) at the left hand-side before the assignment symbol ( = ) is used to store information, the statement(code) at the right hand-side is stored into the left hand side.

The first step after running our idle is to create a new Python script file. This can be done by typing the Ctrl + N shortcut and this will create a new file.

  • The second step is to write the statement
    import cv2
Enter fullscreen mode Exit fullscreen mode

This line of code import the OpenCV library into your Python code, so that you can gain functionalities of all the actions performed by it.

The next step is to assign the path of the image to the variable path and adding r at the front of the string(path). Here is an example below:

    path = r'C:\Users\folksconnect\Pictures\2020-07'
Enter fullscreen mode Exit fullscreen mode
  • The next statement to type is:
    image_path = cv2.imread(path)
Enter fullscreen mode Exit fullscreen mode

What the above line of code does is; it reads the path of the image you entered and stores it in the variable image_path.

Note if the path or image cannot be read (maybe it does not exist or there is an error in the path) this method returns an empty matrix.

  • The next line of statement to type is:
    grey_image = cv2.cvtColor(image_path,  cv2.COLOR_BGR2GRAY )
Enter fullscreen mode Exit fullscreen mode

This line converts the color of the image you stored into image_path into a grey color image.

  • To show this image you can type the code:
    cv2.imshow('Image', grey_image)
Enter fullscreen mode Exit fullscreen mode

This code shows the image in a dialog box with the name of the dialog box as image.

  • Now moving on to the next line of code:
    invert = cv2.bitwise_not(grey_img)
Enter fullscreen mode Exit fullscreen mode

This line of code is used to invert the image. it changes the image pixels to zero if the pixel is greater than zero and vice-versa. For instance; a white image will be changed to black.

Using incremental development law. You can also add this line of code to see how the inverted image looks like.

    cv2.imshow('image', invert)
Enter fullscreen mode Exit fullscreen mode
  • The next step is to blur the image and to do that
    blur = cv2.GaussianBlur(invert, (30, 30), cv2.BORDER_DEFAULT)
Enter fullscreen mode Exit fullscreen mode

The syntax for the GaussianBlur() method is:

    cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType)
Enter fullscreen mode Exit fullscreen mode

In GaussianBlur() method, you need to pass src and ksize values every time and either one, two, or all parameters value from remaining sigmaX, sigmaY and borderType parameter should be passed.

Both sigmaX and sigmaY parameters become optional if you mention the ksize(kernal size) value other than (0,0).

The src stands for the source file which we've input has invert since we want to work on invert.
The ksize value is always in tuple -- i.e. values enclosed in a parenthesis, You can set the value to any range you want depending on your preference.

The sigmaX and sigmaX are optional since ksize as been set
The borderType should also be included, but I love using the default type, so you pass in cv2.BORDER_DEFAULT

  • The next line of code is to invert the blur image again, and we can do that using
    invertedblur = cv2.bitwise_not(blur)
Enter fullscreen mode Exit fullscreen mode
  • The next line of code is to divide the image
    sketch = cv2.divide(grey_img, invertedblur, scale =256.0)
Enter fullscreen mode Exit fullscreen mode
  • Now we are done with our code the last code is just to write the image into a portable network graphic(PNG) and we can achieve that by typing
    cv2.imwrite('sketch.png', sketch)
Enter fullscreen mode Exit fullscreen mode

Congratulations, We have successfully create a sketch of an image. This is an example of the image we sketched using OpenCV.

Image Sketch

This is the full implementation of the code for easy access/use.

    import cv2
    path = r'C:\Users\folksconnect\Pictures\2020-07'
    image_path = cv2.imread(path)
    grey_image = cv2.cvtColor(image_path,  cv2.COLOR_BGR2GRAY
    cv2.imshow('Image', grey_image)
    invert = cv2.bitwise_not(grey_img)
    blur = cv2.GaussianBlur(invert, (30, 30), cv2.BORDER_DEFAULT)
    invertedblur = cv2.bitwise_not(blur)
    sketch = cv2.divide(grey_img, invertedblur, scale =256.0)
    cv2.imwrite('sketch.png', sketch)
Enter fullscreen mode Exit fullscreen mode

Conclusion

Note: Ensure you change the path of the image to your own path ( the path on your PC) as the above path is a directory to an image on my PC.

In this guide, We just built a sketch of an image using OpenCV in Python. If you followed through this guide properly you should be able to set up your own version of this project and also help you in explore other cool features of this awesome library e.g. face recognition and lots more.

You can also improve on it by adding/implementing other features and functionalities.

Useful Resources

Happy Coding!

Top comments (1)

Collapse
 
ttfd profile image
TTFD

This is awesome, thank you very much for this.