【发布时间】:2020-05-06 08:29:34
【问题描述】:
我有一个灰度图像列表。我用这段代码读了它们:
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 个图像)并且它有效。现在,这个列表包含超过 180k 的图像。此外,图像的大小不同,但其他 650 张图像的大小也不同。
【问题讨论】:
-
您可以运行循环并将
print(size)添加到您的resize_images函数中吗?它打印什么? -
感谢您的回答!我将代码更改为
import cv2 def resize_images(img, new_width, new_height): size = (new_width, new_height) resized_img = cv2.resize(img, size) print(size) return resized_img。现在,它会打印 (128, 32) 多次,直到它在某个点停止并且我再次遇到相同的错误。 -
我明白了。它断裂的图像是什么样的?尝试同时打印图像
shape。print(img.shape). -
好的,我也让函数打印img.shape。现在我得到
AttributeError: 'NoneType' object has no attribute 'shape'。似乎任何地方都有一个 NoneType 对象。我如何确定它在哪里? -
好的,这就是我认为正在发生的事情。当您从文件中加载图像时,其中一些可能不是图像。当您在这些文件上运行
cv2.imread(image, cv2.IMREAD_GRAYSCALE)时,它会返回None。我建议你print(f)看看是否一切正常。
标签: python list image opencv error-handling