图像处理|空间滤波-随机椒盐噪声-高斯噪声-均值滤波器-中值滤波器

图像处理|空间滤波-随机椒盐噪声-高斯噪声-均值滤波器-中值滤波器
文章图片


文章目录

  • 1 随机椒盐噪声
  • 2 高斯噪声
  • 3 均值滤波器
  • 4 中值滤波器

1 随机椒盐噪声 椒噪声:灰度值为0的噪声点,黑噪声
盐噪声:灰度值为255的噪声点,白噪声
思路:获取图像长、宽、通道数,在每个通道矩阵中随机产生灰度值为0和255的像素。

def salt_and_pepper_noise(image,percentage): rows,columns,channels = image.shape#获取图片的长、宽、通道数 nums = int(rows*columns*percentage)#每个通道噪声数量 for channel in range(channels): for num in range(nums): row = random.randint(0,rows-1)#随机行号 column = random.randint(0,columns-1)#随机列号 if random.randint(0,1) == 0: image[row,column,channel] = 0#椒噪声 else: image[row,column,channel] = 255#盐噪声 return imageif __name__ == '__main__': image = cv2.imread('./1.jpg')# 读取图片 a = salt_and_pepper_noise(image,percentage=0.1) cv2.imshow('img',a) cv2.waitKey(0)

2 高斯噪声 【图像处理|空间滤波-随机椒盐噪声-高斯噪声-均值滤波器-中值滤波器】思路:产生高斯分布噪声与原图像进行叠加,并控制数值空间为0至255。

def guassian_Noise(image,mean,sigma):#sigma方差 tempt = np.array(image/255,dtype = float) noise = np.random.normal(mean,sigma, image.shape) tempt += noise tempt = np.clip(tempt, 0, 1) tempt = np.uint8(tempt * 255) return temptif __name__ == '__main__': image = cv2.imread('./1.jpg')# 读取图片 b = guassian_Noise(image,0,1) cv2.imshow('img', b) cv2.waitKey(0)

3 均值滤波器 思路:输入一个 n ? n n*n n?n各个元素为1的矩阵作为滤波器核,然后将原图像进行边缘填充,按步长为1的跨度从左到右,从上到下,对填充图片上的 n ? n n*n n?n局域矩阵依次进行点乘,并将点乘后局域矩阵的各个元素值加在一起除以 n 2 n^2 n2,这个值为滤波后图像的一个像素值,全部滤波后就会获得滤波图像,彩色图像是三通道,因此需要对三个通道依次滤波。

左边随机椒盐噪声,右边均值滤波9*9

左边高斯噪声,右边均值滤波9*9
def means_filter(image,filter_size): input_image = np.copy(image) kernal = np.ones((filter_size,filter_size))# 卷积核 padding_num = int((filter_size - 1)/2)#需要补0 b,g,r = cv2.split(input_image)#分开三通道 b = np.pad(b, (padding_num, padding_num), mode='constant', constant_values=0)#图像三通道边缘填充 g = np.pad(g, (padding_num, padding_num), mode='constant', constant_values=0) r = np.pad(r, (padding_num, padding_num), mode='constant', constant_values=0) input_image = cv2.merge([b,g,r])#合并三通道 wide,height,channels = input_image.shape output_image = np.copy(input_image) for channel in range(channels):#均值滤波 for i in range(padding_num,wide-padding_num): for j in range(padding_num,height-padding_num): output_image[i,j,channel] = np.sum(\ kernal * input_image[i-padding_num:i+padding_num+1,j-padding_num:j+padding_num+1,channel])/(filter_size**2) output_image = output_image[padding_num:wide - padding_num, padding_num:height - padding_num]# 裁剪return output_imageif __name__ == '__main__': c = means_filter(image,9) imgs = np.hstack([image,c]) cv2.imshow('img', imgs) cv2.waitKey(0)

4 中值滤波器 思路:输入一个 n ? n n*n n?n各个元素为1的矩阵作为滤波器核,然后将原图像进行边缘填充,按步长为1的跨度从左到右,从上到下,对填充图片上的 n ? n n*n n?n局域矩阵依次进行中值排序,并这个中值为滤波后图像的一个像素值,全部滤波后就会获得滤波图像,彩色图像是三通道,因此需要对三个通道依次滤波。

左边随机椒盐噪声,右边中值滤波9*9

左边高斯噪声,右边中值滤波9*9
def median_filter(image,filter_size): input_image = np.copy(image) kernal = np.ones((filter_size,filter_size))# 卷积核 padding_num = int((filter_size - 1)/2)#需要补0 b,g,r = cv2.split(input_image)#分开三通道 b = np.pad(b, (padding_num, padding_num), mode='constant', constant_values=0)#图像三通道边缘填充 g = np.pad(g, (padding_num, padding_num), mode='constant', constant_values=0) r = np.pad(r, (padding_num, padding_num), mode='constant', constant_values=0) input_image = cv2.merge([b,g,r])#合并三通道 wide,height,channels = input_image.shape output_image = np.copy(input_image) for channel in range(channels):#中值滤波 for i in range(padding_num,wide-padding_num): for j in range(padding_num,height-padding_num): output_image[i,j,channel] = np.median(input_image[i-padding_num:i+padding_num+1,j-padding_num:j+padding_num+1,channel]) output_image = output_image[padding_num:wide - padding_num, padding_num:height - padding_num]# 裁剪return output_imageif __name__ == '__main__': image = cv2.imread('./2.png')# 读取图片 d = median_filter(image,9) imgs = np.hstack([image,d]) cv2.imshow('img', imgs) cv2.waitKey(0) /2.png')# 读取图片 d = median_filter(image,9) imgs = np.hstack([image,d]) cv2.imshow('img', imgs) cv2.waitKey(0)

    推荐阅读