我目前遇到一个非常奇怪的问题,我找不到解释。我有一个包含文件名列表的文本文件。想法是读取此文本文件,并通过读取来一张一张地读取对应的图像。但是会发生一些非常奇怪的事情,例如当我尝试通过手动输入列表的第一个路径来打开图像时,它可以工作,但是当我读取列表文件时,它就无法工作。我进行的测试会更加清楚:
entepath='INRIAPerson'
line = 'Train/pos/crop_000010.png'
plt.figure()
img = cv2.imread(os.path.join(path, line), cv2.IMREAD_UNCHANGED)
plt.imshow(img)
with open(os.path.join(path, 'Train', 'pos.lst'), 'r') as f:
for line in f:
print(line)
plt.figure()
img = cv2.imread(os.path.join(path, line), cv2.IMREAD_UNCHANGED)
plt.imshow(img)
break
输出是这样的:
Train/pos/crop_000010.png
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-6de28785d7e8> in <module>
9 plt.figure()
10 img = cv2.imread(os.path.join(path, line), cv2.IMREAD_UNCHANGED)
---> 11 plt.imshow(img)
12 break
~/anaconda3/envs/pytorch/lib/python3.7/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, **kwargs)
2699 filternorm=filternorm, filterrad=filterrad, imlim=imlim,
2700 resample=resample, url=url, **({"data": data} if data is not
-> 2701 None else {}), **kwargs)
2702 sci(__ret)
2703 return __ret
~/anaconda3/envs/pytorch/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
~/anaconda3/envs/pytorch/lib/python3.7/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
5492 resample=resample, **kwargs)
5493
-> 5494 im.set_data(X)
5495 im.set_alpha(alpha)
5496 if im.get_clip_path() is None:
~/anaconda3/envs/pytorch/lib/python3.7/site-packages/matplotlib/image.py in set_data(self, A)
640 if (self._A.dtype != np.uint8 and
641 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 642 raise TypeError("Image data cannot be converted to float")
643
644 if not (self._A.ndim == 2
TypeError: Image data cannot be converted to float
重要的是要注意,第一个图像已绘制(因此已加载),而第二个图像未绘制。我不知道为什么在第二种情况下它不起作用,可能是多个文件打开的问题?
谢谢!