yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
IMX 415 Camera Module📖
1. Jetson Orin Nano‑NX Usage Course
  • 1. Camera Driver Course
  • 2.1 Advanced AI Vision-Traffic Detection—DeepStream environment
  • 2.2 Advanced AI Vision-Traffic Detection—Four types of detection(traffic)
  • 3. YOLO11 Object Detection
  • 4. YOLO26 Object Detection
  • 5. Mediapipe Environment
  • 6. Mediapipe Hand Detection
  • 7. MediaPipe Face Detection
  • 8. MediaPipe Face Effects
  • 9. MediaPipe 3D Object Recognition
  • 10. MediaPipe Virtual Brush
  • 11. MediaPipe Gesture Recognition
  • 12. MediaPipe Finger Control
  • 13. MediaPipe Posture Estimation
  • 14. MediaPipe Face Mask
  • 15. MediaPipe Overall Detection
  • 16. MediaPipe Green Scene Rremoval(Human)
  • 17. QR code recognition
2. Raspberry Pi 5 Usage Course
  • 1. Camera Driver Course
  • 2. Advanced AI Vision-Human Pose Estimation
  • 3. Target Detection
  • 4. QR Code
  • 5. Face Detection
  • 6. Color Recognition
  • 7. YOLO11 Object Detection
  • 8. YOLO26 Object Detection
  • 9. MediaPipe Face Effects
  • 10. MediaPipe Face Detection
  • 11. MediaPipe 3D Object Recognition
  • 12. MediaPipe Virtual Brush
  • 13. MediaPipe Finger Control
  • 14. MediaPipe Gesture Recognition
3. RDK X5 Usage Course
  • 1. Camera Driver Course
  • 2. Target Detection Algorithm-FCOS
  • 3. Target Detection Algorithm-YOLOv5s
  • 4. YOLO26 Object Detection
4. Camera Bracket Installation (Optional Video)
  • Installation Video
Download
  • Code
  • SystemFile
  • 3D_Model_File
  • Hardware_Info
Download ZIP

Welcome to IMX 415 Camera Module repository

6. Color Recognition

7. Color Recognition

1. Introduction to the HSV color space:

The RGB color space is the color space everyone is most familiar with, that is, the three primary color space. Any color can be formed by mixing these three colors. However, generally the effective processing of images in a color space is carried out in the HSV space. HSV (Hue, Saturation, Value) is a color space created based on the intuitive characteristics of color, also known as the hexcone model.

Why choose the HSV space instead of the RGB space? For images, identifying the corresponding color is feasible in the RGB space, the HSV space, or other color spaces. The reason HSV is chosen is that the hue represented by H can basically determine a certain color, and then combined with the saturation and value information, it is judged to be greater than a certain threshold. RGB, on the other hand, is composed of three components and requires judging the contribution ratio of each component. That is, the recognition range of the HSV space is wider and more convenient.

img

HSV color space model

2. Conversion of three color spaces (gray BGR HSV):

There are more than 150 methods for color space conversion in OpenCV, but there are only two that we commonly use, namely BGR->Gray and BGR->HSV. Note that Gray and HSV cannot be converted to each other. Color space conversion: cv2.cvtColor(input_image, flag)

BGR->Gray: the flag is cv2.COLOR_BGR2GRAY

BGR->HSV: the flag is cv2.COLOR_BGR2HSV

The value range of the HSV color space in OpenCV:

H [0, 179] S [0, 255] V [0, 255]

img

Value ranges of common colors

According to the above ranges, our goal is to identify whether it is our set color range, and first replace this color with white, replace non-this-color with black, and finally replace the white part with the original image.

Code path: /home/pi/Yahboom_Project/1.OpenCV course/04 advanced tutorial/Color_recognition_USB.ipynb

xxxxxxxxxx
​
​
#bgr8 to jpeg format
​
​
import enum
​
​
import cv2
​
​
  
​
​
​
def bgr8_to_jpeg(value, quality=75):
​
​
    return bytes(cv2.imencode('.jpg', value)[1])
#Camera component display
​
​
import traitlets
​
​
import ipywidgets.widgets as widgets
​
​
import time
​
​
# Thread function operation library
​
​
import threading
​
​
import inspect
​
​
import ctypes
​
​
  
​
​
​
origin_widget = widgets.Image(format='jpeg', width=320, height=240)
​
​
mask_widget = widgets.Image(format='jpeg',width=320, height=240)
​
​
result_widget = widgets.Image(format='jpeg',width=320, height=240)
​
​
  
