Skip to content

anandakumar15/Object-Size-Detection-Using-OpenCV

Repository files navigation

Object Detection and Size Measurement

This project enables real-time detection of objects and measurement of their sizes using a camera feed or images. It uses OpenCV for object detection based on Aruco markers and measurement based on a homogeneous background.

Installation

  1. Clone the repository:
  2. https://github.com/anandakumarak/Object-Size-Detection-Using-OpenCV.git
  3. Install dependencies:
  4. pip install opencv-python numpy

Download Aruco Marker

To use Aruco markers as reference points for object size measurement, you can download them from the Aruco Generator website. Once downloaded, print the marker and place it in the view of the camera for measurement.

Demo

How It Works

  • Object Detection: The `HomogeneousBgDetector` class in `object_detector.py` is used to detect objects based on a homogeneous background. It converts images to grayscale, creates masks with adaptive thresholding, finds contours, and filters contours based on area to detect objects. Aruco markers are used as reference points for object size measurement. The markers are detected in the image or camera feed, and their perimeter is used to calculate a pixel to centimeter ratio.

  • Object Size Measurement: For each detected object, the script calculates its width and height in centimeters using the pixel to centimeter ratio. It then draws bounding boxes around the objects with dimensions displayed.
  • Usage

    Live Object Detection (measure_object_size_camera.py)

    1. Run the script:
    2. python measure_object_size_camera.py
    3. Position the camera:
    4. Point the camera towards the objects you want to measure. Ensure the objects are placed on a homogeneous background for accurate measurements.

    5. Use Aruco markers as reference points:
    6. The system uses Aruco markers as reference points for measuring object sizes. These markers should be visible in the camera frame along with the objects.

    7. View measurement results:
    8. The script will display the live camera feed with Aruco markers detected and object sizes measured in centimeters.

    Image-Based Object Detection (measure_object_size.py)

    1. Run the script:
    2. python measure_object_size.py
    3. Provide an image:
    4. Ensure you have an image (`phone_aruco_marker.jpg` in this example) with objects and Aruco markers visible.

    5. Detect objects and measure sizes:
    6. The script will load the image, detect objects, measure their sizes, and display the annotated image with object sizes.

    Sample Output

    Below is an example output image showing object detection and size measurement annotations:

    Output Example

    About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published

    Languages