1

## openCV 影像校正

### 2 個回答

0
kawa0710
iT邦新手 5 級 ‧ 2020-12-23 09:33:26

gamma correction? camera calibration?

2
Maso的萬事屋
iT邦新手 5 級 ‧ 2020-12-24 11:18:48

1.影像中拍攝時目標物與黑/灰/白校正色板放在一起

2.接著將黑/灰/白校正色板圖片切下儲存，並以平均值儲存校正色板R、G、B數值

3.進行contrast correction(應用黑/白之校正色板RGB數值)

``````def constrast_correction(o,image_name,gray):
#開始對比校正
start_time = time.time()
print('{}_對比校正'.format(image_name))
#讀取原圖
#讀取黑/白色板

img = o.copy()

#分離黑/白色板BGR數值
Bb,Bg,Br = cv.split(black)
Wb,Wg,Wr = cv.split(white)

#將BGR數值分別轉為一維矩陣
B_r = np.array(Br).flatten()
B_g = np.array(Bg).flatten()
B_b = np.array(Bb).flatten()

W_r = np.array(Wr).flatten()
W_g = np.array(Wg).flatten()
W_b = np.array(Wb).flatten()

#建立空白圖層=>目的是存放黑/白的BGR
img_bl = np.zeros(img.shape)
img_w = np.zeros(img.shape)

#將黑的BGR各別最小值與白的BGR各別最小值存到空白圖層中
img_bl[:,:,0] = B_b.min()
img_bl[:,:,1] = B_g.min()
img_bl[:,:,2] = B_r.min()

img_w[:,:,0] = W_b.max()
img_w[:,:,1] = W_g.max()
img_w[:,:,2] = W_r.max()

#將原圖以此公式進行計算
result_img = 255*(img-img_bl)/(img_w-img_bl)

#儲存對比校正結果
cv.imwrite('./8_constrast/constrast_'+image_name,result_img)
end_time = time.time()
print("處理的時間{}秒".format(round(end_time-start_time,2)))

``````

4.進行gamma correction(應用灰校正色板RGB數值)

``````def gamma_correction(image,image_name,gray):
#進行gamma校正
start_time = time.time()
print('{}_Gamma校正'.format(image_name))

#將灰色板分離
B,G,R = cv.split(gray)

#計算BGR之gamma值
r1 = math.log10(119)/math.log10(R.mean())
r2 = math.log10(119)/math.log10(G.mean())
r3 = math.log10(119)/math.log10(B.mean())

#呼叫原圖(對比校正後的結果圖)
o = image
img = o.copy()

#將原圖進行gamma校正
img[:,:,2] = 255*((img[:,:,2]/255)**r1)
img[:,:,1] = 255*((img[:,:,1]/255)**r2)
img[:,:,0] = 255*((img[:,:,0]/255)**r3)

end_time = time.time()
print("處理的時間{}秒".format(round(end_time-start_time,2)))

``````

ps:

Ishii, M., Kusada, I., & Yamane, H. (2018). Quantitative Decision Method of Appropriate Apple Harvest Time using Color Information. Electronics and Communications in Japan, 101(2), 61-73.