import numpy as np
class RMPOGM:
'''
基于均值灰度级得变换
'''
def __init__(self, image):
'''
读取图像
'''
self.image = image
for i in range(self.image.get_height()):
for j in range(self.image.get_width()):
if self.image.get_v_mat()[i][j] == 127:
self.image.get_v_mat()[i][j] = 128
pass
def get_InputHistogram(self):
vMat = self.image.get_v_mat()
height = self.image.get_height()
width = self.image.get_width()
tt = height * width
number = 256
v_density = []
v_amount = []
v_gray = []
for i in range(number):
t = np.sum(vMat == i)
d = t / tt
v_density.append(d)
v_gray.append(i)
v_amount.append(t)
s = []
for i in range(len(v_amount)):
if v_amount[i] != 0:
s.append(i)
return v_amount, v_density, v_gray, height, width, s
def get_InputAvg(self, v_den, v_gray):
'''
获取图像v通道的均值
'''
r_mean = np.sum(np.array(v_den) * np.array(v_gray))
return r_mean
pass
def get_H(self, r_mean, v_amount, v_gray):
'''
计算r_meanl,r_meanu
将直方图根据均值划分为H=【HLl,HLu,HUl,HUu】
'''
r_mean = int(r_mean)
r_meanl = 0
r_meanu = 0
L = 256
v1_size = 0
for i in range(0, r_mean + 1):
v1_size = v1_size + v_amount[i]
r_meanl = r_meanl + np.array(v_amount[i]) * np.array(v_gray[i])
r_meanl = int(r_meanl / v1_size)
v2_size = 0
for i in range(r_mean + 1, L):
v2_size = v2_size + v_amount[i]
r_meanu = r_meanu + np.array(v_amount[i]) * np.array(v_gray[i])
r_meanu = int(r_meanu / v2_size)
return r_meanl, r_mean, r_meanu
pass
def get_C(self, v_amount, r_meanl, r_mean, r_meanu):
'''
计算非零频率灰度级数
'''
L = 256
C_Ll = 0
C_Lu = 0
C_Ul = 0
C_Uu = 0
Alpha_Ll = 0
Alpha_Lu = 0
Alpha_Ul = 0
Alpha_Uu = 0
for i in range(r_meanl + 1):
if v_amount[i] != 0:
C_Ll = C_Ll + 1
Alpha_Ll = np.float((1 + r_meanl) / C_Ll)
for i in range(r_meanl + 1, r_mean + 1):
if v_amount[i] != 0:
C_Lu = C_Lu + 1
Alpha_Lu = np.float((r_mean - r_meanl) / C_Lu)
for i in range(r_mean + 1, r_meanu + 1):
if v_amount[i] != 0:
C_Ul = C_Ul + 1
Alpha_Ul = np.float((r_meanu - r_mean) / C_Ul)
for i in range(r_meanu + 1, L):
if v_amount[i] != 0:
C_Uu = C_Uu + 1
Alpha_Uu = np.float((L - 1 - r_meanu) / C_Uu)
return Alpha_Ll, Alpha_Lu, Alpha_Ul, Alpha_Uu, C_Ll, C_Lu, C_Ul, C_Uu
pass
def get_MapGray(self, r_meanl, r_mean, r_meanu,alpha1, alpha2, alpha3, alpha4, C_Ll, C_Lu, C_Ul, C_Uu):
'''
计算最后经过映射后输出的灰度级s
'''
s_Ll = np.zeros(C_Ll)
s_Lu = np.zeros(C_Lu)
s_Ul = np.zeros(C_Ul)
s_Uu = np.zeros(C_Uu)
for j in range(C_Ll):
s_Ll[j]= int ((j+1) * alpha1)
s_Ll=s_Ll.astype(np.uint8)
for j in range(C_Lu):
s_Lu[j] =int(r_meanl+(j + 1) * alpha2)
s_Lu = s_Lu.astype(np.uint8)
for j in range(C_Ul):
s_Ul[j] = int(r_mean+(j + 1) * alpha3)
s_Ul = s_Ul.astype(np.uint8)
for j in range(C_Uu):
s_Uu[j] = int(r_meanu+(j + 1) * alpha4)
s_Uu = s_Uu.astype(np.uint8)
s = list(s_Ll)+list(s_Lu)+list(s_Ul)+list(s_Uu)
return s
pass
```python
from OneToOneMap import RMPOGM
from util import Image
import numpy as np
import cv2 as cv
def init_RMPOGM(image):
Map = RMPOGM(image)
v_amount, v_density, v_gray, height, width, s_old = Map.get_InputHistogram()
r_avg = Map.get_InputAvg(v_density, v_gray)
r_meanl, r_mean, r_meanu = Map.get_H(r_avg, v_amount, v_gray)
alpha1, alpha2, alpha3, alpha4, C_Ll, C_Lu, C_Ul, C_Uu = Map.get_C(v_amount, r_meanl, r_mean, r_meanu)
s = Map.get_MapGray(r_meanl, r_mean, r_meanu,alpha1, alpha2, alpha3, alpha4, C_Ll, C_Lu, C_Ul, C_Uu)
s = np.array(s)
s_old = np.array(s_old)
temp = image.get_v_mat()
cv.imshow("orignal_img", temp)
for i in range(image.get_height()):
for j in range(image.get_width()):
if image.get_v_mat()[i][j] in s_old:
image.get_v_mat()[i][j] = s[np.argwhere(s_old == image.get_v_mat()[i][j])[0][0]]
print(image.get_v_mat())
temp = image.get_v_mat()
hsv_result_img = cv.merge([image.get_h_mat(),image.get_s_mat(),temp])
bgr_result_img=cv.cvtColor(hsv_result_img,cv.COLOR_HSV2BGR)
cv.imshow("v_img",temp)
finally_img = bgr_result_img
cv.imshow("finally_img", finally_img)
cv.waitKey()
cv.destroyAllWindows()
pass
def run(path, pathName):
image = Image(path)
init_RMPOGM(image)
if __name__ == '__main__':
t = "picturedataset/datasetLcenter/"
for i in range(1):
name = str(17 + 1)
path = t + name + ".jpg"
run(path, name)
|