Python OpenCV 错误:(-215)size.width>0 && size.height>0 in function imshow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27953069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:31:37  来源:igfitidea点击:

OpenCV Error: (-215)size.width>0 && size.height>0 in function imshow

pythonimageopencvcomputer-visionopencv3.0

提问by user3377126

I am trying to make a face tracker that combines Haar Cascade Classification with Lucas Kanade good feature detection. However, I keep getting an error that I cannot figure out what it means nor how to solve it.

我正在尝试制作一个结合 Haar Cascade 分类和 Lucas Kanade 良好特征检测的人脸跟踪器。但是,我不断收到一个错误,我无法弄清楚它的含义以及如何解决它。

Can anyone help me here?

有人能帮我一下吗?

Error:

错误:

line 110, in <module>
cv2.imshow('frame',img)
error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: 
error: (-215)size.width>0 && size.height>0 in function imshow

Code:

代码:

from matplotlib import pyplot as plt
import numpy as np

import cv2

face_classifier = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')


cap = cv2.VideoCapture(0)


# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 200,
                       qualityLevel = 0.01,
                       minDistance = 10,
                       blockSize = 7 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Create some random colors
color = np.random.randint(0,255,(100,3))

# Take first frame and find corners in it
ret, old_frame = cap.read()



cv2.imshow('Old_Frame', old_frame)
cv2.waitKey(0)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
restart = True
#while restart == True:
face = face_classifier.detectMultiScale(old_gray, 1.2, 4)

if len(face) == 0:
    print "This is empty"

for (x,y,w,h) in face:
    focused_face = old_frame[y: y+h, x: x+w]

cv2.imshow('Old_Frame', old_frame)

face_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY)

gray = cv2.cvtColor(focused_face,cv2.COLOR_BGR2GRAY)

corners_t = cv2.goodFeaturesToTrack(gray, mask = None, **feature_params)
corners = np.int0(corners_t)

print corners

for i in corners:
    ix,iy = i.ravel()
    cv2.circle(focused_face,(ix,iy),3,255,-1)
    cv2.circle(old_frame,(x+ix,y+iy),3,255,-1)

plt.imshow(old_frame),plt.show()


# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)

while(1):
    ret,frame = cap.read()
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # calculate optical flow
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, corners_t, None, **lk_params)

    # Select good points
    good_new = p1[st==1]
    good_old = corners_t[st==1]

    # draw the tracks
    print "COLORING TIME!"
    for i,(new,old) in enumerate(zip(good_new,good_old)):
        print i
        print color[i]
        a,b = new.ravel()
        c,d = old.ravel()
        mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
        frame = cv2.circle(frame,(a, b),5,color[i].tolist(),-1)
        if i == 99:
            break
    img = cv2.add(frame,mask)

    cv2.imshow('frame',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

    # Now update the previous frame and previous points
    old_gray = frame_gray.copy()
    p0 = good_new.reshape(-1,1,2)

cv2.destroyAllWindows()
cap.release()

回答by Gauranga

This error message

这个错误信息

error: (-215)size.width>0 && size.height>0 in function imshow

错误:(-215)size.width>0 && size.height>0 in function imshow

simply means that imshow() is not getting video frame from input-device. You can try using

只是意味着 imshow() 没有从输入设备获取视频帧。您可以尝试使用

cap = cv2.VideoCapture(1) 

instead of

代替

cap = cv2.VideoCapture(0) 

& see if the problem still persists.

&看看问题是否仍然存在。

回答by moises stevend meza rodriguez

while(cap.isOpened()):

    ret, img = cap.read()
    print img
    if img==None:   #termino los frames?
        break   #si, entonces terminar programa
    #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imshow('img2',img)

回答by ???

You have to delay

你必须延迟

Example Code:

示例代码:

import cv2
import numpy as np
import time

cam = cv2.VideoCapture(0)
time.sleep(2)

while True:
    ret,frame = cam.read()
    cv2.imshow('webcam', frame)
    if cv2.waitKey(1)&0xFF == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()

回答by Zed86M

In these two lines:

在这两行中:

mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)

frame = cv2.circle(frame,(a, b),5,color[i].tolist(),-1)

try instead:

改为尝试:

cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)

cv2.circle(frame,(a, b),5,color[i].tolist(),-1)

I had the same problem and the variables were being returned empty

我遇到了同样的问题,变量返回空

回答by Irum Zahra Awan

cv2.circleand cv2.linesare not working. Mask and frame both are returning None. these functions (line and circle) are in opencv 3 but not in older versions.

cv2.circle并且cv2.lines不工作。面具和框架都回来了None。这些函数(线和圆)在 opencv 3 中,但不在旧版本中。

回答by Brayan Cruz

I have the same problem, fix the ret in capture video

我有同样的问题,修复捕获视频中的 ret

import numpy as np
import cv2

# Capture video from file
cap = cv2.VideoCapture('video1.avi')

while True:

    ret, frame = cap.read()

    if ret == True:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('frame',gray)


        if cv2.waitKey(30) & 0xFF == ord('q'):
            break

    else:
        break

cap.release()
cv2.destroyAllWindows()

回答by pasagar

Check if you have "opencv_ffmpeg330.dll"in python27 root directory or of the python version you are using. If not you will find it in "....\OpenCV\opencv\build\bin".

检查python27根目录或您使用的python版本中是否有“opencv_ffmpeg330.dll”。如果没有,您会在"....\OpenCV\opencv\build\bin" 中找到它。

Choose the appropriate version and copy the dll into the root directory of your python installation and re-run the program

选择合适的版本,将dll复制到你python安装的根目录下,重新运行程序

回答by allenyllee

I use ssh to connect to remote server and have python code execute cv2.VideoCapture(0) to capture remote webcam, then encounter this error message:

我使用 ssh 连接到远程服务器并让 python 代码执行 cv2.VideoCapture(0) 来捕获远程网络摄像头,然后遇到此错误消息:

error: (-215)size.width>0 && size.height>0 in function imshow

错误:(-215)size.width>0 && size.height>0 in function imshow

Finally, I have to grant access to /dev/video0 (which is my webcam device) with my user account and the error message was gone. Use usermod to add user into group video

最后,我必须使用我的用户帐户授予对 /dev/video0(这是我的网络摄像头设备)的访问权限,并且错误消息消失了。使用 usermod 将用户添加到视频组

usermod -a -G video user

回答by Ethan Kuo

I also met the error message in raspberry pi 3, but my solution is reload kernel of camera after search on google, hope it can help you.

我在raspberry pi 3中也遇到了错误信息,但我的解决方案是在谷歌搜索后重新加载相机内核,希望它可以帮助你。

sudo modprobe bcm2835-v4l2

须藤 modprobe bcm2835-v4l2

BTW, for this error please check your camera and file path is workable or not

顺便说一句,对于此错误,请检查您的相机和文件路径是否可用

回答by Par bas

This is a problem with space consumption or choosing the wrong camera. My suggestion in to restart kernel and clear output and run it again. It works then.

这是空间消耗或选择错误相机的问题。我的建议是重新启动内核并清除输出并再次运行它。然后它起作用了。