Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Image data can not convert to float on plt.imshow()

I am trying to segment hand from the depth image attached in this question. In the process, I wrote the below code which process the histogram of the image first and then does median filtering on the processed image. But this code is giving an error continuously even after trying hard to solve it.

    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()

This is the error I am getting. Kindly help in solving this error. I have checked each and every thread regarding this error, but was not successful in solving it. It seems that the code is doing median filtering but is not able to display the image as the error is occuring on the line:

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

As I cannot attach image so here are the details of the image.

The image (gray) is the '1_depth.png' from the dataset "Microsoft Kinect and Leap Motion" at

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

like image 268
Prachi Avatar asked Oct 16 '25 17:10

Prachi


1 Answers

You are trying to apply an image filter to a matplotlib plot. That is not supposed to work. plt.imshow() returns an matplotlib.image.AxesImage, which is a complex class an as such cannot be converted to float.

Instead you should of course apply the filter to the image itself.

img = ...
plt.imshow(img, clim=(0.064, 0.068))
mod_img = ndimage.median_filter(img, 20)
plt.imshow(mod_img)
like image 177
ImportanceOfBeingErnest Avatar answered Oct 19 '25 09:10

ImportanceOfBeingErnest