iT邦幫忙

1

openCV 影像校正

  • 分享至 

  • xImage

想詢問板上各位前輩們,關於影像校正的部分
我在網路上翻了許多關於影像校正相關演算法文章,
想詢問前輩們,openCV 的影像校正有運用哪些演算法?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

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

你指的影像校正是什麼??
gamma correction? camera calibration?
看是哪一個詞, 「opencv」空格再加關鍵字丟google查,就有你想要的答案了。

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

我自己會用來做影像校正的方法,包括gamma correction跟contrast correction,我的做法是搭配校正色版spyder checker 24一起使用,相關的影像如下所示:

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

https://ithelp.ithome.com.tw/upload/images/20201224/20121176HbTY1oQxA2.jpg

2.接著將黑/灰/白校正色板圖片切下儲存,並以平均值儲存校正色板R、G、B數值
https://ithelp.ithome.com.tw/upload/images/20201224/20121176r6FLb3EAny.pnghttps://ithelp.ithome.com.tw/upload/images/20201224/201211765CzVZiG1xg.pnghttps://ithelp.ithome.com.tw/upload/images/20201224/20121176HtWMzqFdNx.png

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

def constrast_correction(o,image_name,gray):
    #開始對比校正
    start_time = time.time()
    print('{}_對比校正'.format(image_name))
    #讀取原圖
    o = cv.imread('./00_cut/cut_'+image_name)
    #讀取黑/白色板
    black = cv.imread('./999_black_ck/black_ck_'+image_name)
    white = cv.imread('./999_white_ck/white_ck_'+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)))

結果圖:
https://ithelp.ithome.com.tw/upload/images/20201224/20121176WOQSGQjznY.jpg

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)))

結果圖(其實很細微,看不太出來,但數值有改變):
https://ithelp.ithome.com.tw/upload/images/20201224/20121176OPyhznCaOS.jpg

ps:
如果想要做這樣的校正,
但是沒有校正色板,
也可以自定義黑/灰/白數值來調整,
以下為沒有校正色板的調整結果

校正前:
https://ithelp.ithome.com.tw/upload/images/20201224/20121176YJa9L9XrRp.png

由於我的目的是要將暗色的環境調亮,
因此我將黑色BGR設定為0,白色BGR設定為170

校正後:
https://ithelp.ithome.com.tw/upload/images/20201224/20121176XciZ6dIe4T.png

以上是我自己在做校正的方法,
希望對你以及有相同問題的人有幫助。

詳細公式請參考此篇文獻:
https://onlinelibrary.wiley.com/doi/pdf/10.1002/ecj.12045

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.

我要發表回答

立即登入回答