OpenCV之去雾&特征检测

这个去雾算法是做一个图像识别分类项目中使用的,用来作图像的预处理,尤其是对于图像的特征被雾遮挡住的情况。

github链接:https://github.com/x2mercy/scene_recognition

暗通道算法——何凯明博士

1:去雾

import cv2  
import numpy as np  
   
def zmMinFilterGray(src, r=7):  
    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):  
    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):  #input rgb image, the range of value [0,1] 
    V1 = np.min(m,2)                                         #get dark path image  
    V1 = guidedfilter(V1, zmMinFilterGray(V1,7), r, eps)     #guided filter 
    bins = 2000  
    ht = np.histogram(V1, bins)                              #calculate light of atmosphere: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)                   #limit the range  
       
    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)               #get shaded image and light  
    for k in range(3):  
        Y[:,:,k] = (m[:,:,k]-V1)/(1-V1/A)           #correct color
    Y =  np.clip(Y, 0, 1)  
    if bGamma:  
        Y = Y**(np.log(0.5)/np.log(Y.mean()))       #correct gamma 
    return Y  
   
if __name__ == '__main__':  
    m = deHaze(cv2.imread('/Users/wangmengxi/Documents/mercy/ec601/pj1/image processing/boston-fog.jpg')/255.0)*255  
    cv2.imwrite('boston-defog.jpg', m)  

首先,要安装opencv,安装方法:https://github.com/opencv/opencv  (推荐homebrew方法安装)

我的环境:Python2.7(表白anaconda)

使用方法:直接把main里面的imread内容换成需要去雾的图像,然后换一下imwrite的图像保存路径

结果:

 

2:特征提取

这里使用特征提取是为了证明,图像中的建筑被雾遮住的特点在去雾之后显现出来(并没有改变建筑原本的特征

方法:检测Harris cornor

工具:OpenCV

代码:

#%matplotlib inline
import numpy as np
from skimage.feature import corner_harris,corner_peaks
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
import skimage.io as io
from skimage.exposure import equalize_hist

def show_corners(corners,image):
    fig=plt.figure()
    plt.gray()
    plt.imshow(image)
    y_corner,x_corner=zip(*corners)
    plt.plot(x_corner,y_corner,'or')
    plt.xlim(0,image.shape[1])
    plt.ylim(image.shape[0],0)
    fig.set_size_inches(np.array(fig.get_size_inches())*1.5)
    plt.show()
if __name__ == '__main__':  
    mandrill=io.imread('/Users/wangmengxi/Documents/mercy/ec601/pj1/image processing/defog1.jpg')

    mandrill=equalize_hist(rgb2gray(mandrill))
    corners=corner_peaks(corner_harris(mandrill),min_distance=2)
    show_corners(corners,mandrill)
    

 

结果:

 

posted @ 2017-11-13 05:59  机智的小八  阅读(4032)  评论(1编辑  收藏  举报