In this Python and OpenCV article we are going to talk about Exploring Computer Vision with Python and OpenCV, Computer vision is a field of study that focuses on enabling computers to interpret and understand visual world. with the rise of artificial intelligence and machine learning, computer vision has become on of the important component in many applications from facial recognition to self driving cars. in this article we are going to talk about computer vision with Python and OpenCV. it is one of the most popular computer vision libraries in Python.
So first of all let’s talk about Python OpenCV, OpenCV short for Open Source Computer Vision, it is powerful open source library that provides different tools and algorithms for image processing and computer vision tasks. OpenCV is written in C++ and provides bindings for Python and this makes it accessible and easy to use for developers.
Exploring Computer Vision with Python and OpenCV
First of all we need to install Python OpenCV
1 |
pip install opencv-python |
So now let’s start from reading an image in Python OpenCV, we can use imread() for reading the image and imshow() from showing the image, and waitKey(0) waits for key event to close the window.
1 2 3 4 5 6 7 8 |
import cv2 # Load an image img = cv2.imread('lena.tif') # Display the image cv2.imshow('Image', img) cv2.waitKey(0) |
Run the code this is the result
Let’s talk about image manipulation using Python and OpenCV, OpenCV provides different tools for image manipulation, including resizing, cropping and rotating images. these are some examples:
1 2 3 4 5 6 7 8 9 10 11 |
# Resize an image resized = cv2.resize(img, (500, 500)) # Crop an image cropped = img[100:300, 200:400] # Rotate an image (h, w) = img.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, 45, 1.0) rotated = cv2.warpAffine(img, M, (w, h)) |
Image filtering is a technique that is used to enhance images by removing noise and highlighting important features. OpenCV provides several builtin filters including Gaussian blur and median blur:
1 2 3 4 5 |
# Apply a Gaussian blur filter blurred = cv2.GaussianBlur(img, (5, 5), 0) # Apply a median blur filter blurred = cv2.medianBlur(img, 5) |
Let’s talk about Object Detection in Python OpenCV, Object detection is a common computer vision task that involves detecting objects in an image and drawing bounding boxes around them. OpenCV provides several object detection algorithms, including Haar cascades and HOG+SVM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import cv2 # Load the cascade classifier for face detection face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Load input image img = cv2.imread('lena.tif') # Convert image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect faces in the image faces = face_cascade.detectMultiScale(gray, 1.3, 5) # Draw rectangles around the detected faces for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) # Display the output image cv2.imshow('Output', img) cv2.waitKey(0) cv2.destroyAllWindows() |
In the above code we have loaded Haar cascade classifier for face detection using cv2.CascadeClassifier(). we then load the input image using cv2.imread(), and convert it to grayscale using cv2.cvtColor(). after that we use the detectMultiScale() function to detect faces in the image, which returns the bounding boxes of the detected faces.
and finally we draw rectangles around the detected faces using cv2.rectangle(), and display the output image using cv2.imshow(). we have used cv2.waitKey() to wait for key event and cv2.destroyAllWindows() to close all open windows.
Run the code and this is the result