【问题标题】:Assertion failure : size.width>0 && size.height>0 in function imshow断言失败:函数 imshow 中的 size.width>0 && size.height>0
【发布时间】:2015-10-13 16:49:31
【问题描述】:

我在树莓派上使用 opencv2 和 python。我是python和opencv的新手。我试图读取 jpeg 图像并显示图像它显示以下错误:

/home/pi/opencv-2.4.9/modules/highgui/src/window.cpp:269: \
  error: (-215) size.width>0 &&  size.height>0 in function imshow.

代码是:

import cv2
# windows to display image
cv2.namedWindow("Image")
# read image
image = cv2.imread('home/pi/bibek/book/test_set/bbb.jpeg')
# show image
cv2.imshow("Image", image)
# exit at closing of window
cv2.waitKey(0)
cv2.destroyAllWindows()

【问题讨论】:

  • 您的图片为空。看here
  • 在imread后添加if image.empty print a warning that image could not be loaded, probably because of wrong path试试"/home/pi/bibek/book/test_set/bbb.jpeg"
  • @Dan,你真的认为 C++ 的答案在这里会有帮助吗?
  • @BhargavRao 问题是一样的(用户试图显示一个空图像)和解决方案也是一样的(在尝试显示它之前检查它不是空的)。每种语言都有一个问题似乎有点过分。

标签: python opencv raspberry-pi


【解决方案1】:

图片加载失败(可能是因为您忘记了路径中的前导/)。 imread 然后返回无。将None 传递给imshow 会导致它尝试创建大小为0x0 的窗口,但失败了。

cv 中糟糕的错误处理可能是由于其在 C++ 实现中的非常薄的包装层(在错误时返回 NULL 是一种常见做法)。

