import numpy as np
import matplotlib.pyplot as plt
import cv2
def log(c, img):
output = c * np.log(1.0 + img)
output = np.uint8(output + 0.5)
return output
img = cv2.imread('./night.jpg')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img2)
plt.show()
output = log(33, img)
output2 = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
plt.imshow(output2)
plt.show()
grayHist = calcGrayHist(output)
x = np.arange(256)
plt.plot(x, grayHist, 'r', linewidth=2, c='black')
plt.xlabel("gray Label")
plt.ylabel("number of pixels")
plt.show()
out = 2.00 * output
out[out > 255] = 255
# 数据类型转换
out = np.around(out)
out = out.astype(np.uint8)
out2 = cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
plt.imshow(out2)
plt.show()
def calcGrayHist(I):
# 计算灰度直方图
h, w = I.shape[:2]
grayHist = np.zeros([256], np.uint64)
for i in range(h):
for j in range(w):
grayHist[I[i][j]] += 1
return grayHist
grayHist = calcGrayHist(out)
x = np.arange(0, 256, 2)
y = [grayHist[i] for i in range(len(grayHist)) if i % 2 == 0]
plt.plot(x, y, 'r', linewidth=2, c='black')
plt.xlabel("gray Label")
plt.ylabel("number of pixels")
plt.show()
import cv2
import numpy as np
#通道分解
(b,g,r) = cv2.split(out)
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
result = cv2.merge((bH,gH,rH),)#通道合成
# 并排放置原图和效果图
res = np.hstack((out,result))
res2 = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
plt.imshow(res2)
plt.show()