类型错误:无法使用matplotlib将图像数据转换为float

2024-04-24 15:04:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本程序,只需加载一个图像并将其打印在matplot中。 我用“histeq”中的方法均衡这个图像(我知道skimage函数),当我运行它时,它会给出以下“TypeError:图像数据不能转换成float”

import matplotlib
import matplotlib.pyplot as plt
from numpy import histogram, cumsum, interp, array
import numpy as np
from PIL import Image
from skimage import data, img_as_float
from skimage import exposure

def histeq(im,nbr_bins=256):

   #get image histogram
   imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
   cdf = imhist.cumsum() #cumulative distribution function
   cdf = 255 * cdf / cdf[-1] #normalize

   #use linear interpolation of cdf to find new pixel values
   im2 = interp(im.flatten(),bins[:-1],cdf)
   print(cdf.size)
   return im2.reshape(im.shape), cdf

fig = plt.figure(figsize=(8, 5))

img = array (Image.open('AquaTermi_lowcontrast.jpg').convert('L'))

img = histeq(img)

#img = img_as_float(img)

axes_img = fig.add_subplot(2, 2, 2)
axes_img.set_axis_off()
axes_img.imshow(img, cmap=plt.cm.gray=-)


plt.show()

我不使用直方图均衡化,因为我不使用二元均衡化工具。 基本上就是用直方图来分离图像,并对两部分进行均衡处理。其结果是两部分的结合。在

不管怎样,这个错误与大代码类似,所以我发布这个只是为了举例。 控制台显示: Error message


Tags: from图像importimgmatplotlibaspltfloat