Python 3.x TypeError:图像数据无法在plt.imshow()上转换为浮点
我试图从这个问题所附的深度图像中分割手。在这个过程中,我编写了下面的代码,首先处理图像的直方图,然后对处理后的图像进行中值滤波。但这段代码不断地给出错误,即使在努力解决它之后Python 3.x TypeError:图像数据无法在plt.imshow()上转换为浮点,python-3.x,image-processing,matplotlib,jupyter-notebook,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 ndima
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
plt.imshow(mod\u img)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
16 plt.show()
17模/模/模=图像中值滤波器(imgplot,20)
--->18 plt.imshow(mod_img)
19 plt.show()
C:\Users\Prachi\AppData\Local\Programs\Python36-32\Anaconda3\lib\site packages\matplotlib\pyplot.py在imshow中(X、cmap、norm、aspect、interpolation、alpha、vmin、vmax、origin、extent、shape、filternorm、filterrad、imlim、重采样、url、hold、data、**kwargs)
3155 filternorm=filternorm,filterrad=filterrad,
3156 imlim=imlim,重采样=重采样,url=url,数据=数据,
->3157**夸尔格)
3158最后:
3159 ax._hold=洗旧
C:\Users\Prachi\AppData\Local\Programs\Python36-32\Anaconda3\lib\site packages\matplotlib\ \uuuuuuu init\uuuuuuuu.py在内部(ax,*args,**kwargs)
1895警告。警告(消息%(标签名称,函数名称),
1896运行时警告,堆栈级别=2)
->1897返回函数(ax,*args,**kwargs)
1898预存单据=内部单据__
1899如果pre_doc为无:
C:\Users\Prachi\AppData\Local\Programs\Python36-32\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py在imshow中(self、X、cmap、norm、aspect、interpolation、alpha、vmin、vmax、origin、extent、shape、filternorm、filterrad、imlim、resample、url、**kwargs)
5122重采样=重采样,**kwargs)
5123
->5124即时设置数据(X)
5125 im.set_α(α)
5126如果im.get\u clip\u path()为无:
C:\Users\Prachi\AppData\Local\Programs\Python36-32\Anaconda3\lib\site packages\matplotlib\image.py in set_data(self,A)
594如果(自身类型)=np.uint8和
595非np.can_cast(self.\u A.dtype,np.float)):
-->596 raise TypeError(“图像数据无法转换为浮点”)
597
598如果(自身)不在(2,3)或
TypeError:图像数据无法转换为浮点
因为我不能附加图像,所以这里是图像的细节
图像(灰色)是数据集“Microsoft Kinect和Leap Motion”中的“1_depth.png”
您试图将图像过滤器应用于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)
根据我的经验,我认为它来自指定的路径。尝试缩短路径。将它放在它工作的同一文件夹中。 而不是C:/Users/Prachi/Desktop/kinect\u leap\u dataset/acquisitions/P1/G1/1\u depth.png 只需将图像放在G1文件夹中,并使其
G1/1_depth.png我在mpimg.imread和plt.imread上遇到了类似的问题。这两个问题都导致了类型错误 相反,我求助于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
哦!!谢谢你的解答:)真是个愚蠢的问题;)