AI — Python Computer Vision Tutorial with OpenCV

Rinu Gour
Rinu Gour
Oct 6, 2018 · 7 min read
AI — Python Computer Vision Tutorial with OpenCV

What is Computer Vision in Python?

Computer Vision is a field of multiple disciplines that care about how computers can gain a high-level understanding from digital images/videos. This is an attempt to automate tasks that the human visual system is able to perform. This is a process of acquiring, processing, analyzing, and understanding digital images, and extracting high-dimensional data from the real world (to produce numerical/symbolic information.)

Typical tasks involved Python Computer Vision are:

  • Recognition
  • Motion Analysis
  • Scene Reconstruction
  • Image Restoration

Fields related to Python Computer Vision:

  • Artificial Intelligence
  • Solid-state Physics
  • Neurobiology
  • Signal Processing
  • Statistics, Optimization, Geometry

Some of the applications of Python Computer Vision:

  • Automatic inspection in manufacturing applications
  • Assisting humans in identification tasks (eg, species identification system)
  • Controlling processes (eg, an industrial robot)
  • Detecting events (eg, visual surveillance)
  • Interaction (eg, input to the device for computer-human interaction)
  • Modeling objects/ environments (eg, medical image analysis)
  • Navigation (eg, autonomous vehicle)
  • Organizing information (eg, indexing databases of images and image sequences)

OpenCV Python Computer Vision

Gary Bradsky started OpenCV at Intel in 1999. While it supports a gamut of languages like C++, Python, and more, and OpenCV-Python is an API for OpenCV to unleash the power of Python and the OpenCV C++ API at once.

For Python, this is a library of bindings with the aim to solve computer vision problems. This library uses NumPy and all its array structures convert to and from NumPy arrays. This also means we can integrate it easily with other libraries like SciPy and Matplotlib (these make use of NumPy).

a. Installing OpenCV in Python

Before you can install OpenCV, make sure you have Python and NumPy installed on your machine.

You can download the wheel for OpenCV here (unofficially), so you don’t run into some DLL Hell:

Then, you can install this file using pip:

pip install [path_of_wheel_file]

b. Importing OpenCV in Python

Get to the IDLE and import OpenCV:

  1. >>> import cv2

You can also check which version you have:

  1. >>> cv2.__version__


Python Computer Vision — Working with Images

Now that we’ve successfully installed OpenCV, let’s get started with it.

Python Computer Vision — Working with Images

a. Reading Images in Python

To read an image, we have the built-in function/method imread().

  1. >>> img=cv2.imread(‘py.jpg’)

Note that prior to this, we have moved to the directory that holds this image.

We can also pass a value for a flag, which is the second argument-

  • cv2.IMREAD_COLOR- To load a color image neglecting existing transparency (default flag)
  • cv2.IMREAD_GRAYSCALE- To load a grayscale image
  • cv2.IMREAD_UNCHANGED- To load an image including an alpha channel

We can pass integers 1, 0, or -1.

  1. >>> img=cv2.imread(‘py.jpg’,0)

If you pass an incorrect image path, this gives us no error, but print(img) gives us None.

Let’s revise Python Data Structures

b. Displaying Images in Python

The function/method cv2.imshow() lets us display an image in a window which fits itself to the size of the image. The first argument is the window name- a string; the second is the image.

  1. >>> img=cv2.imread(‘py.jpg’)
  2. >>> cv2.imshow(‘Python’,img)

How about we display this in grayscale?

Python Computer Vision — Displaying Images in Python

Notice that it let us have two windows at once because we didn’t try to name them the same thing.

Working in scripts, a call to waitKey(0) is beneficial. This is a keyboard-binding function/method with time in milliseconds. This function waits for certain milliseconds for a keyboard event, within which, if we press any key, the program continues. When we pass 0, we make it wait indefinitely for a keystroke. We can also make it wait for specific keys.

cv2.destroyAllWindows() is another function/method to destroy all windows we created. cv2.destroyWindow() destroys a specific window.

c. Writing Images in Python

For this, we have the function/method cv2.imwrite(). The first argument is the file name and the second is the image to save.

  1. >>> cv2.imwrite(‘pygray.png’,img)


This saves the image in grayscale with the name ‘pygray.png’ in the current directory. This image is in the PNG format.

Python Images

d. Displaying Images With Matplotlib

We can display this image using Matplotlib.

  1. >>> import matplotlib.pyplot as plt
  2. >>> plt.imshow(img,cmap=’gray’,interpolation=’bicubic’)

<matplotlib.image.AxesImage object at 0x0584C630>

  1. >>> plt.xticks([]),plt.yticks([])

(([], <a list of 0 Text xticklabel objects>), ([], <a list of 0 Text yticklabel objects>))

  1. >>>
Python Computer Vision — Displaying Images with Matplotlib

Drawing with OpenCV Python Computer Vision

a. Drawing Lines in Python

Can we even draw with Python? Let’s begin with a simple line. This takes the starting and ending coordinates.

  1. >>> import numpy as np
  2. >>> img=np.zeros((512,512,3),np.uint8)
  3. >>> cv2.line(img,(0,0),(511,511),(255,0,0),5)
Drawing Lines in Python

b. Drawing Rectangles in Python

We can draw a rectangle with the rectangle() method/function. This takes the top-left corner and bottom-right corner of the rectangle.

Have a look at Python Heatmap

  1. >>> cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
Drawing Rectangles in Python

