Computer Vision Tutorial with Python

Computer vision is all about the applications and algorithms to build intelligent systems to obtain information from images. In this article, I will introduce you to computer vision tutorial with Python.

What is Computer Vision?

Today, the use of images and videos is everywhere. From sharing photos online to downloading wallpapers, we see the use of images everywhere. Even the search engines are no less so, search for a query and you’ll get the most relevant images in no time.

Giving instructions to the computer and creating machine learning algorithms to figure out what’s inside pictures is what computer vision is. Some of the popular computer vision applications available today are:

  1. Image search
  2. Robot navigation
  3. Medical imaging
  4. Face lock and many more.

With the use of computer vision, we can also today build intelligent systems to track a person moving through a complex object. We can also try to find the names of all the people inside a photograph using the combination of face detection and recognition.

Computer Vision Tutorial with Python

Computer vision allows a computer to make useful decisions about real physical objects and scenes based on detected images. In this section, I will introduce you to a tutorial on computer vision with Python.

To work with Computer Vision applications with Python, we need to use the OpenCV library in Python. OpenCV (Open Source Computer Vision) is a library containing programming functions primarily intended for solving real-time computer vision problems. It contains both classic and advanced computer vision and machine learning algorithms.

You can easily install this library by writing a pip command; pip install opencv-python. Please note that it is installed by the name of opencv-python but while coding we need to import it as cv2.

Getting Started

Now let’s see how to work on the applications of computer vision by using the Python programming language. I will start this computer vision tutorial by importing the cv2 library that we need for this task:

import cv2

Now let’s start by how to import an image and view it:

img = cv2.imread("aman.jpg")
cv2.imshow("Aman", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Before moving forward, I will first create a helper function to view the output image so that we will not repeat the same code again and again:

def viewImage(img, name_of_window):
    cv2.namedWindow(name_of_window, cv2.WINDOW_NORMAL)
    cv2.imshow(name_of_window, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Now let’s see how to crop an image using Python:

crop = img[10:500, 500:2000]
viewImage(crop, "Cropped Image")
crop image with computer vision

Now let’s have a look at how to resize an image:

scaling = 20
#Change Width and Height
width = int(img.shape[1] * scaling / 100)
height = int(img.shape[0] * scaling / 100)
dim = (width,height)
resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)
viewImage(resized, "Resized Image by 20%")
resize image with Python

Now, let’s see how to convert the image into a grayscale:

gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
viewImage(gray_img, "Grayscale Image")
Grayscale image with computer vision using Python

Advance Computer Vision Tutorial with Python

So these were some easy task using computer vision with Python. The complex task can be explored from below:

  1. Face Detection
  2. Convert Images into Pencil Sketches
  3. Real-Time Face Mask Detection

I hope you liked this article on Computer vision Tutorial with Python. Feel free to ask your valuable questions in the comments section below.

Thecleverprogrammer
Thecleverprogrammer
Articles: 75

Leave a Reply