Using OpenCV with Python for Computer Vision. (Face Detection, Edge Detection & More)

Using OpenCV with Python for Computer Vision. (Face Detection, Edge Detection & More)

1*2lq4cq9GEEJcWmgbXdHEWA Using OpenCV with Python for Computer Vision. (Face Detection, Edge Detection & More)

In this tutorial, I will go over the basics of using OpenCV with Python for image and video processing.

I’ll cover how to install OpenCV (it’s easier than teaching your grandparents how to use Facebook), import it into Python, read and display images and videos, and perform tasks such as grayscale conversion, edge detection, and face detection (Real Secret Agent Type Stuff).

With OpenCV, the possibilities for image and video processing are endless!

Installing OpenCV

Before we get started, you need to install OpenCV on your machine. There are several ways to do this, but the easiest way is to use pip. Open a terminal and run the following command:

pip install opencv-python

This will install the latest version of OpenCV on your machine.

Importing OpenCV

Once you have installed OpenCV, you can import it into your Python code using the following command:

import cv2

Reading and displaying images

To read an image using OpenCV, you can use the cv2.imread() function. This function takes the filename of the image as an argument and returns a NumPy array representing the image. Here’s an example:

import cv2
# Load an image using cv2.imread()
img = cv2.imread('image.jpg')
# Display the image using cv2.imshow()
cv2.imshow('image', img)
# Wait for a key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we load an image called image.jpg using cv2.imread(). We then display the image using cv2.imshow(), which opens a window showing the image. Finally, we use cv2.waitKey(0) to wait for a key press, and cv2.destroyAllWindows() to close the window.

Reading and displaying videos

Reading and displaying videos is similar to reading and displaying images. To read a video, you can use the cv2.VideoCapture() function. This function takes the filename of the video as an argument and returns a VideoCapture object. You can then use the read() method of the VideoCapture object to read frames from the video.

import cv2
# Load a video using cv2.VideoCapture()
cap = cv2.VideoCapture('video.mp4')
# Loop over frames from the video
while True:
    # Read a frame from the video
    ret, frame = cap.read()    
    # Display the frame using cv2.imshow()
    cv2.imshow('frame', frame)    
    # Check if the user pressed the 'q' key
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# Release the VideoCapture object and close the window
cap.release()
cv2.destroyAllWindows()

In this example, we load a video called video.mp4 using cv2.VideoCapture(). We then loop over frames from the video using a while loop. Inside the loop, we read a frame from the video using the read() method of the VideoCapture object. We display the frame using cv2.imshow(), and we check if the user pressed the ‘q’ key using cv2.waitKey(). If the user presses ‘q’, we break out of the loop. Finally, we release the VideoCapture object and close the window.

Image and video processing

OpenCV provides a wide range of image and video processing functions. Here are a few examples:

Grayscale conversion

import cv2# Load an image using cv2.imread()
img = cv2.imread('image.jpg')# Convert the image to grayscale using cv2.cvtColor()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Display the grayscale image using cv2.imshow()
cv2.imshow('gray', gray)# Wait for a key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()

Edge detection

import cv2
# Load an image using cv2.imread()
img = cv2.imread('image.jpg')
# Convert the image to grayscale using cv2.cvtColor()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect edges using cv2.Canny()
edges = cv2.Canny(gray, 100, 200)
# Display the edges using cv2.imshow()
cv2.imshow('edges', edges)# Wait for a key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we load an image called image.jpg using cv2.imread(). We convert the image to grayscale using cv2.cvtColor(), and then detect edges using cv2.Canny(). The cv2.Canny() function takes three arguments: the input image, a threshold for the lower bound of the edges, and a threshold for the upper bound of the edges. We then display the edges using cv2.imshow().

Face detection

import cv2
# Load a pre-trained face detection classifier using cv2.CascadeClassifier()
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Load an image using cv2.imread()
img = cv2.imread('image.jpg')
# Convert the image to grayscale using cv2.cvtColor()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces using cv2.CascadeClassifier.detectMultiScale()
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# Draw rectangles around the faces using cv2.rectangle()
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the image with the faces detected using cv2.imshow()
cv2.imshow('image', img)
# Wait for a key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we load a pre-trained face detection classifier using cv2.CascadeClassifier(). We then load an image called image.jpg using cv2.imread(), convert it to grayscale using cv2.cvtColor(), and detect faces using cv2.CascadeClassifier.detectMultiScale(). The cv2.CascadeClassifier.detectMultiScale() function takes three arguments: the input image, a scale factor, and a minimum number of neighboring rectangles that need to be present for a rectangle to be accepted as a face. We then draw rectangles around the faces using cv2.rectangle(), and display the image with the faces detected using cv2.imshow().

Pretty cool, right? This can be considered a nice introduction to OpenCV. But understand that OpenCV provides many more functions for image and video processing, so be sure to check out the official documentation for more information.

Founder & CEO

Lyron Foster is a Prolific Multinational Serial Entrepreneur, Blogger, Author, IT Trainer, Polyglot Coder, Real Estate Investor and Technologist.

Write a comment