如何将均衡后的直方图转换为图像并保存转换后的图像?

时间:2018-06-28 05:33:57

标签: python python-3.x

我输入了图像,计算出图像的直方图。然后我对直方图进行均衡,并将均衡后的直方图保留在y处,现在我想再次将其转换为图像。我怎么能够?这是我的代码,我被困在最后的绘图中……

import matplotlib.image as mpImg
import numpy as np
import matplotlib.pyplot as plt

img = mpImg.imread('TestImage.jpeg',0)
hist,bins = np.histogram(img.flatten(),256,[0,256])
print ("Input Histogram: ",hist)

pdf=hist/np.sum(hist)
cdf = np.cumsum(pdf)

#Transformation Function
tf = np.round(255*cdf)

print ("PDF:-",pdf)
print ("CDF:-",cdf)
print ("Transformed Values:-",tf)

y=[0]*256
c=[0]*256
#Counting the frequency of values in tf
for r in range(256):
    for s in range(256):
        if(s==tf[r]):
            c[s]+=1

#print(c)
#Calculating final Histogram
sum=0
for r in range(256):
    for s in range(256):
        if(c[s]==0):
            y[s]=0
        else:
            for i in range(256):
                if(s==tf[i]):
                    sum+=hist[i]
            y[s]=sum
            sum=0

print("Output Histogram: ",y)
plt.subplot(2,2,1)
plt.title('Original')
plt.plot(hist)

plt.subplot(2,2,2)
plt.title('After')
plt.plot(y,color='b')

plt.subplot(2,2,3)
plt.title('Original')
plt.imshow(img)

plt.subplot(2,2,4)
plt.title('After')
#?????????

0 个答案:

没有答案