#convolve
def imgConvolve(img,kernel):
rows,cols=img.shape
kernel_rows,kernel_cols=kernel.shape
padding_rows,padding_cols=int((kernel_rows-1)/2),int((kernel_cols-1)/2)
convolve_rows=rows+2*padding_rows
convolve_cols=cols+2*padding_cols
img_padding=np.zeros((convolve_rows,convolve_cols))
img_padding[padding_rows:padding_rows+rows,padding_cols:padding_cols+cols]=img[:,:]
img_convolve=np.zeros((rows,cols))
for i in range(padding_rows,padding_rows+rows):
for j in range(padding_cols,padding_cols+cols):
img_convolve[i-padding_rows][j-padding_cols]=int(np.sum(img_padding
[i-padding_rows:i+padding_rows+1,
j-padding_cols:j+padding_cols+1]*kernel))
return img_convolve
线性滤波算法,用模板中的全体像素的平均值来代替原来像素值,对噪声图像特别是有大的孤立点的图像非常敏感,即使有极少数量点存在较大差异也会导致平均值的明显波动
def imgAverageFilter(img,kernel):
return imgConvolve(img,kernel)*(1.0/kernel.size)
kernel=np.random.randint(3,size=(3,3))
img=cv2.imread('IMG_4470.JPG',0)
new_img=imgAverageFilter(img,kernel)
cv2.imwrite('IMG_4470_Average_33.JPG',new_img)
kernel=np.random.randint(3,size=(5,5))
new_img=imgAverageFilter(img,kernel)
cv2.imwrite('IMG_4470_Average_55.JPG',new_img)
kernel=np.random.randint(3,size=(7,7))
new_img=imgAverageFilter(img,kernel)
cv2.imwrite('IMG_4470_Average_77.JPG',new_img)
img=cv2.imread('IMG_4470_gamma_noise.jpg',0)
kernel=np.random.randint(3,size=(3,3))
new_img=imgAverageFilter(img,kernel)
cv2.imwrite('IMG_4470_gamma_noise_aveagefilter.jpg',new_img)
线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程,对整幅图像进行加权平均,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到
def imgGaussianFilter(img,kernel):
return imgConvolve(img,kernel)*(1.0/np.sum(kernel))
kernel=np.random.randint(3,size=(3,3))
new_img=imgGaussianFilter(img,kernel)
cv2.imwrite('IMG_4470_Gaussian_33.JPG',new_img)
kernel=np.random.randint(3,size=(5,5))
new_img=imgGaussianFilter(img,kernel)
cv2.imwrite('IMG_4470_Gaussian_55.JPG',new_img)
kernel=np.random.randint(3,size=(7,7))
new_img=imgGaussianFilter(img,kernel)
cv2.imwrite('IMG_4470_Gaussian_77.JPG',new_img)
img=cv2.imread('IMG_4470_GaussianNoise.jpg',0)
kernel=np.random.randint(3,size=(3,3))
new_img=imgGaussianFilter(img,kernel)
cv2.imwrite('IMG_4470_Gaussianfilter_33.jpg',new_img)
kernel=np.random.randint(5,size=(7,7))
new_img=imgGaussianFilter(img,kernel)
cv2.imwrite('IMG_4470_Gaussianfilter_77.jpg',new_img)
中值滤波,非线性平滑技术,优点:避免图像孤立噪声点的影响,保护图像边缘信息,可以很好的过滤掉椒盐噪声;缺点是易造成图像的不连续性
def imgMedianFilter(img,kernel):
rows,cols=img.shape
kernel_rows,kernel_cols=kernel.shape
padding_rows,padding_cols=int((kernel_rows-1)/2),int((kernel_cols-1)/2)
convolve_rows=rows+2*padding_rows
convolve_cols=cols+2*padding_cols
img_padding=np.zeros((convolve_rows,convolve_cols))
img_padding[padding_rows:padding_rows+rows,padding_cols:padding_cols+cols]=img[:,:]
img_median=np.zeros((rows,cols))
for i in range(padding_rows,padding_rows+rows):
for j in range(padding_cols,padding_cols+cols):
img_median[i-padding_rows][j-padding_cols]=int(np.median(img_padding
[i-padding_rows:i+padding_rows+1,
j-padding_cols:j+padding_cols+1]))
return img_median
kernel=np.random.randint(3,size=(3,3))
new_img=imgMedianFilter(img,kernel)
cv2.imwrite('IMG_4470_median_33.JPG',new_img)
kernel=np.random.randint(3,size=(5,5))
new_img=imgMedianFilter(img,kernel)
cv2.imwrite('IMG_4470_median_55.JPG',new_img)
kernel=np.random.randint(3,size=(7,7))
new_img=imgMedianFilter(img,kernel)
cv2.imwrite('IMG_4470_median_77.JPG',new_img)
kernel=np.random.randint(5,size=(7,7))
new_img=imgMedianFilter(img,kernel)
cv2.imwrite('IMG_4470_median_77_5.JPG',new_img)
kernel=np.random.randint(7,size=(7,7))
new_img=imgMedianFilter(img,kernel)
cv2.imwrite('IMG_4470_median_77_7.JPG',new_img)
kernel=np.random.randint(3,size=(3,3))
new_img=imgMedianFilter(img,kernel)
cv2.imwrite('IMG_4470_PepperandSalt_median.jpg',new_img)