当前位置:   article > 正文

image restoration(图像恢复)----算数平均过滤、几何均值滤波、谐波均值滤波、逆谐波均值滤波(python)_swsalogrithm

swsalogrithm

(1)代码

import cv2
import numpy as np
import matplotlib.pyplot as plt
import scipy
import scipy.stats
import random

def GaussieNoisy(image,sigma):
    # 高斯噪声
    img = image.astype(np.int16)  # 此步是为了避免像素点小于0,大于255的情况
    mu = 0
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            for k in range(img.shape[2]):
                img[i, j, k] = img[i, j, k] + random.gauss(mu=mu, sigma=sigma)
    img[img > 255] = 255
    img[img < 0] = 0
    img = img.astype(np.uint8)
    return img

def spNoisy(image,s_vs_p = 0.5,amount = 0.004):
    # 椒盐噪声
    out = np.copy(image)
    num_salt = np.ceil(amount * image.size * s_vs_p)
    coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
    out[tuple(coords)] = 1
    num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
    coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
    out[tuple(coords)] = 0
    return out




def ArithmeticMeanAlogrithm(image):
    # 算术均值滤波
    new_image = np.zeros(image.shape)
    image = cv2.copyMakeBorder(image,1,1,1,1,cv2.BORDER_DEFAULT)
    for i in range(1,image.shape[0]-1):
        for j in range(1,image.shape[1]-1):
            new_image[i-1,j-1] = np.mean(image[i-1:i+2,j-1:j+2])
    new_image = (new_image-np.min(image))*(255/np.max(image))
    return new_image.astype(np.uint8)
def rgbArithmeticMean(image):
    r,g,b = cv2.split(image)
    r = ArithmeticMeanAlogrithm(r)
    g = ArithmeticMeanAlogrithm(g)
    b = ArithmeticMeanAlogrithm(b)
    return cv2.merge([r,g,b])


def GeometricMeanOperator(roi):
    roi = roi.astype(np.float64)
    p = np.prod(roi)
    return p ** (1 / (roi.shape[0] * roi.shape[1]))
def GeometricMeanAlogrithm(image):
    # 几何均值滤波
    new_image = np.zeros(image.shape)
    image = cv2.copyMakeBorder(image, 1, 1, 1, 1, cv2.BORDER_DEFAULT)
    for i in range(1, image.shape[0] - 1):
        for j in range(1, image.shape[1] - 1):
            new_image[i - 1, j - 1] = GeometricMeanOperator(image[i - 1:i + 2, j - 1:j + 2])
    new_image = (new_image - np.min(image)) * (255 / np.max(image))
    return new_image.astype(np.uint8)
def rgbGemotriccMean(image):
    r,g,b = cv2.split(image)
    r = GeometricMeanAlogrithm(r)
    g = GeometricMeanAlogrithm(g)
    b = GeometricMeanAlogrithm(b)
    return cv2.merge([r,g,b])


def HarmonicMeanOperator(roi):
    roi = roi.astype(np.float64)
    if 0 in roi:
        roi = 0
    else:
        roi = scipy.stats.hmean(roi.reshape(-1))
    return roi
def HarmonicMeanAlogrithm(image):
    # 谐波均值滤波
    new_image = np.zeros(image.shape)
    image = cv2.copyMakeBorder(image,1,1,1,1,cv2.BORDER_DEFAULT)
    for i in range(1,image.shape[0]-1):
        for j in range(1,image.shape[1]-1):
            new_image[i-1,j-1] =HarmonicMeanOperator(image[i-1:i+2,j-1:j+2])
    new_image = (new_image-np.min(image))*(255/np.max(image))
    return new_image.astype(np.uint8)
def rgbHarmonicMean(image):
    r,g,b = cv2.split(image)
    r = HarmonicMeanAlogrithm(r)
    g = HarmonicMeanAlogrithm(g)
    b = HarmonicMeanAlogrithm(b)
    return cv2.merge([r,g,b])


def Contra_harmonicMeanOperator(roi,q):
    roi = roi.astype(np.float64)
    return np.mean((roi)**(q+1))/np.mean((roi)**(q))
