Python OpenCV4 Fun Applications series (four) --- color of objects detected in real time

Today, we have a video to achieve real-time detection of small color of the object instance, has three primary color video objects, we only detect red and green spherical object, as shown below:

The first step required to open the video (or camera):

cap = cv2.VideoCapture ( '1.mp4') # open a video file 
# cap = cv2.VideoCapture (0) # open USB camera

Cycles then need to take the frames, color object detection. Detecting HSV color of an object using a color filter threshold value, the threshold value is set to the key HSV, HSV color following table are commonly used:

But the picture also need specific tools to write your own take to extract a small HSV value of the target on the picture, and then manually set threshold, such as in the picture above the red and green we use HSV thresholds are as follows:

lower_green = np.array ([35, 110 , 106]) # green range low threshold 
upper_green = np.array ([77, 255 , 255]) # green range of the high threshold 

lower_red = np.array ([0, 127 , 128 ]) # red range low threshold 
upper_red = np.array ([10, 255 , 255]) # high threshold red range

The next step is a filtering process, the outline extraction and the final result is indicative denoted with a rectangular box object detection, while the color marked with putText function, the complete code and final results are as follows:
complete code:

# - * - Coding: cp936 - * - 
Import numpy AS NP 

Import CV2 

font = cv2.FONT_HERSHEY_SIMPLEX 

lower_green np.array = ([35, 110, 106]) # green range low threshold 
upper_green = np.array ([77, 255 , 255]) # green range of the high threshold 

lower_red = np.array ([0, 127 , 128]) # red range low threshold 
upper_red = np.array ([10, 255 , 255]) # high threshold red range 

cap = cv2 .VideoCapture ( '1.mp4') # open a video file 
# cap = cv2.VideoCapture (0) # open USB camera 


if (cap.isOpened ()): # video open succeeded 
    In Flag. 1 = 
the else: 
    In Flag = 0 

NUM = 0 
IF (In Flag): 
    the while (True): 
        RET, frame = cap.read () reads a # 
        # IF (frame None IS): 
        IF RET == False: reading frames failed # 
            break
        = cv2.cvtColor hsv_img (Frame, cv2.COLOR_BGR2HSV) 
        mask_green = cv2.inRange (hsv_img, lower_green, upper_green) # puncturing range according to the color selected from 
        mask_red = cv2.inRange (hsv_img, lower_red, upper_red) # puncturing range according to the color selected from 
        mask_green = cv2.medianBlur (mask_green, 7) # median filter 
        mask_red = cv2.medianBlur (mask_red, 7) # median filter 
        mask = cv2.bitwise_or (mask_green, mask_red) 
        cv2.imshow ( 'mask_green', mask_green) 
        cv2.imshow ( 'mask_red', mask_red) 
        cv2.imshow ( 'mask', mask) 
        mask_green, Contours, cv2.findContours = Hierarchy (mask_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        mask_red, contours2, hierarchy2 = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 
        for CNT in Contours: 
            (X, Y, W, H) = cv2.boundingRect (cnt)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2)
            cv2.putText(frame, "green", (x, y - 5), font, 0.7, (0, 255, 0), 2)

        for cnt2 in contours2:
            (x2, y2, w2, h2) = cv2.boundingRect(cnt2)
            cv2.rectangle(frame, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 255), 2)
            cv2.putText(frame, "red", (x2, y2 - 5), font, 0.7, (0, 0, 255), 2)
        num = num + 1
        cv2.imshow("result", frame)
        cv2.imwrite("imgs/%d.jpg"%num, frame)

        if cv2.waitKey(20) & 0xFF == 27:# Press Esc to exit 
            break

cv2.waitKey(0)

cv2.destroyAllWindows()

The net effect animation:

[OpenCV depth attention and learning AI] for more information about learning

Long press or scan the following QR code to follow

  

  

Guess you like

Origin www.cnblogs.com/stq054188/p/11883916.html