OpenCV 实现图像复原 并对图像中有条纹的干扰进行去除

import cv2

'''
三种滤波的方法

高斯噪声
椒盐噪声
均值噪声

'''


## 中值滤波器
def median_blur(image, type, channel):
    '''

    :param image:  原始图像
    :param type:  中值滤波的样式 3 3  1十字形 2 方形   3 ×形
    :param channel: 频道数  2为 灰色 3为 彩色
    :return: new_image
    '''
    new_image = image.copy()
    if channel == 2:  ##灰色
        if type == 1:  ##十字形 3 3
            for row in range(1, new_image.shape[0] - 1):
                for col in range(1, new_image.shape[1] - 1):
                    np = [new_image[row - 1][col],
                          new_image[row][col - 1], new_image[row][col], new_image[row][col + 1],
                          new_image[row + 1][col]]
                    np.sort()
                    new_image[row][col] = np[2]
            return new_image

        elif type == 2:
            for row in range(1, new_image.shape[0] - 1):
                for col in range(1, new_image.shape[1] - 1):
                    np = [new_image[row - 1][col - 1], new_image[row - 1][col], new_image[row - 1][col + 1],
                          new_image[row][col - 1], new_image[row][col], new_image[row][col + 1],
                          new_image[row + 1][col - 1], new_image[row + 1][col], new_image[row + 1][col + 1]]
                    np.sort()
                    new_image[row][col] = np[4]
            return new_image

        elif type == 3:
            for row in range(1, new_image.shape[0] - 1):
                for col in range(1, new_image.shape[1] - 1):
                    np = [new_image[row - 1][col - 1], new_image[row - 1][col + 1],
                          new_image[row][col],
                          new_image[row + 1][col - 1], new_image[row + 1][col + 1]]
                    np.sort()
                    new_image[row][col] = np[2]
            return new_image

        else:
            print("type 参数输入错误!!!")
    elif channel == 3:
        if type == 2:        #方形
            for row in range(1, new_image.shape[0] - 1):
                for col in range(1, new_image.shape[1] - 1):
                    for ch in range(new_image.shape[2]):
                        np = [new_image[row - 1][col - 1][ch],new_image[row - 1][col][ch],new_image[row - 1][col + 1][ch],
                              new_image[row][col - 1][ch], new_image[row][col][ch], new_image[row][col + 1][ch],
                              new_image[row + 1][col - 1][ch], new_image[row + 1][col][ch],new_image[row + 1][col + 1][ch]]
                        np.sort()
                        new_image[row][col][ch] = np[4]
            return new_image

        elif type == 1:  ##十字形
            for row in range(1, new_image.shape[0] - 1):
                for col in range(1, new_image.shape[1] - 1):
                    for ch in range(new_image.shape[2]):
                        np = [new_image[row - 1][col][ch],
                              new_image[row][col - 1][ch], new_image[row][col][ch], new_image[row][col + 1][ch],
                              new_image[row + 1][col][ch]]
                        np.sort()
                        new_image[row][col][ch] = np[2]
            return new_image

        elif type == 3:   # ×形
            for row in range(1, new_image.shape[0] - 2):
                for col in range(1, new_image.shape[1] - 2):
                    for ch in range(new_image.shape[2]):

                        np = [
                            new_image[row - 2][col - 2][ch],                           new_image[row - 2][col + 2][ch],
                            new_image[row - 1][col - 1][ch],                           new_image[row - 1][col + 1][ch],
                                                             new_image[row][col][ch],
                              new_image[row + 1][col - 1][ch],                        new_image[row + 1][col + 1][ch],
                              new_image[row + 2][col - 2][ch],                          new_image[row + 2][col + 2][ch],

                        ]
                        np.sort()
                        new_image[row][col][ch] = np[4]
            return new_image

        else:
            print("type 参数输入错误!!!")
    else:
        print("channel 参数输入错误!!!")


def cv_median_blur(image, dst):
    '''

    :param image: 原始图像
    :param dst: 卷积核大小
    :return:new_image
    '''
    new_image = cv2.medianBlur(image, dst)
    return new_image


##  加权均值滤波器

def quan_meanfiltering(image, channel):
    new_image = image.copy()
    if channel == 2:  ##灰色
        for row in range(1, new_image.shape[0] - 1):
            for col in range(1, new_image.shape[1] - 1):
                np = [new_image[row - 1][col - 1], 2 * new_image[row - 1][col], new_image[row - 1][col + 1],
                      2 * new_image[row][col - 1], 4 * new_image[row][col], 2 * new_image[row][col + 1],
                      new_image[row + 1][col - 1], 2 * new_image[row + 1][col], new_image[row + 1][col + 1]]
                new_image[row][col] = sum(np) / 16
        return new_image

    elif channel == 3:
        for row in range(1, new_image.shape[0] - 1):
            for col in range(1, new_image.shape[1] - 1):
                for ch in range(new_image.shape[2]):
                    np = [new_image[row - 1][col - 1][ch], 4 * new_image[row - 1][col][ch],new_image[row - 1][col + 1][ch],
                          4 * new_image[row][col - 1][ch], 8 * new_image[row][col][ch], 4 * new_image[row][col + 1][ch],
                          new_image[row + 1][col - 1][ch], 4 * new_image[row + 1][col][ch],new_image[row + 1][col + 1][ch]]
                    new_image[row][col][ch] = sum(np) / 28
        return new_image

    else:
        print("channel 参数输入错误!!!")


if __name__ == '__main__':
    file_path = 'test.png'
    image = cv2.imread(file_path)

    new_image = median_blur(image,3,3)

    new1_image = quan_meanfiltering(new_image,3)

    cv2.imshow('for.png',new1_image)
    cv2.waitKey(0)

你可能感兴趣的:(opencv,计算机视觉,python)