Note: histogram equalization by scipy

click here to see source code 
# Source code
import numpy as np
from scipy import misc as mc
import matplotlib.pyplot as plt

def getHist(img):
	h = np.zeros(256)
	for i in img.flatten():
		h[i] = h[i] + 1
	return h

def normalization_histogram(h, t):
	for i in range(256):
		h[i] = h[i]/t
	return h

def getCDF(h):
	cdf = np.zeros(256)
	for i in range(256):
		cdf[i] = sum(h[0:i])
	return cdf

lena = mc.lena()
# to get image height & witdh
x, y = lena.shape
# get total pixel
t = x*y

f, axarr = plt.subplots(2,3)
axarr[0, 0].imshow(lena)
axarr[0, 0].set_title('original image')

hist = getHist(lena)
hist = normalization_histogram(hist, t)
axarr[0, 1].bar(range(256), hist)
axarr[0, 1].set_xlim([0,255])
axarr[0, 1].set_title('historam')

cdp = getCDF(hist)

axarr[0, 2].plot(np.linspace(0,1,256),cdp)
axarr[0, 2].set_title('CDF')

# histogram equalization
nlena = np.zeros([x,y], dtype=np.uint8)
for i in range(x):
	for j in range(y):
		t = cdp[lena[i,j]]*255

axarr[1, 0].imshow(nlena)
axarr[1, 0].set_title('After equlazation')

# re-calc new lena
x, y = nlena.shape
t = x*y
hist = getHist(nlena)
hist = normalization_histogram(hist, t)

axarr[1, 1].bar(range(256), hist)
axarr[1, 1].set_xlim([0,255])
cdf = getCDF(hist)
axarr[1, 2].plot(np.linspace(0,1,256),cdf)




一個回應 to “Note: histogram equalization by scipy”

  1. new version getCDF function, should fastest

    def getCDF(h):
    	cdf = np.zeros(256)
    	cdf[0] = h[0]
    	for i in range(1,256):
    		cdf[i] = cdf[i-1] + h[i]
    	return cdf


在下方填入你的資料或按右方圖示以社群網站登入: Logo

您的留言將使用 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )


您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

%d 位部落客按了讚: