简体   繁体   English

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

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

I am trying to make a face tracker that combines Haar Cascade Classification with Lucas Kanade good feature detection.我正在尝试制作一个结合 Haar Cascade 分类和 Lucas Kanade 良好特征检测的人脸跟踪器。 However, I keep getting an error that I cannot figure out what it means nor how to solve it.但是,我不断收到一个错误,我无法弄清楚它的含义以及如何解决它。

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()

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.只是意味着 imshow() 没有从输入设备获取视频帧。 You can try using您可以尝试使用

cap = cv2.VideoCapture(1) 

instead of代替

cap = cv2.VideoCapture(0) 

& see if the problem still persists. & 看看问题是否仍然存在。

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()

I had this problem.我有这个问题。

Solution: Update the path of the image.解决方法:更新图片路径。

If the path contains (for example: \\n or \\t or \\a ) it adds to the corruption.如果路径包含(例如: \\n or \\t or \\a ),则会增加损坏。 Therefore, change every back-slash "\\" with front-slash "/" and it will not make create error but fix the issue of reading path.因此,将每个反斜杠“\\”更改为正斜杠“/”,它不会导致创建错误,而是修复读取路径的问题。

Also double check the file path/name.还要仔细检查文件路径/名称。 any typo in the name or path also gives the same error.名称或路径中的任何拼写错误也会出现相同的错误。

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()

I have also met this issue.我也遇到过这个问题。 In my case, the image path is wrong, so the img read is NoneType .就我而言,图像路径是错误的,因此读取的 img 是NoneType After I correct the image path, I can show it without any issue.更正图像路径后,我可以毫无问题地显示它。

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我遇到了同样的问题,变量返回空

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顺便说一句,对于此错误,请检查您的相机和文件路径是否可用

That error also shows when the video has played fine and the script will finish but that error always throws because the imshow() will get empty frames after all frames have been consumed.该错误还会显示视频何时播放正常并且脚本将完成,但该错误始终会引发,因为 imshow() 在所有帧都被消耗后将获得空帧。

That is especially the case if you are playing a short (few sec) video file and you don't notice that the video actually played on the background (behind your code editor) and after that the script ends with that error.如果您正在播放一个短(几秒)的视频文件并且您没有注意到视频实际上是在后台播放的(在您的代码编辑器后面),则尤其如此,之后脚本以该错误结束。

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)

cv2.circle and cv2.lines are not working. cv2.circlecv2.lines不起作用。 Mask and frame both are returning None . Mask 和 frame 都返回None these functions (line and circle) are in opencv 3 but not in older versions.这些函数(线和圆)在 opencv 3 中,但不在旧版本中。

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.最后,我必须使用我的用户帐户授予对 /dev/video0(这是我的网络摄像头设备)的访问权限,并且错误消息消失了。 Use usermod to add user into group video使用 usermod 将用户添加到视频组

usermod -a -G video user

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.然后它起作用了。

Although this is an old thread, I got this error as well and the solution that worked for me is not mentioned here.虽然这是一个旧线程,但我也遇到了这个错误,这里没有提到对我有用的解决方案。

Simply put, in my case the webcam was still in use on the background, as I saw the LED light being on.简单地说,就我而言,网络摄像头仍在后台使用,因为我看到 LED 灯亮着。 I have not yet been able to reproduce the issue, so I'm not sure a simple cv2.VideoCapture(0).release() would have solved it.我还没有能够重现这个问题,所以我不确定一个简单的cv2.VideoCapture(0).release()会解决它。 I'll edit this post if and when I have found it out.如果我发现它,我会编辑这篇文章。

For me a restart of my PC solved the issue, without changing anything to the code.对我来说,重新启动我的电脑解决了这个问题,而无需对代码进行任何更改。

This Error can also occur if you slice a negative point and pass it to the array.如果将负点切片并将其传递给数组,也会发生此错误。 So check if you did所以检查你是否做了

I was facing the same problem while trying to open images containing spaces and special characters like the following ´ in their names我在尝试打开包含空格和特殊字符的图像时遇到了同样的问题,如名称中的以下´

So, after modifying the images names removing their spaces and special characters, everything worked perfectly.因此,在修改图像名称删除它们的空格和特殊字符后,一切正常。

Check if you have "opencv_ffmpeg330.dll" in python27 root directory or of the python version you are using.检查python27根目录或正在使用的 python 版本中是否有“opencv_ffmpeg330.dll” If not you will find it in "....\\OpenCV\\opencv\\build\\bin" .如果没有,您会在"....\\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安装的根目录下,重新运行程序

只需使用像.jpeg.png这样的图像扩展名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:函数imshow中的(-215)size.width&gt; 0 &amp;&amp; size.height&gt; 0 - error: (-215) size.width>0 && size.height>0 in function imshow OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'imshow' - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' opencv 错误:(-215:断言失败)size.width&gt;0 &amp;&amp; size.height&gt;0 in function 'cv::imshow' - opencv error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' 相机流 - OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'cv::imshow' - Camera Streaming - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' OpenCV(4.2.0) 错误: (-215:Assertion failed) size.width&gt;0 &amp;&amp; size.height&gt;0 in function 'cv::imshow' - OpenCV(4.2.0) error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' 相机校准opencv python imshow错误:(-215)size.width&gt; 0 &amp;&amp; size.height&gt; 0 - Camera Calibration opencv python imshow error: (-215) size.width>0 && size.height>0 错误:(-215:断言失败)函数“imshow”中的 size.width&gt;0 &amp;&amp; size.height&gt;0 - error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' 错误:(-215:断言失败)函数 cv::imshow 中的 size.width&gt;0 &amp;&amp; size.height&gt;0 - error: (-215:Assertion failed) size.width>0 && size.height>0 in function cv::imshow 错误:.. \\ .. \\ .. \\ opencv-2.4.8 \\ modules \\ highgui \\ src \\ window.cpp:269:错误:(-215)size.width&gt; 0 &amp;&amp; size.height&gt; 0在函数cv中: :imshow - error: ..\..\..\opencv-2.4.8\modules\highgui\src\window.cpp:269: error: (-215) size.width>0 && size.height>0 in function cv::imshow 断言失败:function imshow 中的 size.width&gt;0 &amp;&amp; size.height&gt;0 - Assertion failure : size.width>0 && size.height>0 in function imshow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM