(OpenCV,cv2)通过相机透镜捕获图像时出现cv2.error

kd3sttzy  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(252)

Error (-215) size.width>0 && size.height>0 occurred when attempting to display an image using OpenCV
我有类似的问题与此讨论,在我读了它仍然没有线索,为如何解决我的,我张贴在这里我的情况

  • 我使用raspberrypi 4 ; 32位,Linux运行Python ;罗技USB摄像头透镜;在虚拟环境中运行 *
  • Python脚本opencv_camera.py
import cv2
 
# define a video capture object
vid = cv2.VideoCapture(1) ########## 1- device id =1###if 1 deosn't work try 2
 
while(True):
     
    ret, frame = vid.read()
 

    cv2.imshow('frame', frame)
     
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
vid.release()

cv2.destroyAllWindows()

发生错误:
函数“imshow”中出现错误:OpenCV(4.6.0)....

  • 输出:
(venv) joy@raspberrypi:~/Desktop $ python opencv_camera.py
[ WARN:0@0.020] global /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Traceback (most recent call last):
  File "/home/joy/Desktop/opencv_camera.py", line 14, in <module>
    cv2.imshow('frame', frame)
cv2.error: OpenCV(4.6.0) /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
  • 故障排除状态:现在发现Ret没有
import cv2
 
 
# define a video capture object
vid = cv2.VideoCapture(2) ########## 1- device id =1###if 1 deosn't work try 2
 
while(True):
    ret, frame = vid.read()
           

    if ret == True:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
               
    else:
       
        print("ret is empty")
        break

 
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

输出:

(venv) joy@raspberrypi:~/Desktop $ python opencv_camera.py
[ WARN:0@0.019] global /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
ret is empty
qncylg1j

qncylg1j1#

如果要拍摄图像,则不应使用

while(True):

因为这意味着你将不断地阅读来自相机的内容。
参考下面的代码,如果你只想拍一张照片,你可能会不断运行

ret, frame = vid.read()

重拍一张新照片
代码:

import cv2
 
 
# define a video capture object
vid = cv2.VideoCapture(0) 

#Read the current camera
ret, frame = vid.read()
cv2.imshow('frame', frame)
cv2.waitKey(1)
             
#Unattach the camera
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
vkc1a9a2

vkc1a9a22#

在我将vid = cv2.VideoCapture()更改为0之后,现在视频功能正在工作,但我最初的目的是拍摄一张图像

import cv2
 
 
# define a video capture object
vid = cv2.VideoCapture(0) ########## 1- device id =1###if 1 deosn't work try 2

 
while(True):
    ret, frame = vid.read()
             

    if ret == True:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
       
           
    else:
       
        print("ret is empty")
        break
 
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
  • 也用于采摘
import cv2
import time
 
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret,frame = cap.read() # return a single frame in variable `frame`
 
# run 60 sec x 2 min
time_end = time.time() + 100
 
while time.time() < time_end:
 
    while(True):
        cv2.imshow('img1',frame) #display the captured image
        if cv2.waitKey(60) & 0xFF == ord('y'): #save on pressing 'y'
            cv2.imwrite('/home/joy/Desktop/pic.png',frame)
            cv2.destroyAllWindows()
            break
   
    cap.release()
    break

相关问题