噪声的存在严重影响了遥感图像的质量,因此在图像增强处理和分类处理之前,必须予以纠正.图像中各种妨碍人们对其信息接受的因素即可称为图像噪声 。噪声在理论上可以定义为“不可预测,只能用统计方法来认识的随机误差”。
消除噪声成分成为平滑或滤波,他有两个重要的要求,第一是不能破坏图片轮廓边缘的重要信息,第二是图片要清晰,视觉效果好.噪声处理有均值滤波/高斯滤波/中值滤波/双线性滤波
边缘检测算法有差分检测/Reboerts算子/Sobel算子/Prewitt算子/Kirsch算子/Laplace算子
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import PIL.Image as Image
import skimage
from skimage import filters
#实现均值滤波的代码
def imgConvolve(image, kernel):
'''
卷积操作
'''
img_h = int(image.shape[0])
img_w = int(image.shape[1])
kernel_h = int(kernel.shape[0])
kernel_w = int(kernel.shape[1])
#padding 图片的大小不变
padding_h = int((kernel_h - 1) / 2)
padding_w = int((kernel_w - 1) / 2)
convolve_h = int(img_h + 2*padding_h)
convolve_w = int(img_w + 2*padding_w)
#分配空间
img_padding = np.zeros((convolve_h, convolve_w))
#将像素值填充进去
img_padding[padding_h:padding_h+img_h, padding_w:padding_w+img_w] = image[:, :]
image_convolve = np.zeros(image.shape)
#进行卷积
for i in range(padding_h,padding_h+img_h):
for j in range(padding_w,padding_w+img_w):
image_convolve[i-padding_h][j-padding_w] = int(np.sum(img_padding[i-padding_h:i + padding_h+1, j-padding_w:j+padding_w+1]*kernel))
return image_convolve
def avgFilter(image, kernel):
'''
均值滤波
'''
img = imgConvolve(image, kernel)
return img * (1.0 / kernel.size)
#读取图片,灰度处理
data = cv.imread("filter_test.jpg")
data = cv.cvtColor(data, cv.COLOR_BGR2GRAY)
#保存灰度图片
cv.imwrite("real_img.jpg", data, [int(cv.IMWRITE_JPEG_QUALITY), 70])
cv.waitKey(0)
cv.destroyWindows()
kernel_1 = np.ones([3,3])
kernel_2 = np.ones([5,5])
#均值滤波模糊处理
avg_data_1 = avgFilter(data, kernel_1)
avg_data_2 = avgFilter(data, kernel_2)
#保存图片
cv.imwrite('avg_filter_1.jpg',avg_data_1,[int(cv.IMWRITE_JPEG_QUALITY),70]) #jpeg格式,质量为70
cv.imwrite('avg_filter_2.jpg',avg_data_2,[int(cv.IMWRITE_JPEG_QUALITY),70]) #jpeg格式,质量为70
#把保存图片
img1 = cv.imread("avg_filter_1.jpg")
img2 = cv.imread("avg_filter_2.jpg")
imgs = np.hstack([img1, img2])
cv.imwrite("diff_filter.jpg", imgs, [int(cv.IMWRITE_JPEG_QUALITY), 70])
#生成含有噪声图片
rel_img = cv.imread("mouse.jpg") #(412, 550, 3)
#加入高斯噪声
noise_img = skimage.util.random_noise(rel_img, mode='gaussian', seed=None, clip=True)
#salt椒盐滤波
noise_img = skimage.util.random_noise(rel_img, mode='salt', seed=None, clip=True)
plt.figure(figsize=(10, 10))
plt.imshow(noise_img)
plt.xticks([])
plt.yticks([])
plt.savefig("gau_noise_img.jpg")
plt.show()
#opencv实现降噪
img_path = "gau_noise_img.jpg"
img = cv.imread(img_path)
#均值滤波
img1 = cv.blur(img, (5,5))
#高斯滤波
img2 = cv.GaussianBlur(img, (5,5), 0) #0指sigma
#中指滤波
img3 = cv.medianBlur(img, 5)
#双值滤波
img4 = cv.bilateralFilter(img, 9, 75, 75)
titles = ['real', 'mean', 'gaussian', 'median', 'bilateral']
imgs = [img, img1, img2, img3, img4]
plt.figure(figsize=(20, 10))
for i in range(5):
plt.xticks([])
plt.yticks([])
plt.subplot(2, 3, i+1) #下标是从1开始的
plt.imshow(imgs[i])
plt.title(titles[i])
#先save在show,因为如果show后会产生一张新的空白图片,再保存下载的图片就是空白的
#画质70
plt.xticks([])
plt.yticks([])
plt.savefig("real_filter.jpg", quality=100)
plt.show()
#边缘检测
real_img = cv.imread("mouse.jpg")
real_img = cv.cvtColor(real_img, cv.COLOR_BGR2GRAY)
gau_filter_img = cv.imread("gau_noise_img.jpg")
gau_filter_img = cv.cvtColor(gau_filter_img, cv.COLOR_BGR2GRAY)
salt_filter_img = cv.imread("salt_noise_img.jpg")
salt_filter_img = cv.cvtColor(salt_filter_img, cv.COLOR_BGR2GRAY)
#-1表示和原图像相同的深度, 两个1分别是两个方向的边缘检测
real_sobel_img = cv.Sobel(salt_filter_img, -1, 1, 1, ksize=3)
real_prewitt_img = filters.prewitt(salt_filter_img)
titles = ["real", "sobel", "prewitt"]
imgs = [salt_filter_img, real_sobel_img, real_prewitt_img]
plt.figure(figsize=(20, 10))
for i in range(3):
plt.xticks([])
plt.yticks([])
plt.subplot(1,3,i+1)
plt.imshow(imgs[i], cmap="gray") #显示灰度图即可
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.savefig("salt_edge_img.jpg")
plt.show()