from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import imutils
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640,480))
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
#cv2.imshow("frame", image)
gray = cv2.cvtColor(frame.array, cv2.COLOR_BGR2GRAY)[1]
#cv2.imshow("Gray", gray)
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)[1]
#cv2.imshow("Thresh", thresh)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
output = thresh.copy()
cv2.imshow("frame", image)
cv2.imshow("Gray", gray)
cv2.imshow("Thresh", thresh)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
if key == ord("q"):
break
伙计们,当我在带有opencv的虚拟环境中运行上述代码时,我正在运行raspbian的raspberry pi 3 b +上的python 3.5.3上运行opencv 3.3.0,它可以编译并运行,但无法正常工作。框架窗口显示并更新良好,但打开了灰色和脱粒框架,然后宽度接近0。我附上一张照片,如果有人有任何深刻见解。desktop
答案 0 :(得分:0)
尝试更改
gray = cv2.cvtColor(frame.array, cv2.COLOR_BGR2GRAY)[1]
简单
gray = cv2.cvtColor(frame.array, cv2.COLOR_BGR2GRAY)