【讨论】:

    【解决方案2】:

    这是导致问题的路径,我遇到了同样的问题,但是当我给出图像的完整路径时,它工作得很好。

    【讨论】:

      【解决方案3】:

      在 Rpi 3 中使用 Raspbian 时,我在尝试读取 qrcode 时遇到了同样的问题。该错误是因为 cv2 无法读取图像。如果使用 png 图片安装 pypng 模块。

      sudo pip install pypng
      

      【讨论】:

        【解决方案4】:

        在您指定文件地址的代码中使用 r
        例如:

        import cv2
        img = cv2.imread(r'D:\Study\Git\OpenCV\resources\lena.png')
        cv2.imshow('output', img)
        cv2.waitKey(0)
        

        r 代表“原始”,会导致字符串中的反斜杠被解释为实际的反斜杠而不是特殊字符。

        【讨论】:

          【解决方案5】:

          就我而言,我忘记将终端的工作目录更改为我的代码+testImage 的工作目录。因此,它未能在那里找到图像。

          最后,这对我有用:

          我将图像和 Python 文件保存在桌面上。我把我的cmd目录改成了它,

          cd Desktop
          

          然后检查我的文件:

          ls
          

          这是我的有效代码:

          import cv2
          import numpy as np
          
          im = cv2.imread('unnamed.jpg')
          #Display the image
          cv2.imshow('im',im)
          cv2.waitKey(2000) #Milliseconds
          

          【讨论】:

            【解决方案6】:

            我也遇到了类似的错误,所以我认为与其打开一个新问题,不如在这里收集所有问题,因为已经有一些有用的答案......

            我的代码(在 Python 中使用 OpenCV 打开视频的教科书代码):

            import cv2 as cv
            import os
            
            path = 'C:/Users/username/Google Drive/Master/THESIS/uva_nemo_db/videos/'
            os.chdir(path)
            video_file = '001_deliberate_smile_2.mp4'
            cap = cv.VideoCapture(video_file) 
            
            
            if not cap.isOpened():
                print("Error opening Video File.")
            
            while True:
                # Capture frame-by-frame
                ret, frame = cap.read()
                cv.imshow('frame',frame)
            
                if cv.waitKey(1) & 0xFF == ord('q'):
                    break
            
                # if frame is read correctly, ret is True
                if not ret:
                    print("Can't retrieve frame - stream may have ended. Exiting..")
                    break
            
            # When everything done, release the capture
            cap.release()
            cv.destroyAllWindows()
            

            我目瞪口呆的原因是我遇到了同样的错误——但是——视频实际上是在播放的……当运行代码时,Python 解释器会打开一个运行视频的 Python 实例。一旦视频结束,它就会跳出循环,关闭视频并抛出错误:

            Traceback(最近一次调用最后一次): 文件“C:/Users/username/Documents/smile-main/video-testing.py”,第 24 行,在 cv.imshow('frame',frame) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wwma2wne\opencv\modules\highgui\src\window.cpp:376: 错误: ( -215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

            如有任何意见,我将不胜感激!

            **

            编辑:我是如何修复错误的!

            **

            我将我的代码封装在 try/except 中,如下所示:

            # Import required libraries
            import cv2 as cv
            import os
            
            path = 'C:/Users/username/Google Drive/Master/THESIS/uva_nemo_db/videos/'
            # test_path = 'C:/Users/username/Downloads'
            os.chdir(path)
            os.getcwd()
            video_file = '001_deliberate_smile_2.mp4'
            
            cap = cv.VideoCapture(video_file) #cap for "Video Capture Object"
               
            
            if not cap.isOpened():
                print("Error opening Video File.")
            try:
                while True:
                    # Capture frame-by-frame
                    ret, frame = cap.read()
                    cv.imshow('frame',frame)
            
                    if cv.waitKey(1) & 0xFF == ord('q'):
                        break
            
                    # if frame is read correctly, ret is True
                    if not ret:
                        print("Can't retrieve frame - stream may have ended. Exiting..")
                        break
            except:
                print("Video has ended.")
            
            
            # When everything done, release the capture
            cap.release()
            cv.destroyAllWindows()
            

            如果视频播放正常,为什么会弹出这个错误,以及为什么 try/except 消除了它,我仍然很感激。

            谢谢!

            【讨论】:

            • 好吧...虽然我不是 100% 因为这个修复的原因,我可以告诉你我做了什么阻止了这个错误的弹出,如果你可以 - 你可以解释一下我。我将代码封装在 try/except 中,将 try: 放在 while True: 语句之前,并将 except: 放在 break 之后。
            【解决方案7】:

            导致此错误的原因之一是指定路径下没有文件。因此,一个好的做法是验证这样的路径(如果您使用的是基于 linux 的机器):

            ls <path-provided-in-imread-function>
            

            如果路径不正确或文件丢失,您将收到错误消息。

            【讨论】:

              【解决方案8】:

              在读取图像文件时,指定颜色选项应该可以解决这个问题, 例如:

              image=cv2.imread('img.jpg',cv2.IMREAD_COLOR)
              

              添加cv2.IMREAD_COLOR 应该可以解决这个问题

              【讨论】:

                【解决方案9】:

                这个问题发生在我刚刚未能写入图像的扩展名时。
                请检查您是否忘记写扩展名或图像完整路径的任何其他部分。

                请记住,无论您是使用 OpenCV 还是 Mathplotlib 打印图像,都需要扩展。

                【讨论】:

                  【解决方案10】:

                  我用这段代码解决了

                      os.chdir(f"{folder_path}")
                  

                  【讨论】:

                    【解决方案11】:

                    这是因为图像没有加载。在 VScode 对我来说,相对路径是个问题,但是从 VSCode 本身复制文件路径后,问题就解决了。

                    【讨论】:

                      【解决方案12】:

                      在 VSCode 上我也遇到了同样的问题。尝试在 Notepad++ 上运行相同的代码并且它有效。要在 VSCode 上解决此问题,请不要忘记在左侧窗格中打开您正在使用的文件夹。这解决了我的问题。

                      【讨论】:

                        猜你喜欢
                        • 2014-01-16
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2019-07-24
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多