OpenCV Project – LEGO Brick Finder

Free OpenCV course with real-time projects Start Now!!

The LEGO brick finder is an innovative computer vision application designed to identify and locate LEGO bricks within images or real-world scenes. Using advanced color-based detection and image processing techniques, it can distinguish various LEGO brick colors and shapes, aiding in the quick and efficient detection of LEGO pieces in diverse contexts. Whether for sorting, building, or inventory management, this tool simplifies the task of pinpointing LEGO bricks of interest.

Prerequisites For OpenCV Lego Brick Finder

To utilize the LEGO brick finder, a solid grasp of Python and OpenCV is essential, along with the following system requirements.

1. Python 3.7 and above
2. Any Python editor (VS code, Pycharm, etc.)

Download OpenCV Lego Brick Finder Project

Please download the source code of OpenCV Lego Brick Finder Project from the following link: OpenCV Lego Brick Finder Project Code.

Installation

Open windows cmd as administrator

1. Install OpenCV library.

pip install opencv-python

Let’s Implement It

1. Import all the necessary packages

import cv2
import numpy as np

2. It reads an image and resizes it to specified width. Next, It converts it to the HSV color space for further image processing and analysis.

img = cv2.imread('img.jpg')
width = 600
scale = width / img.shape[1]
img = cv2.resize(img, None, fx=scale, fy=scale)
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

3. It defines HSV color ranges for different colored LEGO brick detection.

range_color = {
    'red': [(0, 100, 100), (10, 255, 255)],
    'dark_green': [(30, 80, 80), (75, 255, 255)], 
    'yellow': [(20, 100, 100), (30, 255, 255)],
    'blue': [(90, 100, 100), (140, 255, 255)], 
}

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

4. It creates an empty mask and for each defined color range, it generates masks based on HSV values, combining them into ‘mask_comb’.

mask_comb = np.zeros(img.shape[:2], dtype=np.uint8)

for color, (lower, upper) in range_color.items():
    mask = cv2.inRange(hsv_img, np.array(lower), np.array(upper))
    mask_comb |= mask

5. It finds contours in the combined mask, filtering them based on a minimum area of 500 pixels.

contours, _ = cv2.findContours(mask_comb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
min_area = 500
contour_filter = [c for c in contours if cv2.contourArea(c) > min_area]

6. It iterates through the filtered contours and calculates the bounding rectangles. Next, it draws a green rectangle around the detected LEGO bricks from the image.

for contour in contour_filter:
    x, y, w, h = cv2.boundingRect(contour)
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

7. It displays the modified image with the detected LEGO bricks using OpenCV. It waits for a key press and then closes the image window.

cv2.imshow('DataFlair', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV LEGO Brick Finder Output

opencv lego brick finder

opencv lego brick finder output

lego brick finder

lego brick finder opencv output

Conclusion

In conclusion, the Lego Brick Finder project has demonstrated the successful integration of technology into the realm of play and creativity. This project combines computer vision and machine learning to effortlessly identify and locate specific Lego bricks within vast collections.

This innovative solution streamlines the building process, making it more accessible and enjoyable for enthusiasts of all ages and reaffirming the timeless appeal of Lego in the modern era.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *