python - 如何修复 imshow() 显示灰色图像
问题描述
我正在尝试在我的树莓派屏幕上显示网络摄像头捕获。它显示第一帧没有问题,但下一帧显示为灰色帧。
这适用于 Windows,但不适用于覆盆子。
import cv2 as cv
cap = cv.VideoCapture(0)
ret, frame = cap.read()
cv.imshow('frame0',frame)
while True:
ret, frame = cap.read()
cv.imshow('frame',frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
解决方案
首先安装Picamera
模块,然后尝试:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
来源:https ://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/
推荐阅读
- angular - 如何通过索引/序数迭代Angular 5中的枚举?
- awk - 使用 grep、sed、awk(或 cut)定位要删除的字符串
- java - 用两个数组中的公共元素创建一个新数组
- python - 继续按住一个键并释放另一个
- ffmpeg - 我可以结合这两个命令吗?(或者我在打一场失败的战斗?)
- vba - MSWord - 前导引号不通过 VBA 获取字体
- amazon-web-services - 如何限制 S3 视频的访问次数?
- sql - NHibernate IN 表达式/限制达到 2100 参数限制 SQL Server
- java - 如何在eclipse中使用Maven Jetty插件运行多个maven项目
- xml - 以特定表顺序从 XML 进行 XLS 转换