gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:05:14 25 4
gpt4 key购买 nike

我有一张灰色图像列表。我用以下代码阅读它们:

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

现在,我正在尝试使用此功能调整 read_images中所有图像的大小:
def resize_images(img, new_width, new_height):
size = (new_width, new_height)
resized_img = cv2.resize(img, size)
return resized_img

我应用了如下功能:
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'

有人可以帮忙吗?
您需要什么进一步的信息?我已经将此功能应用于少量图像(约650张图像)的列表,并且可以正常工作。现在,该列表包含超过18万张图像。此外,图像的大小不同,但对于其他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)

关于python - 错误: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error: (-215:Assertion failed),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61630722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com