Brainy Pi

Available to select audience (currently beta)

Color detection on Brainy Pi
Color detection is an important problem in computer vision, and it has numerous applications, such as object tracking, image segmentation, and robotics. In this article, we will learn how to do Color Detection using OpenCV on Brainy Pi , a single-board computer.
OpenCV is an open-source computer vision library that provides a variety of tools for image and video processing, such as reading, writing, and manipulating images and videos. It is a widely used library in the computer vision community due to its efficiency, simplicity, and versatility.
BrainyPi, an innovative platform that allows developers to create intelligent systems with ease.

A little about source code

The source code contains 2 python files
  1. colorDetection.py – file is the main file of the program. When this file runs it opens up the camera and then tries to match the color according to the filter.
  2. colorDetectUtils.py – file is where the actual color detection happens. colorDetection.py file calls the methods inside this file and then we pass the image through the detectColors function. Finally we pass the output to the colorDetection.py file and then we display the output on the screen.

Running the Code for Color Detection using OpenCV on Brainy Pi

To run the color detection program on BrainyPi, follow these steps:
  1. Connect USB webcam to BrainyPi.
  2. Clone the GitHub Repository
    git clone https://github.com/brainypi/brainypi-opencv-examples.git 
  3. Install Required Dependencies
    cd color-detection
    sudo apt install python3-dev cmake
    pip install -r requirements.txt
  4. Run Color detection
    python colorDetection.py
    The program will open up the camera and try to match the color in the camera to the color filter. The output will be displayed on the screen. To exit the program, press the q key.

Adapting the Code for Your Own Use

If you want to use this color detection code for your own project, you’ll need to make some changes to customize it for your specific use case. Here are some tips for adapting the code:
  1. createColorMask() – Create color mask to detect the specified color.
    • Inputs
      1. inputImage ([opencv Mat]): Input image
      2. colorLower ([opencv Mat]): Lower range of the color
      3. colorUpper ([opencv Mat]): Upper range of the color
    • Outputs:
      1. color_mask []: Contains the color mask
  2. detectColors() – Detect color in image
    • Inputs
      1. inputImage ([opencv Mat]): Input image
      2. colorMask ([opencv Mat]): Color Mask generated from createColorMask()
      3. name ([opencv Mat]): Name of the color
    • Outputs
      1. colorInfo [list]: list of colors detected in the image with bounding box info
      2. outputImage [opencv Mat]: Output Image

Different Color Detection using OpenCV on Brainy Pi

The current code is set to detect Red color in the image. If you want to change the color then you have to change the the upper limit and lower limit in the colorDetection.py file.
colorLowerLimit = np.array([136, 187, 111], np.uint8)
colorUpperLimit = np.array([180, 255, 255], np.uint8)

gyuw4

To change the color, first get the upper and lower H & S points in the above color chart.
For example: Let’s say we want to detect color blue, then
  1. Create a rectangle in the color chart, marking all the blue colors that you want to detect.

    gyuw45

  2. Note down the lower and upper values of H and S
    • H_lower = 90
    • H_upper = 130
    • S_lower = 50
    • S_upper = 255
  3. We will keep the V values as constant
    • V_lower = 20
    • V_upper = 255
  4. Now we can change the upper and lower limit, in the colorDetection.py file.
    colorLowerLimit = np.array([90, 50, 20], np.uint8)
    colorUpperLimit = np.array([130, 255, 255], np.uint8)
  5. Now your code should detect the Blue color instead of Red.

Conclusion

In conclusion, color detection is a crucial task in computer vision that has numerous applications, such as object tracking, image segmentation, and robotics. OpenCV, an open-source computer vision library, provides a powerful set of tools for image and video processing, making it a widely used library in the computer vision community. The article explained how to detect colors using OpenCV on Raspberry Pi and BrainyPi, providing step-by-step instructions and tips for adapting the code to different use cases. By following the article, anyone can easily learn how to detect colors in images or videos using OpenCV on BrainyPi.
0 Comments

Leave a reply

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

*