1、导入所需要的库
import cv2
import numpy as np
2、视频读取
cap = cv2.VideoCapture('红绿灯.mp4')
while True:
ret,frame = cap.read()
if ret == False:
break
frame=cv2.resize(frame,(1720,800))
3、图片截取
src = cv2.imread("green.png")
src_l = cv2.resize(src,(600,500))#修改图片的尺寸
# cv2.imshow('re',src_l)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#------------------------
# print("--------- HL截取ROI区域的测试 ---------")
# print("鼠标选择ROI,然后点击 enter键")
r = cv2.selectROI('org', src_l, False) # ,返回 (x_min, y_min, w, h)
# roi区域
roi = src_l[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
cv2.destroyAllWindows()
cv2.imshow('ROI',roi)#显示ROI区域
#-------------
HSV_img = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
cv2.imshow('d',HSV_img)
image_hsv = cv2.resize(HSV_img,(600,500))#修改图片的尺寸
H = image_hsv[:, :, 0]
H = np.squeeze(np.reshape(H, (1, -1)))
H_value = np.array([])
print(H)
4、二值图像颜色判定
# def judge_light_color(src):
# for i in H:
# if i > 0 and i < 124:
# H_value = np.append(H_value, i)
# elif i > 155: # 将大于155的红色的H值,都看成是1,方便处理
# H_value = np.append(H_value, 1.0)
# H_avg = np.average(H_value)
# if H_avg <= 18 or H_avg >= 156:
# print('R')
# elif H_avg >= 18 and H_avg <= 34:
# print('y')
# elif H_avg >= 35 and H_avg <= 100:
# print('G')
# else:
# print("x")
# return color
# cv2.imshow('HSV',image_hsv)
# H, S, V = cv2.split(roi)
# cv2.imshow('H', H)
# cv2.imshow('S', S)
# cv2.imshow('V', V)
cv2.waitKey()
cv2.destroyAllWindows()
# print(H)
# print(S)
# print(V)
# cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
# cv2.imshow("input", src)
# src = cv2.resize(src, (0,0), fx=1.2, fy=1.2)#水平轴与垂直轴上的比例因子,我给它拉大了1.2倍
# print("--------- HL截取ROI区域的测试 ---------")
# print("鼠标选择ROI,然后点击 enter键")
# # r = cv2.selectROI('org', src, False) # ,返回 (x_min, y_min, w, h)
#
# # roi区域
# roi = src[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# cv2.destroyAllWindows()
# cv2.imshow('ROI',roi)#显示ROI区域
#
# (B,G,R) = cv2.split(roi)#提取R、G、B分量
#
# cv2.imshow("Red",R)
# cv2.imshow("Green",G)
# cv2.imshow("Blue",B)
# cv2.waitKey(0)
# print(R)
# print("---------------")
# print(B)
# print("---------------")
# print(G)
# if
# elif
# elif
k = cv2.waitKey(0) & 0xFF
if k == 27: # 按Esc 键即可退出
cv2.destroyAllWindows()
# rose2 = cv2.resize(rose,(600,500))#修改图片的尺寸
# cv2.imwrite('D:\s_er.jpg', roi)
# """
# 提取图中的红色部分
# """
# hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
# low_hsv = np.array([0,43,5])
# high_hsv = np.array([10,255,255])
# mask = cv2.inRange(hsv,lowerb=low_hsv,upperb=high_hsv)
# cv2.imshow("test",mask)
# cv2.waitKey(0)
# cv2.destroyAllWindows()