图像处理的Python问题,怎么解决

2025-02-27 08:24:47
推荐回答(1个)
回答1:

imtools.py里面也要有numpy 的引用才对
def histeq(im,nbr_bins=256):
"""对一幅灰度图像进行直方图均衡化"""

#计算图像的直方图
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() #累计分布函数
cdf = 255 * cdf / cdf[-1] #归一化

#使用累计分布函数的线性插值,计算新的像素
im2 = interp(im.flatten(),bins[:-1],cdf)

return im2.reshape(im.shape),cdf

以上代码我定义在imtools.py文件里并且放在了python2.7里

然后我在num.py里引用他

Python code?
1
2
3
4
5
6
7
8
9
10

from PIL import Image
from pylab import *
from numpy import *
import imtools

im= array(Image.open('E:\\daima\\pydaima\\shijue\\tupian1\\gang2.jpg').convert('L'))

im2,cdf =imtools.histeq(im)

出现以下错误:
Traceback (most recent call last):
File "", line 1, in
a=imtools.histeq(im)
File "E:\daima\pydaima\shijue\imtools.py", line 32, in histeq
NameError: global name 'histogram' is not defined