image_path = 'image_06621.jpg' img = process_image(image_path) imshow(img)
上面的代码是执行对图像的展示的相关代码,但是出现了下面的报错:
NameError: name ‘imshow’ is not defined.
原因是没有导入matplotlib,在第一行加入:
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt image_path = 'image_06621.jpg' img = process_image(image_path) imshow(img)
然后又出现了下面的报错,没有画出相应的图像:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_12484/1515367799.py in <module> 2 image_path = 'image_06621.jpg' 3 img = process_image(image_path) ----> 4 imshow(img) D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\matplotlib\_api\deprecation.py in wrapper(*args, **kwargs) 454 "parameter will become keyword-only %(removal)s.", 455 name=name, obj_type=f"parameter of {func.__name__}()") --> 456 return func(*args, **kwargs) 457 458 # Don't modify *func*'s signature, as boilerplate.py needs it. D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\matplotlib\pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, interpolation_stage, filternorm, filterrad, resample, url, data, **kwargs) 2638 interpolation_stage=None, filternorm=True, filterrad=4.0, 2639 resample=None, url=None, data=None, **kwargs): -> 2640 __ret = gca().imshow( 2641 X, cmap=cmap, norm=norm, aspect=aspect, 2642 interpolation=interpolation, alpha=alpha, vmin=vmin, D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\matplotlib\_api\deprecation.py in wrapper(*args, **kwargs) 454 "parameter will become keyword-only %(removal)s.", 455 name=name, obj_type=f"parameter of {func.__name__}()") --> 456 return func(*args, **kwargs) 457 458 # Don't modify *func*'s signature, as boilerplate.py needs it. D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs) 1410 def inner(ax, *args, data=None, **kwargs): 1411 if data is None: -> 1412 return func(ax, *map(sanitize_sequence, args), **kwargs) 1413 1414 bound = new_sig.bind(ax, *args, **kwargs) D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, interpolation_stage, filternorm, filterrad, resample, url, **kwargs) 5486 **kwargs) 5487 -> 5488 im.set_data(X) 5489 im.set_alpha(alpha) 5490 if im.get_clip_path() is None: D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\matplotlib\image.py in set_data(self, A) 713 if not (self._A.ndim == 2 714 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]): --> 715 raise TypeError("Invalid shape {} for image data" 716 .format(self._A.shape)) 717 TypeError: Invalid shape (3, 224, 224) for image data
原因是图像的矩阵错误,错误提示了相应的解决办法,图像的第三个参数是颜色通道的个数,将其进行转置后就能画出相应的图像
import matplotlib.pyplot as plt image_path = 'image_06621.jpg' img = process_image(image_path) imshow(img.T)