SoFunction
Updated on 2024-11-19

Python-opencv implementation of red and green color recognition operations

1. Color space conversion (RGB to HSV)

To tune the red and green HSVs more accurately, I created six scrollbars using the () function

#Create HSV minimum scroll bar
('H_min','image',35,180,nothing)
('S_min','image',43,255,nothing)
('V_min','image',46,255,nothing)

#Create HSV highest scroll bar
('H_max','image',0,180,nothing)
('S_max','image',255,255,nothing)
('V_max','image',255,255,nothing)

The actual effect is shown in the picture

2. Identify colors and draw rectangular boxes

The color threshold has been determined, which allows for color recognition.

To make the recognition more stable, adaptive thresholds are added to the code.

th_img = (mask,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,5,2)

3. Drawing rectangular boxes

Use the function () to detect object wheel frames

Then use the function () to find the smallest rectangular box

Use the function () to draw the

contours_green,hierarchy = (th_green,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours_red,hierarchy = (th_red,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for red in contours_red:
 x_red,y_red,w_red,h_red = (red)
 if w_red>width|h_red>height:
 (img,(x_red,y_red),((x_red+h_red),(y_red+w_red)),(0,255,0),1)
for red in contours_red:
 x_red,y_red,w_red,h_red = (red)
 if w_red>width|h_red>height:
 (img,(x_red,y_red),((x_red+h_red),(y_red+w_red)),(0,255,0),1)

To bring out the color disparity, I use a green rectangular box, draw red objects, and a red rectangular box to draw green objects

Above this Python-opencv to achieve red and green color recognition operation is all that I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.