#导入numpy库和opencv库 import numpy as np import cv2
#读取图片,并对图片进行分割有效区域
img = cv2.imread("rrrrrr.PNG",1)
roi = img[220:260, 300:380]
#展式图片
cv2.imshow("img", roi)


#统计rgb的值 bb = np.sum(roi,1) cc = np.sum(bb,0) print(cc) #打印rgb各个值

#进行比较判断,比较bgr的值。
if cc[1]<cc[2]:
print("红灯")
elif cc[1]>cc[2]:
print("绿灯")
else:
print("黄灯")
#关闭
cv2.waitKey(0)
cv2.destroyAllWindows()
最后结果

本文介绍了一种使用Python的OpenCV库和NumPy库来识别图片中交通灯颜色的方法。通过读取并分割特定区域的图片,统计该区域的RGB值,并根据BGR值的大小关系判断交通灯的颜色。

被折叠的 条评论
为什么被折叠?



