# 0416midterm ### Data 讀取期中考提供的圖片,4張386*382大小,分別為過暗、過亮、灰階、普通。 ``` import cv2 import matplotlib.pyplot as plt import numpy as np #cv2讀取圖片 dark = cv2.imread('dark.PNG') bright = cv2.imread('bright.PNG') gray = cv2.imread('gray.PNG') normal = cv2.imread('normal.PNG') #用pyplot呈現圖片 plt.figure(figsize=(20,5)) #设置窗口大小 plt.subplot(1,4,1), plt.title('dark') #把圖片排列成1行顯示4個 plt.imshow(dark), plt.axis('off') #把x軸的標籤去除 plt.subplot(1,4,2), plt.title('bright') plt.imshow(bright), plt.axis('off') plt.subplot(1,4,3), plt.title('gray') plt.imshow(gray), plt.axis('off') plt.subplot(1,4,4), plt.title('normal') plt.imshow(normal), plt.axis('off') plt.show() #印出圖片 ``` ![](https://i.imgur.com/4i9tBGT.png) --- ### power-low power-low公式s=cr^γ,將4張圖片分別做公式運算,先將圖片除以255以便把數值壓在0~1之間,做γ次方的運算後,在乘上255恢復色彩顯示範圍。 ``` #設定兩個gamma值 gamma_h = 2.0 gamma_l = 0.5 #power-low運算 dark_t_h = np.array(255*(dark/255)**gamma_h, dtype = 'uint8') bright_t_h = np.array(255*(bright/255)**gamma_h, dtype = 'uint8') gray_t_h = np.array(255*(gray/255)**gamma_h, dtype = 'uint8') normal_t_h = np.array(255*(normal/255)**gamma_h, dtype = 'uint8') dark_t_l = np.array(255*(dark/255)**gamma_l, dtype = 'uint8') bright_t_l = np.array(255*(bright/255)**gamma_l, dtype = 'uint8') gray_t_l = np.array(255*(gray/255)**gamma_l, dtype = 'uint8') normal_t_l = np.array(255*(normal/255)**gamma_l, dtype = 'uint8') plt.figure(figsize=(20,5)) plt.subplot(1,4,1), plt.title('dark_transform_h') plt.imshow(dark_t_h), plt.axis('off') plt.subplot(1,4,2), plt.title('bright_transform_h') plt.imshow(bright_t_h), plt.axis('off') plt.subplot(1,4,3), plt.title('gray_transform_h') plt.imshow(gray_t_h), plt.axis('off') plt.subplot(1,4,4), plt.title('normal_transform_h') plt.imshow(normal_t_h), plt.axis('off') plt.show() plt.figure(figsize=(20,5)) plt.subplot(1,4,1), plt.title('dark_transform_l') plt.imshow(dark_t_l), plt.axis('off') plt.subplot(1,4,2), plt.title('bright_transform_l') plt.imshow(bright_t_l), plt.axis('off') plt.subplot(1,4,3), plt.title('gray_transform_l') plt.imshow(gray_t_l), plt.axis('off') plt.subplot(1,4,4), plt.title('normal_transform_l') plt.imshow(normal_t_l), plt.axis('off') plt.show() cv2.imwrite("dark_transform_h.jpg", dark_t_h) cv2.imwrite("bright_transform_h.jpg", bright_t_h) cv2.imwrite("gray_transform_h.jpg", gray_t_h) cv2.imwrite("normal_transform_h.jpg", normal_t_h) cv2.imwrite("dark_transform_l.jpg", dark_t_l) cv2.imwrite("bright_transform_l.jpg", bright_t_l) cv2.imwrite("gray_transform_l.jpg", gray_t_l) cv2.imwrite("normal_transform_l.jpg", normal_t_l) ``` ![](https://i.imgur.com/9uO7XfJ.png) --- ### histogram_equalization histogram-equalization方法能夠將cdf分布從原本的聚集,變為分散狀態以達到明暗對比分明的效果,做法式將灰度最大值推到256,把最小值推到0,其中他們彼此的區間會被等量的加大。 ![](https://i.imgur.com/Ms73wtu.png) ``` def img_cdf(img, title): #圖片的cdf hist, bins = np.histogram(img.flatten(),256,[0,256]) plt.hist(img.flatten(),256,[0,256],color="g") plt.xlim([0,256]) plt.title(title) plt.show() def histogram_equalization(img_in): b, g, r = cv2.split(img_in) #切出rgb三個值 hist_b, bin_b = np.histogram(b.flatten(),256,[0,256]) hist_g, bin_g = np.histogram(g.flatten(),256,[0,256]) hist_r, bin_r = np.histogram(r.flatten(),256,[0,256]) cdf_b = np.cumsum(hist_b) #將數組累加 cdf_g = np.cumsum(hist_g) cdf_r = np.cumsum(hist_r) cdf_m_b = np.ma.masked_equal(cdf_b,0) #直方圖均衡化 cdf_m_b = (cdf_m_b - cdf_m_b.min())*255/(cdf_m_b.max()-cdf_m_b.min()) #公式運算 cdf_final_b = np.ma.filled(cdf_m_b,0).astype('uint8') #將0補上平均值 cdf_m_g = np.ma.masked_equal(cdf_g,0) cdf_m_g = (cdf_m_g - cdf_m_g.min())*255/(cdf_m_g.max()-cdf_m_g.min()) cdf_final_g = np.ma.filled(cdf_m_g,0).astype('uint8') cdf_m_r = np.ma.masked_equal(cdf_r,0) cdf_m_r = (cdf_m_r - cdf_m_r.min())*255/(cdf_m_r.max()-cdf_m_r.min()) cdf_final_r = np.ma.filled(cdf_m_r,0).astype('uint8') img_b = cdf_final_b[b] img_g = cdf_final_g[g] img_r = cdf_final_r[r] img_out = cv2.merge((img_b,img_g,img_r)) return img_out img_cdf(dark,"dark_cdf") img_cdf(bright,"bright_cdf") img_cdf(gray,"gray_cdf") img_cdf(normal,"normal_cdf") dark_transform = histogram_equalization(dark) bright_transform = histogram_equalization(bright) gray_transform = histogram_equalization(gray) plt.figure(figsize=(20,5)) plt.subplot(1,3,1), plt.title('dark_transform') plt.imshow(dark_transform), plt.axis('off') plt.subplot(1,3,2), plt.title('bright_transform') plt.imshow(bright_transform), plt.axis('off') plt.subplot(1,3,3), plt.title('gray_transform') plt.imshow(gray_transform), plt.axis('off') plt.show() cv2.imwrite("dark_transform.jpg", dark_transform) cv2.imwrite("bright_transform.jpg", bright_transform) cv2.imwrite("gray_transform.jpg", gray_transform) ``` ![](https://i.imgur.com/qTj7Nd0.png =300x)![](https://i.imgur.com/F0TF93f.png =300x) ![](https://i.imgur.com/1kBpqoB.png =300x)![](https://i.imgur.com/hpZTxHR.png =300x) ![](https://i.imgur.com/kuEdQNv.png) --- ### Conclusion 比較power-low以及histogram equalization,power-low能夠決定的是整張圖片的亮度,只能同時變亮或變暗,所以在過暗和過亮兩張圖片,可以有稍微好的效果,但作用在灰階圖片上完全不行。 至於histogram equalization,因為可以將亮的部分變得更亮,暗的部分變得更暗,明暗區分出來,可以讓物件更明顯。