图像模糊化
-
缩小
- 长、宽按比例缩小后:img5 = cv.resize(img3, (w, h)) # 缩小
-
卷积
- img2 = cv.filter2D(img1, -1, kernel) #卷积
- 长、宽按比例缩小后:img6 = cv.resize(cv.filter2D(img4, -1, kernel), (w, h)) # 卷积并缩小
- 可以避免高频噪声干扰模型训练
- 同时打印时可以减少锯齿,保留平滑边缘
-
高斯模糊
- img2 = cv.GaussianBlur(img1, ksize=(5, 5), sigmaX=2.0, sigmaY=2.0)
自定义卷积核
- 均值滤波:通过用周围像素的平均值替换每个像素值,实现图像平滑(模糊)
- 自定义一个 kernel 核
- kernel = np.ones((5, 5), np.float32) / 25
- 做卷积操作,第 2 个参数为:ddepth,输出图像深度(-1 表示与输入相同)
- dst = cv.filter2D(img, -1, kernel)
- plt.imshow(cv.cvtColor(dst, cv.COLOR_BGR2RGB)) # 均值滤波
- 自定义一个 kernel 核
高斯滤波
-
- 归一化:生成的权重会归一化,确保总和为 1(避免图像亮度偏移)
- 一维高斯核:a = cv.getGaussianKernel(ksize=5, sigma=1, ktype=cv.CV_64F)
- 二维高斯核: kernel = np.dot(a, a.T)
- 高斯 filter 过滤器
- dst = cv.GaussianBlur(img, ksize=(9, 9), sigmaX=2.0, sigmaY=2.0) # 标准差值越大,越模糊
- plt.imshow(cv.cvtColor(dst, cv.COLOR_BGR2RGB)) # 高斯模糊
- 垂直高斯核
- kernel1 = cv.getGaussianKernel(ksize=9, sigma=2, ktype=cv.CV_64F)
- 水平高斯核
- kernel2 = np.transpose(kernel1)
- 二次卷积操作
- dst = cv.filter2D(cv.filter2D(img, -1, kernel1), -1, kernel2)
- 归一化:生成的权重会归一化,确保总和为 1(避免图像亮度偏移)
中值滤波
-
-
加噪声
- noisy_img = np.random.normal(10, 10, (img.shape[0], img.shape[1],img.shape[2])) # 均值和标准差都为 10
- noisy_img = np.clip(noisy_img, 0, 255).astype(np.uint8) # 截取 0-255 的值
- img = img + noisy_img # 合成原图和噪声图
- 中值滤波
- dst = cv.medianBlur(img, ksize=5)
-
双边滤波
- 双边滤波: 中间的纹理删除,保留边缘信息
- 双边滤波
- dst = cv.bilateralFilter(img, d=9, sigmaColor=75, sigmaSpace=75)
- 双边滤波