python实现rgb图像 sift配准 直方图均衡化 均值、中值、高斯去噪 暗通道法去雾代码

直方图均衡化

import numpy as np
import cv2


def hisEqulColor(img):
    ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
    channels = cv2.split(ycrcb)
    cv2.equalizeHist(channels[0], channels[0])
    cv2.merge(channels, ycrcb)
    cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR, img)
    return img

im = cv2.imread('2019_low.png')
#cv2.imshow('im1', im)
cv2.waitKey(0)

eq = hisEqulColor(im)
cv2.imshow('image2',eq )
cv2.waitKey(0)
cv2.imwrite('img_jhh.png',eq)

配准

import numpy as np
import cv2
import Utility


def txpz(img1,img2):
    result,_,_ = Utility.siftImageAlignment(img1,img2)
    allImg = np.concatenate((img1,img2,result),axis=1)
#    cv2.namedWindow('Result',cv2.WINDOW_NORMAL)
#    cv2.imshow('Result',allImg)
    cv2.imwrite('peizhun.png', result)
#    cv2.waitKey(0)


img1 = cv2.imread('2019_low.png')
img2 = cv2.imread('pz1.png')
txpz(img1,img2)

去噪

import cv2
import numpy as np


def blur_demo(image):

    dst = cv2.blur(image, (1, 4))
    cv2.imshow("avg_blur_demo", dst)
    cv2.imwrite('img_blur.png', dst)

def median_blur_demo(image):    # 中值模糊  对椒盐噪声有很好的去燥效果
    dst = cv2.medianBlur(image, 3)
    cv2.imshow("median_blur_demo", dst)
    cv2.imwrite('img_meblur.png', dst)

def custom_blur_demo(image):

    kernel = np.ones([3, 3], np.float32)/9
    dst = cv2.filter2D(image, -1, kernel)
    cv2.imshow("custom_blur_demo", dst)
    cv2.imwrite('img_csblur.png', dst)

def guassian_blur_demo(image):
    dst = cv2.GaussianBlur(img, (3, 3), 0)
    cv2.imshow("guassian_blur_demo", dst)
    cv2.imwrite('img_gsblur.png', dst)


src = cv2.imread("gsnoise.png")
img = cv2.resize(src,None,fx=0.8,fy=0.8,interpolation=cv2.INTER_CUBIC)
cv2.imshow('input_image', img)

#blur_demo(img)
#median_blur_demo(img)
median_blur_demo(img)

cv2.waitKey(0)

暗通道法去雾

import cv2
import numpy as np


def zmMinFilterGray(src, r=7):


    return cv2.erode(src, np.ones((2 * r + 1, 2 * r + 1)))  # 使用opencv的erode函数更高效


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):  # 输入rgb图像,值范围[0,1]

    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

 

你可能感兴趣的:(Python学习)