Computer Vision is an exciting field that enables machines to interpret and understand visual data from the world around them. Python is a powerful programming language that has become the go-to choice for computer vision applications due to its simplicity, flexibility and vast number of libraries. In this tutorial, we will explore how to use Python for computer vision.
Setting up a Development Environment
Before we get started, we need to set up our development environment. We will be using Python 3 along with the OpenCV library. OpenCV is an open-source computer vision library that contains a vast array of functions for image and video processing. You can install OpenCV using pip by running:
pip install opencv-python
Reading and Displaying an Image
Our first step is to read and display an image using OpenCV. We can do this using the imread() and imshow() functions respectively. Let’s take a look at an example:
import cv2 # Read an image img = cv2.imread('image.jpg') # Display the image cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
This code reads an image named “image.jpg” and displays it on the screen. The waitKey() function waits until the user presses a key and the destroyAllWindows() function closes all windows.
Image Processing
Now that we know how to read and display an image, let’s move on to some basic image processing operations. We can perform operations such as resizing, rotating, and cropping an image using OpenCV. Here’s an example:
# Resize an image resized_img = cv2.resize(img, (500, 500)) # Rotate an image rows, cols = img.shape[:2] M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1) rotated_img = cv2.warpAffine(img, M, (cols, rows)) # Crop an image cropped_img = img[100:500, 100:500]
The above code resizes the image to 500×500, rotates it by 45 degrees, and crops it. These are just a few examples of the many image processing operations that can be performed using OpenCV.
Object Detection
Object detection is a computer vision technique that involves detecting objects in an image or video. OpenCV contains several functions that can be used for object detection, such as Haar cascades and HOG (Histogram of Oriented Gradients) features. Let’s take a look at an example of object detection using a Haar cascade:
# Load the Haar cascade face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Read an image img = cv2.imread('image.jpg') # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) # Draw rectangles around the faces for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) # Display the image with faces detected cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
This code detects faces in an image using a Haar cascade, draws rectangles around them, and displays the image. We can adjust the scaleFactor and minNeighbors parameters to modify the accuracy of the detection.
Python is a powerful tool for computer vision applications due to its simplicity, flexibility and vast number of libraries. In this tutorial, we explored how to use Python for computer vision, covering topics such as setting up a development environment, reading and displaying images, image processing, and object detection. By utilizing the OpenCV library, we can easily perform complex computer vision tasks in Python.
Want to learn more about Python, checkout the Python Official Documentation for detail.