NameError: name ‘imshow’ is not defined. 和TypeError: Invalid shape (3, 224, 224) for image data

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)

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙有肥鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值