流程图如下
全代码:
import cv2 import numpy as np cap = cv2.VideoCapture('red.mp4') while True: ret, frame = cap.read() if ret == False: break frame = cv2.resize(frame, (1720, 800)) # 截取roi区域 roiColor = frame[320:340, 510:600] # 转换hsv颜色空间 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) # 中值滤波 red_blur = cv2.medianBlur(mask_red, 7) # green lower_hsv_green = np.array([49, 79, 137]) upper_hsv_green = np.array([90, 255, 255]) mask_green = cv2.inRange(hsv, lowerb=lower_hsv_green, upperb=upper_hsv_green) # 中值滤波 green_blur = cv2.medianBlur(mask_green, 7) # yellow lower_hsv_yellow = np.array([0, 19, 110]) upper_hsv_yellow = np.array([240, 153, 255]) mask_yellow = cv2.inRange(hsv, lowerb=lower_hsv_yellow, upperb=upper_hsv_yellow) # 中值滤波 yellow_blur = cv2.medianBlur(mask_yellow, 7) # 因为图像是二值的图像,所以如果图像出现白点,也就是255,那么就取他的max最大值255 red_color = np.max(red_blur) green_color = np.max(green_blur) yellow_color = np.max(yellow_blur) # 在red_color中判断二值图像如果数值等于255,那么就判定为red if red_color == 255: cv2.rectangle(frame, (500, 300), (630, 350), (0, 0, 255), 2) # 按坐标画出矩形框 cv2.putText(frame, "red", (420, 360), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2) # 显示red文本信息 # 在green_color中判断二值图像如果数值等于255,那么就判定为green elif green_color == 255: cv2.rectangle(frame, (500, 300), (630, 350), (0, 255, 0), 2) # 按坐标画出矩形框 cv2.putText(frame, "green", (420, 360), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) # 显示green文本信息 # 在yellow_color中判断二值图像如果数值等于255,那么就判定为yellow elif yellow_color == 255: cv2.rectangle(frame, (500, 300), (630, 350), (0, 255, 255), 2) # 按坐标画出矩形框 cv2.putText(frame, "yellow", (420, 360), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 255), 2) # 显示yellow文本信息 cv2.imshow('frame', frame) red_blur = cv2.resize(red_blur, (300, 200)) green_blur = cv2.resize(green_blur, (300, 200)) yellow_blur = cv2.resize(yellow_blur, (300, 200)) cv2.imshow('red_window', red_blur) cv2.imshow('green_window', green_blur) cv2.imshow('yellow_window', yellow_blur) c = cv2.waitKey(1) if c == 27: break