opencv均值滤波

参考链接
opencv均值滤波_第1张图片

import os
import re
import cv2

#cv2.imshow('image',img)显示
#cv2.waitKey(10000);  停留
#cv2.destroyAllWindows() 关闭

from PIL import Image
import numpy as np
import matplotlib.pylab as pylab

#######################_添加噪声_#################################
def add_noisy(image, n=10000):
    result = image.copy()
    w, h = image.shape[:2]
    for i in range(n):
        # 分别在宽和高的范围内生成一个随机值,模拟代表x, y坐标
        x = np.random.randint(1, w)
        y = np.random.randint(1, h)
        if np.random.randint(0, 2) == 0:
            # 生成白色噪声(盐噪声)
            result[x, y] = 0
        else:
            # 生成黑色噪声(椒噪声)
            result[x, y] = 255
    return result

if __name__ == '__main__':
    img = cv2.imread('111.jpg')
    color_image_noisy = add_noisy(img, 10000)
    
    cv2.imshow('color_image_noisy',color_image_noisy)
    lena_deal = cv2.blur(color_image_noisy,(3,3))
    cv2.imshow('lena_deal',lena_deal)
    cv2.waitKey(10000);
    cv2.destroyAllWindows() 

3x3卷积核

5x5卷积核
opencv均值滤波_第2张图片

你可能感兴趣的:(机器视觉,opencv,均值算法,计算机视觉)