TypeError:图像数据无法在plt.imshow()上转换为float

时间:2017-06-10 18:37:16

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

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

    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/

4 个答案:

答案 0 :(得分:0)

您正尝试将图像过滤器应用于matplotlib图。这不应该奏效。 plt.imshow()返回matplotlib.image.AxesImage,这是一个复杂的类,因此无法转换为float。

相反,您当然应该将滤镜应用于图像本身。

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

答案 1 :(得分:0)

根据我的经验,我认为这是从指定的路径出发的。尝试缩短路径。将其放在有效的同一文件夹中。 代替C:/Users/Prachi/Desktop/kinect_leap_dataset/acquisitions/P1/G1/1_depth.png 只需将图像放在G1文件夹中并制作 G1 / 1_depth.png

答案 2 :(得分:0)

只需将matplotlib升级为

pip install --upgrade matplotlib 

一切都会正常工作。

答案 3 :(得分:0)

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