Python与图像处理(2):灰度变换,直方图均衡化

来源:互联网 发布:如何与室友相处知乎 编辑:程序博客网 时间:2024/05/01 18:41

本实验主要实现:对图像进行反相处理,将图像像素值变换到100…200区间,对图像像素值求平方后得到图像,对图像进行直方图均衡化处理

import tkFileDialogfrom PIL import Imagefrom numpy import *from pylab import *import imtoolsfilename = tkFileDialog.askopenfilename(initialdir='E:\experimentcode\Python\stripenoise')#读入图片im = Image.open(filename)#读取图像到数组中imA = array(im.convert('L'))imB = 255-imA #对图像进行反相处理imC = (100.0/255)*imA+100  #将图像像素值变换到100...200区间imD = 255.0*(imA/255.0)**2#对图像像素值求平方后得到的图像figure()subplot(2,2,1)imshow(imA)subplot(2,2,2)imshow(imB)subplot(2,2,3)imshow(imC)subplot(2,2,4)imshow(imD)#直方图均衡化im1,cdf = imtools.histeq(imA)figure()subplot(121)imshow(im1)subplot(122)plot(cdf)show()imtools.pyfrom PIL import Imagedef histeq(im,nbr_bins=256):    """对一幅灰度图像进行直方图均衡化"""    #计算图像的直方图    imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)    cdf = imhist.cumsum()#计算分布函数    cdf = 255*cdf/[-1]#归一化    #使用累计分布函数的线性插值,计算新的像素值    im2 = interp(im.flatten(),bins[:-1],cdf)    return im2.reshape(im.shape),cdf

实验结果
这里写图片描述

这里写图片描述
参考《Python计算机视觉》

0 0
原创粉丝点击