python判断红绿灯

import cv2
import numpy as npcap = cv2.VideoCapture(r"C:\Users\吴金松\Videos.mp4")  # 读取视频
fps = cap.get(cv2.CAP_PROP_FPS)  # 获取帧率while cap.isOpened():ret, frame = cap.read()cv2.namedWindow("frame", 0)  # 0可调大小,注意:窗口名必须imshow里面的一窗口名一致cv2.resizeWindow("frame", 960, 540)  # 设置长和宽gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 将视频以灰度图展示cv2.putText(gray, 'fps: ' + str(fps), (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)  # 在展示窗口中显示帧率
# 划定三个感兴趣区域分别为R, Y, G
# ROI划定规则:图像矩阵名称[上 : 下, 左 : 右]R = gray[452:454, 594:596]Y = gray[446:448, 630:632]G = gray[444:446, 672:674]# 为划定的感兴趣区域赋予变量sum_R = np.sum(R)sum_Y = np.sum(Y)sum_G = np.sum(G)# 判断模块if sum_R > 200:# 如果R中那四个像素点的灰度值之和大于200,则显示红灯cv2.putText(frame, 'Red', (580, 400), 2, 2, (0, 0, 255), 3)cv2.putText(frame, 'Red', (1460, 650), 2, 2, (0, 0, 255), 3)cv2.putText(frame, 'Red', (1230, 670), 2, 2, (0, 0, 255), 3)# 用法   putText(选定的视频[或赋予其的变量], "要显示的文字", 字体[0为默认], 字体粗细程度, [颜色 BGR](  ,  ,  ,), 字体大小)elif sum_Y > 200:# 如果Y中那四个像素点的灰度值之和大于200,则显示黄灯cv2.putText(frame, 'Yellow', (550, 410), 2, 2, (0, 255, 255), 3)cv2.putText(frame, 'Yellow', (1460, 650), 2, 2, (0, 255, 255), 3)cv2.putText(frame, 'Yellow', (1230, 670), 2, 2, (0, 255, 255), 3)elif sum_G > 200:# 如果G中那四个像素点的灰度值之和大于200,则显示绿灯cv2.putText(frame, 'Green', (550, 410), 2, 2, (0, 255, 0), 3)cv2.putText(frame, 'Green', (1460, 650), 2, 2, (0, 255, 0), 3)cv2.putText(frame, 'Green', (1230, 670), 2, 2, (0, 255, 0), 3)else:# 如果都小于200,则显示等待cv2.putText(frame, '', (550, 410), 2, 2, (255, 255, 255), 3)cv2.putText(frame, '', (1460, 650), 2, 2, (255, 255, 255), 3)cv2.imshow('frame', frame)  # 显示原视频# 按 'm' 退出if cv2.waitKey(1) & 0xFF == ord('m'):break
cap.release()  # 释放

本文链接:https://my.lmcjl.com/post/6671.html

展开阅读全文

4 评论

留下您的评论.