NameError: name ‘imshow’ is not defined. 和TypeError: Invalid shape (3, 224, 224) for image data
生活随笔
收集整理的這篇文章主要介紹了
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 == 2714 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)總結
以上是生活随笔為你收集整理的NameError: name ‘imshow’ is not defined. 和TypeError: Invalid shape (3, 224, 224) for image data的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 照片怎么识别文字?有什么软件可以拍照识别
- 下一篇: 会计准则中计算机软件费用摊销,财务软件累