Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Haar Cascades for Object Detection - Python
Next article icon

Object detection refers to identifying and locating objects within images or videos.OpenCV provides a simple way to implement object detection using Haar Cascades a classifier trained to detect objects based on positive and negative images. In this article we will focus on detecting objects using it which is simple and effective for real-time object detection. But before that lets understand what is haar cascades.

Understanding Haar Cascades

Haar Cascade classifiers are an effective tool for object detection based on Haar features. The key idea is to detect objects in images at multiple scales using a cascade of classifiers. It was originally introduced by Paul Viola and Michael Jones in their paper.

  • Positive Images: These are images that contain the object to be detected like a stop sign.
  • Negative Images: These are images that do not contain the object and are used to train the classifier to learn what to ignore.

Haar Cascades are particularly well-suited for real-time object detection making them a popular choice for detecting faces, vehicles and stop signs.

Implementation of Object Detection with Haar Cascades

Here is the step by step implementation of object detection using OpenCV. For this you can download the Haar Cascade XML file for object detection and the sample image fromhere. Place them in the same directory as your Python script.

1. Loading the Image

The first step in object detection is to load the image in which you want to detect objects.

  • cv2.imread(): Reads the image.
Python
importcv2frommatplotlibimportpyplotaspltimg=cv2.imread("image.jpg")

2. Converting Image Color Formats

OpenCV reads images in BGR format by default but we need the image in RGB format for better visualization with Matplotlib. Additionally for object detection we need a grayscale version of the image.

  • cv2.cvtColor(): Converts the image from one color space to another (BGR to RGB and BGR to grayscale).
  • cv2.COLOR_BGR2GRAY: Converts an image from BGR (Blue, Green, Red) color space to grayscale.
  • cv2.COLOR_BGR2RGB: Converts an image from BGR (Blue, Green, Red) color space to RGB (Red, Green, Blue) color space.
  • plt.imshow(): Displays the image in a Matplotlib window.
  • plt.show(): Renders the image.
Python
img_rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)plt.imshow(img_rgb)plt.show()

Output:

detect-object-python-opencv

3. Loading Haar Cascade Classifier

Next you need to load the Haar Cascade classifier. The classifier is trained to detect specific objects such as stop signs in this case. Ensure that the XML file is present in your project directory.

  • cv2.CascadeClassifier(): Loads the Haar Cascade XML file that contains the trained classifier.
Python
stop_cascade=cv2.CascadeClassifier('stop_data.xml')

4. Detecting Objects in the Image

  • detectMultiScale(): detect objects in the image. This function performs detection at multiple scales which helps detect objects of varying sizes in grayscale.
  • minSize=(20, 20): Defines the minimum size of the object to be detected. Smaller objects are ignored.
Python
found=stop_cascade.detectMultiScale(img_gray,minSize=(20,20))

5. Drawing Rectangles Around Detected Objects

Once objects are detected we draw a green rectangle around each detected object. This helps visualize the detection result.

  • cv2.rectangle(): Draws a rectangle on the image at the coordinates(x, y) with a widthw and heighth. The color is green(0, 255, 0) and the rectangle has a thickness of 5 pixels.
Python
for(x,y,w,h)infound:cv2.rectangle(img_rgb,(x,y),(x+w,y+h),(0,255,0),5)

6. Displaying the Result

Finally, the processed image is displayed using Matplotlib, showing the detected objects with rectangles.

Python
plt.imshow(img_rgb)plt.show()

Output :

python-opencv-detect-image

In this article we explored how to perform object detection using OpenCV-Python with Haar Cascades. Haar Cascades are an efficient and fast method for detecting objects in real-time making them useful for various applications such as traffic sign detection, face detection and many more. You can easily apply object detection to your own projects and further explore advanced techniques such as training your own Haar Cascade classifiers for different objects.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp