-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNormEqua.py
60 lines (46 loc) · 1.82 KB
/
NormEqua.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import matplotlib.pyplot as plt
import numpy as np
import cv2
from PIL import Image
# ----------------------------- Normalization function -------------------------
def normalize(img):
lmin = float(img.min())
lmax = float(img.max())
return np.floor((img-lmin)/(lmax-lmin)*225.0)
# ----------------------------- Equalization function --------------------------
def equalize(img_filename,save_filename):
#load file as pillow Image
img = Image.open(img_filename)
# convert to grayscale
imgray = img.convert(mode='L')
#convert to NumPy array
img_array = np.asarray(imgray)
"""
STEP 1: Normalized cumulative histogram
"""
#flatten image array and calculate histogram via binning
histogram_array = np.bincount(img_array.flatten(), minlength=256)
#normalize
num_pixels = np.sum(histogram_array)
histogram_array = histogram_array/num_pixels
#normalized cumulative histogram
chistogram_array = np.cumsum(histogram_array)
"""
STEP 2: Pixel mapping lookup table
"""
transform_map = np.floor(255 * chistogram_array).astype(np.uint8)
"""
STEP 3: Transformation
"""
# flatten image array into 1D list
img_list = list(img_array.flatten())
# transform pixel values to equalize
eq_img_list = [transform_map[p] for p in img_list]
# reshape and write back into img_array
eq_img_array = np.reshape(np.asarray(eq_img_list), img_array.shape)
######################################
# WRITE EQUALIZED IMAGE TO FILE
######################################
#convert NumPy array to pillow Image and write to file
eq_img = Image.fromarray(eq_img_array, mode='L')
eq_img.save(save_filename)