An In-Depth Introduction to Computer Vision with OpenCV

Computer vision is an exciting field of artificial intelligence that focuses on enabling computers to interpret and understand visual information from the world. With computer vision, we can build systems that can identify objects, detect faces, read text, analyze medical imagery, perceive depth, and much more. The possibilities are endless, and computer vision is powering innovative applications across industries like robotics, self-driving cars, augmented reality, healthcare, retail, and security.

One of the most popular and powerful tools for computer vision is OpenCV. In this article, we‘ll dive deep into OpenCV and learn how to perform a variety of computer vision techniques. By the end, you‘ll have a solid foundation for building your own computer vision applications.

What is OpenCV?

OpenCV (Open Source Computer Vision) is an open-source library for computer vision and machine learning. It provides a comprehensive set of optimized algorithms and functions for image and video processing, object detection, facial recognition, feature extraction, and much more.

OpenCV was originally developed by Intel and released in 1999. Since then, it has become the most widely used computer vision library, with interfaces for C++, Python, Java, and MATLAB/Octave. It‘s fast, robust, and used extensively in both academia and industry.

Some key advantages of OpenCV include:

  • Extensive collection of computer vision and machine learning algorithms
  • Highly optimized C/C++ backend for maximum performance
  • Active community, detailed documentation, and many free learning resources
  • Cross-platform and supports hardware acceleration
  • Liberal open-source license

Installing OpenCV

To get started with OpenCV in Python, you‘ll need to install the opencv-python package. The easiest way is with pip:

pip install opencv-python

This will install both OpenCV and its dependencies, including NumPy.

You can verify OpenCV installed correctly by importing it in the Python REPL:

import cv2 
print(cv2.__version__)

This should print your OpenCV version without any errors.

Basic Image Processing with OpenCV

Let‘s start exploring OpenCV‘s functionality with some basic image processing operations. First, we need to load an image:

import cv2

img = cv2.imread(‘image.jpg‘)

This reads the image file and stores it as a NumPy array in the img variable. If the image is in the same directory as your Python script, you can specify the filename directly. Otherwise, use the full path to the image file.

To display the loaded image, use cv2.imshow():

cv2.imshow(‘Image‘, img)
cv2.waitKey(0) 
cv2.destroyAllWindows()

This will open a window displaying the image. The waitKey(0) line waits for a key press before closing the window. You can save the image to a new file with cv2.imwrite():

cv2.imwrite(‘new_image.jpg‘, img)

Resizing, Cropping, and Rotating

Resizing an image is easily done with cv2.resize():

resized = cv2.resize(img, (200, 200))

This resizes the image to 200×200 pixels. You can also specify the new size as a scaling factor:

resized = cv2.resize(img, None, fx=0.5, fy=0.5)

To crop an image, simply use array slicing:

cropped = img[50:200, 100:400]

This extracts a 150×300 pixel subregion starting at coordinates (100, 50).

To rotate an image, use cv2.rotate():

rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

Color Space Conversion

OpenCV uses the BGR color space by default, but we can easily convert to other spaces like grayscale or HSV:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 

Drawing on Images

OpenCV provides functions to draw shapes and text on images:

cv2.line(img, (0,0), (200,200), (255,0,0), 5)
cv2.rectangle(img, (50,50), (150,150), (0,255,0), -1) 
cv2.circle(img, (100,100), 50, (0,0,255), 2)
cv2.putText(img, ‘Hello OpenCV‘, (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)

This draws a blue line, green filled rectangle, red circle, and white text on the image.

Basic Thresholding and Filtering

Thresholding is an important technique for segmenting images by intensity:

_, thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)

This applies binary thresholding with a threshold of 128. Pixel values above 128 are set to white (255) and those below are set to black (0).

To smooth or blur an image, we can use filtering functions:

blurred = cv2.GaussianBlur(img, (5,5), 0)

This applies a 5×5 Gaussian filter to blur the image.

Image Transformations

OpenCV provides functions for more advanced geometric transformations of images.

Scaling, Translation, and Rotation

To scale, translate, or rotate an image, we first create a transformation matrix and then apply it with cv2.warpAffine():

M = cv2.getRotationMatrix2D((w/2, h/2), 45, 0.5)
rotated = cv2.warpAffine(img, M, (w,h))

This rotates the image by 45 degrees around its center and scales it by 0.5.

Perspective and Affine Transformations

Perspective transformations can correct for perspective distortion:

pts1 = np.float32([[0,0], [300,0], [0,300], [300,300]])
pts2 = np.float32([[0,0], [300,0], [100,300], [200,300]])
M = cv2.getPerspectiveTransform(pts1, pts2)
transformed = cv2.warpPerspective(img, M, (300,300))

This maps a rectangular image onto a trapezoid-shaped image.

Affine transformations can perform scaling, rotation, translation, and shearing simultaneously:

pts1 = np.float32([[0,0], [200,0], [0,200]])
pts2 = np.float32([[0,0], [100,0], [0,300]]) 
M = cv2.getAffineTransform(pts1, pts2)
transformed = cv2.warpAffine(img, M, (200,200))

Edge Detection and Image Gradients

Edge detection is a fundamental technique in computer vision for finding object boundaries. OpenCV provides several edge detection functions:

edges = cv2.Canny(gray, 100, 200)

This applies the Canny edge detector to find edges in a grayscale image. The 100 and 200 parameters are the lower and upper thresholds for edge strength.

We can also compute image gradients directly:

sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(gray, cv.CV_64F, 0, 1, ksize=5)

This computes the x and y gradients using the Sobel operator with a kernel size of 5.

Contour Detection and Shape Analysis

Contours are curves that join continuous points along object boundaries. We can find contours in binary images:

contours, _ = cv2.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 

The contours are returned as a list of (x,y) coordinates. We can draw the detected contours:

cv2.drawContours(img, contours, -1, (0,255,0), 2)

And analyze their shapes:

for contour in contours:
    area = cv2.contourArea(contour)
    perimeter = cv2.arcLength(contour, True)
    _, _, w, h = cv2.boundingRect(contour)
    aspect_ratio = w / float(h)

Image Segmentation

Image segmentation aims to partition an image into meaningful regions. OpenCV provides several segmentation techniques:

# Watershed algorithm
markers = cv2.watershed(img, markers)

# GrabCut
mask = np.zeros(img.shape[:2], np.uint8)
fgModel = np.zeros((1,65), np.float64)  
bgModel = np.zeros((1,65), np.float64)
rect = (50,50,450,290)
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

The watershed algorithm segments an image based on intensity gradients, while GrabCut uses graph cuts to segment foreground objects from the background.

Object Detection

Object detection is the task of localizing and classifying objects in an image. OpenCV provides several pre-trained object detectors:

# Face detection with Haar cascade
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml‘)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)

# Pedestrian detection with HOG + SVM
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
pedestrians, _ = hog.detectMultiScale(img)
for (x,y,w,h) in pedestrians:
    cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)

The Haar cascade classifier is used for real-time face detection, while the HOG (Histogram of Oriented Gradients) detector combined with a linear SVM is used for pedestrian detection.

Feature Detection and Matching

Feature detection aims to find interest points or keypoints in an image that are invariant to scale and rotation. These keypoints can then be matched between images to find correspondences, which is useful for image stitching, 3D reconstruction, object tracking, and more.

Popular feature detectors in OpenCV include:

  • Harris corner detector
  • SIFT (Scale-Invariant Feature Transform)
  • SURF (Speeded Up Robust Features)
  • ORB (Oriented FAST and Rotated BRIEF)

Here‘s an example of detecting and matching ORB features:

orb = cv2.ORB_create()

kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)

img3 = cv.drawMatches(img1, kp1, img2, kp2, matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

This detects ORB keypoints and descriptors in two images, matches them using brute-force matching, and visualizes the matches.

Neural Networks and Deep Learning

In recent years, deep neural networks have achieved state-of-the-art results on computer vision tasks like image classification, object detection, and segmentation.

OpenCV provides a module for using pre-trained deep learning models:

net = cv2.dnn.readNetFromCaffe(‘deploy.prototxt‘, ‘model.caffemodel‘)
blob = cv2.dnn.blobFromImage(img, 1.0, (224,224), (104,117,123)) 
net.setInput(blob)
preds = net.forward()

This loads a pre-trained Caffe model and runs inference to classify an input image. OpenCV supports models trained with various deep learning frameworks including TensorFlow, Torch, and Darknet.

Practical OpenCV Projects

Now that you‘re familiar with the core concepts of computer vision and OpenCV, here are some ideas for practical projects you can build:

  • Document Scanner: Detect and extract documents from an image, correct for perspective, and apply enhancements.
  • Face Swapper: Detect faces in two images and swap them, blending the results seamlessly.
  • Selfie Filters: Detect facial landmarks and overlay fun filters or masks, similar to Snapchat lenses.
  • People Counter: Count the number of people entering and exiting a room using background subtraction or HOG.
  • License Plate Reader: Detect and recognize license plate numbers from car images using edge detection, contours, and OCR.
  • Image Stitching: Stitch together multiple overlapping images into a single panorama using feature matching.
  • Object Tracking: Track the movement of objects across video frames using techniques like optical flow or tracking-by-detection.

Building these projects will help solidify your understanding of computer vision concepts and give you valuable practical experience.

Resources for Learning More

To dive deeper into computer vision and OpenCV, check out these great resources:

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts