暗通道去霧演算法的python實現

來源:互聯網
上載者:User

標籤:技術   write   最大   mini   去霧   style   idt   python   pen   

    何凱明博士的去霧文章和演算法實現已經漫天飛了,我今天也就不囉裡囉唆,直接給出自己python實現的完整版本,全部才60多行代碼,簡單易懂,並有簡要注釋,去霧效果也很不錯。

    在這個python版本中,計算量最大的就是最小值濾波,純python寫的,慢,可以進一步使用C最佳化,其他部分都是使用numpy和opencv的現成東東,效率還行。

 

import cv2import numpy as np def zmMinFilterGray(src, r=7):    ‘‘‘最小值濾波,r是濾波器半徑‘‘‘    if r <= 0:        return src    h, w = src.shape[:2]    I = src    res = np.minimum(I  , I[[0]+range(h-1)  , :])    res = np.minimum(res, I[range(1,h)+[h-1], :])    I = res    res = np.minimum(I  , I[:, [0]+range(w-1)])    res = np.minimum(res, I[:, range(1,w)+[w-1]])    return zmMinFilterGray(res, r-1) def guidedfilter(I, p, r, eps):    ‘‘‘引導濾波,直接參考網上的matlab代碼‘‘‘    height, width = I.shape    m_I = cv2.boxFilter(I, -1, (r,r))    m_p = cv2.boxFilter(p, -1, (r,r))    m_Ip = cv2.boxFilter(I*p, -1, (r,r))    cov_Ip = m_Ip-m_I*m_p     m_II = cv2.boxFilter(I*I, -1, (r,r))    var_I = m_II-m_I*m_I     a = cov_Ip/(var_I+eps)    b = m_p-a*m_I     m_a = cv2.boxFilter(a, -1, (r,r))    m_b = cv2.boxFilter(b, -1, (r,r))    return m_a*I+m_b def getV1(m, r, eps, w, maxV1):  #輸入rgb映像,值範圍[0,1]    ‘‘‘計算大氣遮罩映像V1和光照值A, V1 = 1-t/A‘‘‘    V1 = np.min(m,2)                                         #得到暗通道映像    V1 = guidedfilter(V1, zmMinFilterGray(V1,7), r, eps)     #使用引導濾波最佳化    bins = 2000    ht = np.histogram(V1, bins)                              #計算大氣光照A    d = np.cumsum(ht[0])/float(V1.size)    for lmax in range(bins-1, 0, -1):        if d[lmax]<=0.999:            break    A  = np.mean(m,2)[V1>=ht[1][lmax]].max()             V1 = np.minimum(V1*w, maxV1)                   #對值範圍進行限制         return V1,A def deHaze(m, r=81, eps=0.001, w=0.95, maxV1=0.80, bGamma=False):    Y = np.zeros(m.shape)    V1,A = getV1(m, r, eps, w, maxV1)               #得到遮罩映像和大氣光照    for k in range(3):        Y[:,:,k] = (m[:,:,k]-V1)/(1-V1/A)           #顏色校正    Y =  np.clip(Y, 0, 1)    if bGamma:        Y = Y**(np.log(0.5)/np.log(Y.mean()))       #gamma校正,預設不進行該操作    return Y if __name__ == ‘__main__‘:    m = deHaze(cv2.imread(‘land.jpg‘)/255.0)*255    cv2.imwrite(‘defog.jpg‘, m)

  

下面給兩個運行效果吧

 

暗通道去霧演算法的python實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.