c. Drawing Circles in Python

We have the circle() method/function for this. This takes the coordinates and the radius.

Let’s discuss Python Function Arguments

  1. >>>,(447,63),63,(0,0,255),-1)
Drawing Circles in Python

d. Drawing Ellipses in Python

We can pass multiple arguments to the ellipse() method/function- center location (x,y), axes lengths, angle, startAngle, and endAngle.

  1. >>> cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
  2. >>> cv2.imshow(‘image’,img)
Drawing Ellipses in Python.

e. Drawing Polygon in Python

The polylines() method/function takes the coordinates of vertices.

Let’s revise the Python Directory

  1. >>> pts=np.array([[10,5],[50,70],[250,360],[400,400]],np.int32)
  2. >>> pts=pts.reshape((-1,1,2))
  3. >>> cv2.polylines(img,[pts],True,(0,255,255))
Drawing Polygon in Python

f. Putting Text in Images

Finally, let’s see how we can add text to this image.

  1. >>> font=cv2.FONT_HERSHEY_SIMPLEX
  2. >>> cv2.putText(img,’OpenCV’,(10,500),font,4,(255,255,23),2,cv2.LINE_AA)
  3. >>> cv2.imshow(‘image’,img)
Putting Text in Images

Basic Operations on Images

Let’s demonstrate this through one code.

Have a look at Python OrderedDict

  1. >>> import cv2
  2. >>> img=cv2.imread(‘py.jpg’)
  3. >>> y,x=100,50
  4. >>> (b,g,r)=img[y,x] #Reading color values at y, x positions
  5. >>> b,g,r #Printing color values to screen

(35, 10, 0)

  1. >>> img[y,x]=(0,0,255) #Setting pixel color to red; BGR scheme
  2. >>> region_of_interest=img[y:y+50,x:x+50] #Region of interest at (x,y) of dimensions 50x50
  3. >>> cv2.imshow(‘image’,img)
Python Computer Vision — Basic Operations on Images
  1. >>> cv2.imshow(‘ROI’,region_of_interest)
  1. >>> region_of_interest[:,:]=(66,22,78) #Setting pixels to new color
  2. >>> cv2.imshow(‘New Image’,img)
Python Computer Vision — Basic Operations

Do you know about Python Collections

Detecting Edges in Python Computer Vision

Edges of objects are how we remember them. We can choose to display only object edges in OpenCV with the function/method Canny().

  1. >>> import numpy as np
  2. >>> img=cv2.imread(‘py.jpg’)
  3. >>> cv2.imwrite(‘edges_py.jpg’,cv2.Canny(img,602,315))


  1. >>> cv2.imshow(‘edges’,cv2.imread(‘edges_py.jpg’))
Detecting Edges in Python Computer Vision

Detecting Faces in Python Computer Vision

Let’s come to the fun part! OpenCV will also let us detect faces in pictures! Let’s take a sample image. (Can you guess who that is?)

Have a look at Python Sequences

Detecting Faces in Python Computer Vision

Now, we will use the Haar cascade classifier to do this. We need the haarcascade_frontalface_default.xml file for this; you can search for this on your machine.

  1. >>> import numpy as np
  2. >>> img=cv2.imread(‘py.jpg’)
  3. >>> cv2.imwrite(‘edges_py.jpg’,cv2.Canny(img,602,315))


Let’s revise Pythpn Regular Expressions

  1. >>> cv2.imshow(‘edges’,cv2.imread(‘edges_py.jpg’))
  2. >>> import numpy as np
  3. >>> fd=cv2.CascadeClassifier(‘C:\\Users\\Ayushi\\Downloads\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_frontalface_default.xml’)
  4. >>> img=cv2.imread(‘mel.jpg’)
  5. >>> gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Converting it to grayscale
  6. >>> faces=fd.detectMultiScale(gray,1.3,5) #Performing the detection
  7. >>> for (x,y,w,h) in faces:
  8. img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3)
  9. >>> cv2.imwrite(‘face_mel.jpg’,img)


Detecting Faces in Python Computer Vision

You can see that this drew a blue square around the face in the picture. Now, let’s try detecting her eyes.

Eye Detection in Python Computer Vision

Now, one last thing we’d like to discuss is detecting eyes. We use the Haar classifier for this too.

You must read Python Web Framework

  1. >>> eye_cascade=cv2.CascadeClassifier(‘C:\\Users\\Ayushi\\Downloads\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_eye.xml’)
  2. >>> img=cv2.imread(‘mel.jpg’)
  3. >>> gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  4. >>> eyes=eye_cascade.detectMultiScale(gray,1.03,5)
  5. >>> for (ex,ey,ew,eh) in eyes:
  6. img=cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
  7. >>> cv2.imwrite(‘mel_eyes.jpg’,img)


Eye Detection in Python Computer Vision

Here, you can see that it detected three eyes! one of which is her lips. Anyway, this is accurate many times, we happened to stumble upon one of the pictures that make the exception. Tell us in the comments below if this has happened to you.

So, this was all in Python Computer Vision Tutorial. Hope you like our explanation.

Conclusion — Python Computer Vision

Hence, in this Python Computer Vision tutorial, we discussed the meaning of Computer Vision in Python AI. Also, we saw drawing with OpenCV, Detecting Edges, and Faces. Moreover, we learned eye detection in Computer Vision Python. Is this explanation helpful to you? Give your feedback in the comments.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store