OpenCV模糊(平滑)

模糊(平滑)是用于减少图像噪声的常用图像处理操作。 该过程从图像中去除高频内容,如边缘,并使其平滑。

一般而言,通过低通滤波器内核的卷积(图像的每个元素被添加到其本地邻居,由内核加权)图像来实现模糊。

模糊(平滑)

在此操作过程中,图像与盒式过滤器(标准化)进行卷积。 在这个过程中,图像的中心元素被核心区域中所有像素的平均值所取代。

可以使用imgproc类的blur()方法在图像上执行此操作。 以下是这种方法的语法 -

blur(src, dst, ksize, anchor, borderType)

该方法接受以下参数 -

  • src - 表示此操作的源(输入图像)的Mat对象。
  • dst - 表示此操作的目标(输出图像)的Mat对象。
  • ksize - 表示内核大小的Size对象。
  • anchor - 表示定位点的整数类型的变量。
  • borderType - 整数类型的变量,表示要用于输出的边框的类型。

示例

以下程序演示如何对图像执行平滑(模糊)操作。

package com.zyiz.blur;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class BlurTest {
   public static void main(String args[]) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file ="F:/worksp/opencv/images/sample2.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();

      // Creating the Size and Point objects
      Size size = new Size(45, 45);
      Point point = new Point(20, 30);

      // Applying Blur effect on the Image
      Imgproc.blur(src, dst, size, point, Core.BORDER_DEFAULT);

      // blur(Mat src, Mat dst, Size ksize, Point anchor, int borderType)
      // Writing the image
      Imgcodecs.imwrite("F:/worksp/opencv/images/blur.jpg", dst);
      System.out.println("Image processed");
   }
}

假定以下是上述程序中指定的输入图像sample2.jpg

执行上面示例代码,得到以下结果 -


上一篇:OpenCV添加文本

下一篇:OpenCV高斯模糊

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程