def Contra_harmonicMeanAlogrithm(image,q):
    # 逆谐波均值滤波
    new_image = np.zeros(image.shape)
    image = cv2.copyMakeBorder(image,1,1,1,1,cv2.BORDER_DEFAULT)
    for i in range(1,image.shape[0]-1):
        for j in range(1,image.shape[1]-1):
            new_image[i-1,j-1] = Contra_harmonicMeanOperator(image[i-1:i+2,j-1:j+2],q)
    new_image = (new_image-np.min(image))*(255/np.max(image))
    return new_image.astype(np.uint8)
def rgbContra_harmonicMean(image,q):
    r,g,b = cv2.split(image)
    r = Contra_harmonicMeanAlogrithm(r,q)
    g = Contra_harmonicMeanAlogrithm(g,q)
    b = Contra_harmonicMeanAlogrithm(b,q)
    return cv2.merge([r,g,b])




if __name__ == '__main__':
    house = cv2.imread("E:/pythontupian/6.jpg")
    house = cv2.resize(cv2.cvtColor(house, cv2.COLOR_BGR2RGB), (200, 200))
    plt.imshow(house)
    plt.axis("off")
    plt.title("Original Image")
    plt.show()   # 原图像

    flagN = input("请选择加入的噪声:\n"
                  "高斯噪声 -- 1\n"
                  "椒盐噪声 -- 2\n")

    if flagN == "1":
        GuassHouse = GaussieNoisy(house,18)
        plt.imshow(GuassHouse)
        plt.axis("off")
        plt.title("Gauss noise Image")
        plt.show()   # 加入高斯噪声后的图像
    elif flagN == "2":
        spHouse = spNoisy(house)
        plt.imshow(spHouse)
        plt.axis("off")
        plt.title("Salt And peper Image")
        plt.show()  # 加入椒盐噪声后的图像


    flagF = input("请选择滤波器:\n"
                  "算术均值滤波 -- a\n"
                  "几何均值滤波 -- b\n"
                  "谐波均值滤波 -- c\n"
                  "逆谐波均值滤波 -- d\n")

    if flagF == "a":
        if flagN == "1":
            plt.imshow(rgbArithmeticMean(GuassHouse))
        elif flagN == "2":
            plt.imshow(rgbArithmeticMean(spHouse))
        plt.title("Arithmetic Mean Filter")
        plt.show()  # Arithmetic Mean Filter
    elif flagF == "b":
        if flagN == "1":
            plt.imshow(rgbGemotriccMean(GuassHouse))
        elif flagN == "2":
            plt.imshow(rgbGemotriccMean(spHouse))
        plt.title("Geometric Mean Filter")
        plt.show()  # Geometric Mean Filter
    elif flagF == "c":
        if flagN == "1":
            plt.imshow(rgbHarmonicMean(GuassHouse))
        elif flagN == "2":
            plt.imshow(rgbHarmonicMean(spHouse))
        plt.title("Harmonic Mean Filter")
        plt.show()  # Harmonic Mean Filter
    elif flagF == "d":
        if flagN == "1":
            plt.imshow(rgbContra_harmonicMean(GuassHouse,2))
        elif flagN == "2":
            plt.imshow(rgbContra_harmonicMean(spHouse,2))
        plt.title("Contra-harmonic Mean Filter")
        plt.show()  # Contra-harmonic Mean Filter

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179

(2) 结果展示

原图
在这里插入图片描述
加高斯噪声后
在这里插入图片描述
算术均值滤波后
在这里插入图片描述
几何均值滤波后
在这里插入图片描述
谐波均值滤波后
在这里插入图片描述
逆谐波均值滤波后
在这里插入图片描述
加椒盐噪声后
在这里插入图片描述
算术均值滤波后
在这里插入图片描述
几何均值滤波后
在这里插入图片描述
谐波均值滤波后
在这里插入图片描述
逆谐波均值滤波后
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/238599
推荐阅读
相关标签
  

闽ICP备14008679号