【问题标题】:TypeError: Image data can not convert to float on plt.imshow()TypeError:图像数据无法在 plt.imshow() 上转换为浮点数
【发布时间】:2017-11-12 14:13:57
【问题描述】:

我正在尝试从这个问题中附加的深度图像中分割手。在此过程中,我编写了以下代码,该代码首先处理图像的直方图,然后对处理后的图像进行中值滤波。但是即使在努力解决它之后,这段代码也会不断地出错。

    import numpy as np
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import scipy
    from scipy import ndimage, misc
    from scipy.misc import imshow
    import skimage
    img = mpimg.imread('C:/Users/Prachi/Desktop/kinect_leap_dataset/acquisitions/P1/G1/1_depth.png')
    plt.hist(img.ravel(), bins=256, range=(0.0, 1.0))
    plt.show()
    imgplot = plt.imshow(img, clim=(0.064, 0.068))
    #plt.axis('off')
    plt.show()
    mod_img = ndimage.median_filter(imgplot, 20)
    plt.imshow(mod_img)
    plt.show()

这是我得到的错误。请帮助解决此错误。我已经检查了有关此错误的每个线程,但未能成功解决它。 似乎代码正在执行中值滤波,但由于在线发生错误而无法显示图像:

plt.imshow(mod_img)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-129-8482175fdf0e> in <module>()
     16 plt.show()
     17 mod_img = ndimage.median_filter(imgplot, 20)
---> 18 plt.imshow(mod_img)
     19 plt.show()

C:\Users\Prachi\AppData\Local\Programs\Python\Python36-32\Anaconda3\lib\site-packages\matplotlib\pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, data, **kwargs)
   3155                         filternorm=filternorm, filterrad=filterrad,
   3156                         imlim=imlim, resample=resample, url=url, data=data,
-> 3157                         **kwargs)
   3158     finally:
   3159         ax._hold = washold

C:\Users\Prachi\AppData\Local\Programs\Python\Python36-32\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1895                     warnings.warn(msg % (label_namer, func.__name__),
   1896                                   RuntimeWarning, stacklevel=2)
-> 1897             return func(ax, *args, **kwargs)
   1898         pre_doc = inner.__doc__
   1899         if pre_doc is None:

C:\Users\Prachi\AppData\Local\Programs\Python\Python36-32\Anaconda3\lib\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)
   5122                               resample=resample, **kwargs)
   5123 
-> 5124         im.set_data(X)
   5125         im.set_alpha(alpha)
   5126         if im.get_clip_path() is None:

C:\Users\Prachi\AppData\Local\Programs\Python\Python36-32\Anaconda3\lib\site-packages\matplotlib\image.py in set_data(self, A)
    594         if (self._A.dtype != np.uint8 and
    595                 not np.can_cast(self._A.dtype, np.float)):
--> 596             raise TypeError("Image data can not convert to float")
    597 
    598         if (self._A.ndim not in (2, 3) or

TypeError: Image data can not convert to float

由于我无法附上图片,所以这里是图片的详细信息。

图像(灰色)是来自数据集“Microsoft Kinect and Leap Motion”的“1_depth.png”,位于

http://lttm.dei.unipd.it/downloads/gesture/

【问题讨论】:

    标签: python-3.x image-processing matplotlib jupyter-notebook


    【解决方案1】:

    您正在尝试将图像过滤器应用于 matplotlib 图。那不应该工作。 plt.imshow() 返回一个 matplotlib.image.AxesImage,这是一个复杂的类,因此不能转换为浮点数。

    您当然应该将过滤器应用于图像本身。

    img = ...
    plt.imshow(img, clim=(0.064, 0.068))
    mod_img = ndimage.median_filter(img, 20)
    plt.imshow(mod_img)
    

    【讨论】:

    • 哦!感谢这个解决方案:) 这么愚蠢的问题 ;)
    【解决方案2】:

    根据我的经验,我认为它来自指定的路径。尝试缩短路径。把它放在它工作的同一个文件夹中。 而不是 C:/Users/Prachi/Desktop/kinect_leap_dataset/acquisitions/P1/G1/1_depth.png 只需将图像放入G1文件夹并制作它 G1/1_depth.png

    【讨论】:

      【解决方案3】:

      我在使用 mpimg.imread 和 plt.imread 时遇到了类似的问题。两者都引发了 TypeError。

      相反,我求助于有效的 cv2.imread:

      import cv2 as cv
      
      im_plt = plt.imread("kangaroo/images/00090.jpg")
      im_cv = cv.imread("kangaroo/images/00090.jpg")[:, :, ::-1] #OpenCV users BGR instead of RGB
      
      plt.axis("off")
      
      #plt.imshow(im_plt) # does not work due to TypeError: Image data of dtype object cannot be converted to float
      plt.imshow(im_cv) # works
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        • 2023-03-30
        • 1970-01-01
        相关资源
        最近更新 更多