OpenCV图片无法显示
网上看了好些个解决方法,都不行,人都晕了
import cv2 as cv
import numpy as np
#构建一个名字为SW的窗口
cv.namedWindow('SW',cv.WINDOW_NORMAL)
#调整SW窗口的的大小为720*540
#cv.resizeWindow('SW',720,540)
#VideoCapture函数用来捕捉摄像头,数字0表示不同的设备
cap=cv.VideoCapture(1,cv.CAP_DSHOW)
print('调取摄像头函数执行')
#循环读取摄像头的每一帧
while True:
#读取每一帧标记,True表示读到了数据,false表示没有读到数据
success,frame = cap.read()
if not success:
print("不能读取显示内容")
break
if cap.isOpened():
print('打开了摄像头')
else:
print('未打开摄像头')
#显示数据
print('Successful Print')
cv.imshow('SW',frame)
print('显示')
key = cv.waitKey(0)
print('等待输入键值')
if key == ord('q'):
break
#释放资源
cap.release()
cv.destroyAllWindows()
电脑型号是Y7000P
可能是因为你没有插入摄像头或者缺少相关的库,试试下面这段代码:
如果可行还望 采纳
import cv2
# 开启摄像头,0代表默认摄像头
cap = cv2.VideoCapture(0)
# 判断是否开启成功
if cap.isOpened():
print("摄像头已经开启")
else:
print("摄像头没有开启")
# 循环读取每一帧
while True:
# 读取每一帧
ret, frame = cap.read()
if ret:
# 显示图像
cv2.imshow("frame", frame)
else:
print("无法读取图像")
break
# 等待按键
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
cap.release()
cv2.destroyAllWindows()
试下:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Display the resulting frame
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
你是内置摄像头还是外置摄像头?
如果不用 OpenCV 程序,用系统的设备管理器,摄像头能否正常工作?或者,摄像头能否用微信视频通话?