import cv2
import sys
import numpy as np
from PyQt5.QtWidgets import QLabel,QApplication
from PyQt5.QtGui import QPixmap
green_val = 114
def shibie():
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# get a frame
#frame = cv2.imread("home\pi\lanse.jpg")
#cv2.imshow("haha",frame)
#cv2.waitKey(1000)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
blue_lower = np.array([green_val - 10, 100, 100])
blue_upper = np.array([green_val + 10, 255, 255])
blue_mask = cv2.inRange(hsv, blue_lower, blue_upper)
blue_res = cv2.bitwise_and(frame, frame, mask=blue_mask)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
blue_closing = cv2.morphologyEx(blue_res, cv2.MORPH_CLOSE, kernel)
blue_gray = cv2.cvtColor(blue_closing, cv2.COLOR_BGR2GRAY)
(thresh2, blue_bw) = cv2.threshold(blue_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
blue_black = cv2.countNonZero(blue_bw)
cv2.imshow("hahaa",blue_bw)
cv2.waitKey(1000)
if blue_black > 121000:#判断二值化像素点个数
print("视频中有蓝色像素")
app = QApplication(sys.argv)
label = QLabel()
label.setPixmap(QPixmap("lanse.jpg"))
label.show()
label.showFullScreen()
label.setScaledContents(True) # 让图片自适应label大小
while(True):
if cv2.waitKey(2000):#如果有新的拍照指令则退出,继续循环
break
else:
print("无蓝色")
# 完成所有操作后,释放捕获
cap.release()
# show a frame
#cv2.imshow("capture", frame)
#cv2.imwrite("home\pi\lanse.jpg", frame)
#a = cv2.imread("home\pi\lanse.jpg")
#cv2.imshow("duqu",a)
while(True):
shibie()
#sys.exit(app.exec())
cv2.destroyAllWindows()