​
​
​
# create a horizontal box container to place the image widget next to eachother
​
​
image_container = widgets.HBox([origin_widget, mask_widget, result_widget])
​
​
  
display(image_container)
#Thread related functions
​
​
def _async_raise(tid, exctype):
​
​
    """raises the exception, performs cleanup if needed"""
​
​
    tid = ctypes.c_long(tid)
​
​
    if not inspect.isclass(exctype):
​
​
        exctype = type(exctype)
​
​
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
​
​
    if res == 0:
​
​
        raise ValueError("invalid thread id")
​
​
    elif res != 1:
​
​
        # """if it returns a number greater than one, you're in trouble,
​
​
        # and you should call it again with exc=NULL to revert the effect"""
​
​
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)      
​
​
def stop_thread(thread):
​
​
    _async_raise(thread.ident, SystemExit)
#Main process function
​
​
import cv2
​
​
import numpy as np
​
​
import ipywidgets.widgets as widgets
​
​
  
​
​
  
​
​
cap = cv2.VideoCapture(0)
​
​
cap.set(3, 640)
​
​
cap.set(4, 480)
​
​
cap.set(5, 120)  #Set frame rate
​
​
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
​
​
# image.set(cv2.CAP_PROP_BRIGHTNESS, 40) #Set brightness -64 - 64  0.0
​
​
# image.set(cv2.CAP_PROP_CONTRAST, 50)   #Set contrast -64 - 64  2.0
​
​
# image.set(cv2.CAP_PROP_EXPOSURE, 156)  #Set exposure value 1.0 - 5000  156.0
​
​
  
​
​
​
  
​
​
#Red is selected by default. To recognize other colors, comment out the red range code below and uncomment the other color range code sections.
​
​
# Red range
​
​
color_lower = np.array([0, 43, 46])
​
​
color_upper = np.array([10, 255, 255])
​
​
  
​
​
# #Green range
​
​
# color_lower = np.array([35, 43, 46])
​
​
# color_upper = np.array([77, 255, 255])
​
​
  
​
​
# #Blue range
​
​
# color_lower=np.array([100, 43, 46])
​
​
# color_upper = np.array([124, 255, 255])
​
​
  
​
​
# #Yellow range
​
​
# color_lower = np.array([26, 43, 46])
​
​
# color_upper = np.array([34, 255, 255])
​
​
  
​
​
# #Orange range
​
​
# color_lower = np.array([11, 43, 46])
​
​
# color_upper = np.array([25, 255, 255])
​
​
  
​
​
  
​
​
def Color_Recongnize():
​
​
    
​
​
    while(1):
​
​
        # get a frame and show  Get the video frame and convert it to HSV format, use cvtColor() to convert the BGR format to HSV format, with the parameter cv2.COLOR_BGR2HSV.
​
​
        ret, frame = cap.read()
​
​
        #cv2.imshow('Capture', frame)
​
​
        origin_widget.value = bgr8_to_jpeg(frame)
​
​
  
​
​
        # change to hsv model
​
​
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
​
​
  
​
​
        # get mask  Use the inRange() function and the upper and lower bounds of the blue range in the HSV model to obtain the mask. In the mask, the blue part of the original video will be turned white, and the other parts black.
​
​
        mask = cv2.inRange(hsv, color_lower, color_upper)
​
​
        #cv2.imshow('Mask', mask)
​
​
        mask_widget.value = bgr8_to_jpeg(mask)
​
​
  
​
​
        # detect blue  Perform a bitwise AND operation between the mask and the original video frame, which will replace the white in the mask with the real image:
​
​
        res = cv2.bitwise_and(frame, frame, mask=mask)
​
​
        #cv2.imshow('Result', res)
​
​
        result_widget.value = bgr8_to_jpeg(res)
​
​
  
​
​
        #     if cv2.waitKey(1) & 0xFF == ord('q'):
​
​
        #         break
​
​
        time.sleep(0.01)
​
​
    cap.release()
​
​
    #cv2.destroyAllWindows()
#Start the process
​
​
thread1 = threading.Thread(target=Color_Recongnize)
​
​
thread1.setDaemon(True)
​
​
thread1.start()
#End the process, this code only needs to be executed when ending
​
​
stop_thread(thread1)

img

This is the effect of performing red recognition. If you want to recognize other colors, you can comment out the red range and uncomment other color ranges.

For the CSI camera, the code path to execute is /home/pi/Yahboom_Project/1.OpenCV course/04 advanced tutorial/Color_recognition_CSI.ipynb

Note: For the CSI camera, if you want to re-run the program, you need to do the following:

img

Face Detection
YOLO11 Object Detection

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文