繁体   English   中英

错误:OpenCV(4.2.0)C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045:错误:(-215:断言失败)

[英]error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error: (-215:Assertion failed)

我有一个灰度图像列表。 我用这段代码读了它们:

import glob
import cv2


folders = glob.glob(r'path\to\images\*')
imagenames_list = []
for folder in folders:
    for f in glob.glob(folder+'/*.png'):
        imagenames_list.append(f)

read_images = []        

for image in imagenames_list:
    read_images.append(cv2.imread(image, cv2.IMREAD_GRAYSCALE))

现在,我正在尝试使用此 function 调整read_images中所有图像的大小:

def resize_images(img, new_width, new_height):
    size = (new_width, new_height)
    resized_img = cv2.resize(img, size)
    return resized_img

我应用了 function 如下:

resized_img = [resize_images(img, new_width=128, new_height=32) for img in read_images]

Python 返回此错误:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-21-9352534280fd> in <module>
      1 #Anwenden der Funktion auf die Liste col_image
----> 2 resized_img = [resize_images(img, new_width=128, new_height=32) for img in read_images]

<ipython-input-21-9352534280fd> in <listcomp>(.0)
      1 #Anwenden der Funktion auf die Liste col_image
----> 2 resized_img = [resize_images(img, new_width=128, new_height=32) for img in read_images]

<ipython-input-20-e4d9e7d9b2fa> in resize_images(img, new_width, new_height)
      2 def resize_images(img, new_width, new_height):
      3     size = (new_width, new_height)
----> 4     resized_img = cv2.resize(img, size)
      5     return resized_img

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

有人可以帮忙吗? 您需要什么进一步的信息? 我已经将此 function 应用于较少数量的图像(大约 650 个图像)的列表并且它有效。 现在,这个列表包含超过 180k 的图像。 此外,图像的大小不同,但其他 650 张图像的大小也不同。

试试这个以找出哪里出错了。 希望它会告诉您哪个文件使循环崩溃。

import glob
import cv2
from cv2 import error


def resize_images(img_, new_width, new_height):
    size = (new_width, new_height)
    resized_img_ = cv2.resize(img_, size)
    return resized_img_


folders = glob.glob(r'path\to\images\*')
img_names_list = []
read_imgs = []
resized_imgs = []
for folder in folders:
    for file in (glob.glob(folder+'/*.png')):
        img_names_list.append(file)
        img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
        read_imgs.append(img)
        try:
            resized_img = resize_images(img, new_width=128, new_height=32)
            resized_imgs.append(resized_img)
            # print(file, img, resized_img)
        except error as err:
            print(f'{file} returns {img}. Resize does not work')
            print(err)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM