Python OpenCV Object Detection
Object detection is a technique to locate and identify objects in an image or video. OpenCV (Open Source Computer Vision) is a library of programming functions for real time computer vision applications. It is used for image and video analysis, like facial recognition and detection, license plate reading, photo editing, and more.
Syntax
The basic syntax for object detection in OpenCV using Python is:
import cv2
# Load image and classifier
image = cv2.imread('image.jpg')
classifier = cv2.CascadeClassifier('classifier.xml')
# Convert image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect objects
objects = classifier.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
# Draw rectangles around objects
for (x, y, w, h) in objects:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display image with objects detected
cv2.imshow('Objects Detected', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example
Here's an example of object detection in OpenCV using Python:
import cv2
# Load image and classifier
image = cv2.imread('car.jpg')
classifier = cv2.CascadeClassifier('cars.xml')
# Convert image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect cars
cars = classifier.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
# Draw rectangles around cars
for (x, y, w, h) in cars:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display image with cars detected
cv2.imshow('Cars Detected', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output
The output of the code will be an image with rectangles drawn around the detected objects.
Explanation
The code starts by importing the necessary modules and loading both the image and classifier. The classifier is an XML file that contains the pre-trained model to be used for object detection.
Next, the image is converted to grayscale, which is necessary for object detection algorithms. The detectMultiScale()
function is then called, which takes the grayscale image, a scale factor, and a minimum number of neighbors as arguments. The scale factor determines how much the image size is reduced at each image scale, which can affect the detection accuracy and speed. The minimum neighbors parameter specifies how many neighbors each candidate object rectangle should have to retain it. This parameter helps to remove false positives and increase detection accuracy.
The detectMultiScale()
function returns an array of objects, which are four-valued coordinates representing the location and size of each detected object. Finally, the rectangle()
function is used to draw a rectangle around each detected object, and the resulting image is displayed using imshow()
.
Use
Object detection in OpenCV using Python can be used in a variety of applications, such as surveillance systems, vehicle detection and counting, face detection, and more. It can be used in both real-time and non-real-time scenarios.
Important Points
- OpenCV is a library of programming functions for real time computer vision applications.
- Object detection is a technique to locate and identify objects in an image or video.
- The
detectMultiScale()
function is used to detect objects in an image, taking a grayscale image, scale factor, and minimum neighbors as arguments. - The
rectangle()
function is used to draw a rectangle around each detected object in the original image. - Object detection in OpenCV using Python can be used in a variety of applications.
Summary
Object detection in OpenCV using Python is a technique to locate and identify objects in an image or video. The detectMultiScale()
function is used to detect objects, taking a grayscale image, scale factor, and minimum neighbors as arguments. The rectangle()
function is used to draw a rectangle around each detected object in the original image. Object detection in OpenCV using Python can be used in a variety of applications, such as surveillance systems, vehicle detection and counting, face detection, and more.