Opencv image processing based on python performs color detection on traffic lights at traffic intersections, the first step for unmanned car driving!

1. Video reading

First, read the video in, because the video I tested is 4k, so I adjusted the resolution of the video with resize

cap = cv2.VideoCapture('video/小路口.mp4')
while True:
    ret,frame = cap.read()
    if ret == False:
        break
    frame = cv2.resize(frame,(1920,1080))
    cv2.imshow('frame',frame)
    c = cv2.waitKey(10)
    if c==27:
        break

imshow() (as shown below)

2. Intercept roi area

Intercept the roi area, that is to say, in order to avoid unnecessary interference factors, we have to intercept the position of the traffic light (as shown in the figure below)

The intercepted roi (as shown in the figure below)

3. Convert hsv color space

HSV color component range (refer to the original link for details)
Generally, effective processing of color space images is performed in HSV space, and then a strict range needs to be given for the corresponding HSV components in the basic colors. The following is calculated through experiments Fuzzy range (the exact range is not given on the Internet).

H: 0— 180

S: 0— 255

V: 0-255

Here part of the red is classified as the purple range (as shown in the figure below):

The above is given a specific color value. If your color effect is not good, you can use python code to fine-tune the min and max values. Use the API in opencv to get the desired color you need. You can copy the following code to Make color adjustments.
1. First you have to take a picture of the roi area
2. Read the picture and adjust the color value

The color adjustment code is as follows: (refer to the video tutorial link for details)

import cv2
import numpy as np

def empty(a):
    pass

def stackImages(scale,imgArray):
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range ( 0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor= np.hstack(imgArray)
        ver = hor
    return ver

#读取的图片路径
path = './green.jpg'
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",640,240)
cv2.createTrackbar("Hue Min","TrackBars",0,179,empty)
cv2.createTrackbar("Hue Max","TrackBars",19,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",110,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",240,255,empty)
cv2.createTrackbar("Val Min","TrackBars",153,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)

while True:
    img = cv2.imread(path)
    imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    h_min = cv2.getTrackbarPos("Hue Min","TrackBars")
    h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
    s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
    s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
    v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
    v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
    print(h_min,h_max,s_min,s_max,v_min,v_max)
    lower = np.array([h_min,s_min,v_min])
    upper = np.array([h_max,s_max,v_max])
    mask = cv2.inRange(imgHSV,lower,upper)
    imgResult = cv2.bitwise_and(img,img,mask=mask)


    imgStack = stackImages(0.6,([img,imgHSV],[mask,imgResult]))
    cv2.imshow("Stacked Images", imgStack)
    cv2.waitKey(1)

After running the code, the adjustment result (as shown in the figure below), it is obvious that the green color has been acquired.

4. Binary image color judgment

Because the image is a binary image, if the image has a white point, which is 255, then take his max maximum value of 255, the video frame changes continuously and then iterates through each color value

red_color = np.max(red_blur)
green_color = np.max(green_blur)
if red_color == 255:
	print('red')
elif green_color == 255:
	print('green')

5. The color result is drawn on the image

Use a rectangular frame to select the traffic light area

cv2.rectangle(frame,(1020,50),(1060,90),(0,0,255),2) 
#Draw a rectangular frame by coordinates cv2.putText(frame, "red", (1020, 40), cv2 .FONT_HERSHEY_COMPLEX, 1, (0, 0, 255),2)#Display red text information



6. Complete code

import cv2 
import numpy as np 

cap = cv2.VideoCapture('video/小路口.mp4') 
while True: 
    ret,frame = cap.read() 
    if ret == False: 
        break 
    frame = cv2.resize(frame,(1920 ,1080)) 
    #Intercept 
    roi area roiColor = frame[50:90,950:1100] #Convert 
    hsv color space 
    hsv = cv2.cvtColor(roiColor,cv2.COLOR_BGR2HSV) 

    #red 
    lower_hsv_red = np.array([157,177,122]) 
    upper_hsv_red = np .array([179,255,255]) 
    mask_red = cv2.inRange(hsv,lowerb=lower_hsv_red,upperb=upper_hsv_red) 
    #Median 
    filtering red_blur = cv2.medianBlur(mask_red, 7) 
    #green 
    lower_hsv_green = np.array([49,79,137])
    upper_hsv_green = np,array([90,255] 
    mask_green = cv2.inRange(hsv,lowerb=lower_hsv_green,upperb=upper_hsv_green) 
    #Median 
    filter green_blur = cv2.medianBlur(mask_green, 7) #Because the 

    image is a binary image, If the image has a white point, which is 255, then take his maximum max value 255 
    red_color = np.max(red_blur) 
    green_color = np.max(green_blur) 
    #Judging the binary image in red_color if the value is equal to 255, then Determined as red 
    if red_color == 255: 
        print('red') 
                        #. . . This is the coordinate I often confuse. . . Just list them out and remember them. . . 
                        # y y+hx x+w 
                        #frame[50:90,950:1100] 

                        # xy x+w y+h 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,0,255),2 ) #Draw a rectangular frame by coordinates
        cv2.putText(frame, "red", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255),2) red text information
    #Judge the binary image in green_color if the value is equal to 255, then it is judged as green 
    elif green_color == 255: 
        print('green') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,255 ,0),2) 
        cv2.putText(frame, "green", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0),2) 

    cv2.imshow('frame',frame) 
    red_blur = cv2.resize(red_blur,(300,200)) 
    green_blur = cv2.resize(green_blur,(300,200)) 
    cv2.imshow('red_window',red_blur) 
    cv2.imshow('green_window',green_blur) 

    c = cv2.waitKey(10) 
    if c==27: 
        break

Detect the effect of red light (as shown in the figure below)

Check the effect of the green light (as shown in the figure below)

At last! ! ! Click here to get the complete project code

First contact with opencv! So please don’t spray me, the big guys in the visual field! (/狗头) The
 
amount of code is very small, there is no generalization ability, a very low approach. . . But for me, learning hsv color space is very helpful! That's it! Ollie give it!

Everyone can support Xiaoming children's shoes more!

This article is reprinted, the copyright belongs to the author, if there is any infringement, please contact the editor to delete it!

Original address: https://blog.csdn.net/weixin_44912902

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/109049373