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)
  • 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__
    ‘3.4.3’
  • Python Computer Vision — Working with Images


    Now that we’ve successfully installed OpenCV, let’s get started with it.
  • 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.
    • 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)
      True
      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. >>> plt.show()
      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)

Comments