Meituan two-sided question
Briefly introduce three filtering ideas:
What they have in common: The three filtering methods can actually be understood as convolution operations, which only filter for a given Filter. If the kernel size is now 3*3 (if the values are all set to 1), 9 cells will be processed. If there is no padding, it will start from the second row by default.
Median filtering: After element-wise multiplication of the 9 points of the candidate area and the 9 points of the filter, the point at the center of the kernel is assigned the median value of the 9 products. If there is a 3*3 kernel (all values are 1) , then after completing the median filtering, the position of 5 is 18 at this time.
Maximum filtering and mean mean that after multiplying 9 elements element by element, the maximum and average of these 9 values are taken.
The idea of coding: considering the motion calculation of padding, stride and sliding window, at the same time, I need to know several functions of numpy. I forgot about it at the time, which was embarrassing.
import numpy as np
def median_filter(input_image,kernel,stride=1,padding=False):
"""
中值滤波/最大滤波/均值滤波
:param input_image: 输入图像
:param filter_size: 滤波器大小
:return:
"""
# 填充(默认为1)
padding_num = 1
if padding:
padding_num = int((kernel.shape[0]-1)/2)
input_image = np.pad(input_image,(padding_num,padding_num),mode="constant",constant_values=0)
out_image = np.copy(input_image)
# 填充后的图像大小
w,h = input_image.shape
print(input_image.shape,padding_num)
for i in range(padding_num,w-padding_num,stride):
for j in range(padding_num,h-padding_num,stride):
region = input_image[i-padding_num:i+padding_num+1,j-padding_num:j+padding_num+1]
print(i,j)
print(region.shape,kernel.shape)
# 确保 图像提取的局部区域 与 核大小 一致
assert (region.shape == kernel.shape)
# 中值滤波np.median, 最大值滤波 np.maximum 均值滤波: np.mean
out_image[i,j] = np.median(np.dot(region,kernel))
# 裁剪原图像大小
if padding:
out_image = out_image[padding_num:w-padding_num,padding_num:h-padding_num]
return out_image
if __name__ == '__main__':
# 随机浮点数, 模仿灰度图
input_image = np.random.rand(16,16)
# 标准正态分布
kernel = np.random.rand(3,3)
print(input_image.shape,kernel.shape)
output = median_filter(input_image,kernel)
print(output.shape)