OpenCV imshow功能在jupyter笔记本中显示黑色图像 [英] OpenCV imshow function display black image in jupyter notebook
问题描述
我使用从opencv网站复制的以下代码:
I use the following code copied from opencv website :
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
但是图像是黑色的,带有一些白噪声:
But the image is black with some white noise :
我非常确定问题不是来自我的网络摄像头设备,因为我在Windows 10中使用相机" APP,图片可以很好地显示.
I am pretty sure the problems is not come from my webcam device because I use "camera" APP in Windows 10, the picture can display well.
以下是我的python环境:
The following is my python environment :
Python : 3.7.1
OpenCV : 4.1.0.25 (also tried 3.4.5.20)
OS : windows 10
Webcam : Logitech C525
----------------------------更新------------------ --------------
----------------------------update--------------------------------
我使用anaconda spyder运行相同的代码,它运行完美!
I use anaconda spyder to run the same code, it work perfectly!
问题仅在我使用jupyter笔记本时出现,有什么解决方法吗?
The problems only shows up when I use jupyter notebook, any solutions?
推荐答案
尝试一下,您可以使用isOpened()
确保可以连接到相机.
Try this, you can use isOpened()
to ensure that you can connect to the camera.
from threading import Thread
import cv2, time
class VideoStreamWidget(object):
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(.01)
def show_frame(self):
# Display frames in main program
cv2.imshow('frame', self.frame)
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
cv2.destroyAllWindows()
exit(1)
if __name__ == '__main__':
video_stream_widget = VideoStreamWidget()
while True:
try:
video_stream_widget.show_frame()
except AttributeError:
pass
这篇关于OpenCV imshow功能在jupyter笔记本中显示黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!