OpenCV-python 实现高频提升滤波

基础知识

1、图像锐化: 叠加图像微分结果(拉普拉斯算子)、减除图像积分结果
 
2、设原始图像为f(x,y) ,平滑图像为g(x,y) ,放大系数A;

      则非锐化掩模:

          

    【即平滑时丢失的锐化分量

      3、对原始图像乘以A再减去平滑图像可实现高频提升滤波:

     

      当A=1时, h_{b}(x,y)为非锐化掩模;

      当A>1时,加权的原始图像与非锐化掩模相加,得到锐化图像;

      当A=2时, 称为非锐化掩模化


    实现

       1、代码:

import cv2
import matplotlib.pyplot as plt

class imageSizeError(Exception):
    def __init__(self):
        self.value = "图片大小错误"
    def __str__(self):
        return self.value

#矩阵减法
def decreaseArray(image1, image2):
    if image1.shape == image2.shape:
        image = image1.copy()
        for i in range(image1.shape[0]-1):
            for j in range(image1.shape[1]-1):
                image[i][j] = image1[i][j] - image2[i][j]
                j = j+1
            i = i+1
        return image
    else:
        raise imageSizeError()

#矩阵加法
def increaseArray(image1, image2):
    if image1.shape == image2.shape:
        image = image1.copy()
        for i in range(image1.shape[0]-1):
            for j in range(image1.shape[1]-1):
                image[i][j] = image1[i][j] + image2[i][j]
                j = j+1
            i = i+1
        return image
    else:
        raise imageSizeError()

#显示函数
def showImages(images):
    for i in range(len(images)):
        img = images[i]
        title = "("+str(i+1)+")"
        #行,列,索引
        plt.subplot(2, 2, i+1)
        plt.imshow(img, cmap="gray")
        plt.title(title,fontsize=10)
        plt.xticks([])
        plt.yticks([])
    plt.show()

if __name__ == "__main__":
    image = cv2.imread("image.jpg")
    imageAver3 = cv2.blur(image, (3, 3))
    unsharpMask = decreaseArray(image, imageAver3)
    imageSharp = increaseArray(image, unsharpMask)
    images = [image, imageAver3, unsharpMask, imageSharp]
    showImages(images)

          2、结果

OpenCV-python 实现高频提升滤波_第1张图片

(1)原始图像

(2)3×3平均滤波结果图像

(3)非锐化掩模

(4)非锐化掩模化结果图像

你可能感兴趣的:(python,图像处理,OpenCV)