OpenCV - 高斯模糊



在高斯模糊操作中,影像與高斯濾波器卷積,而不是盒式濾波器。高斯濾波器是一種低通濾波器,它減少了高頻分量的影響。

您可以使用 imgproc 類的 Gaussianblur() 方法對影像執行此操作。以下是此方法的語法:

GaussianBlur(src, dst, ksize, sigmaX)

此方法接受以下引數:

  • src − 一個 Mat 物件,表示此操作的源(輸入影像)。

  • dst − 一個 Mat 物件,表示此操作的目標(輸出影像)。

  • ksize − 一個 Size 物件,表示核心的大小。

  • sigmaX − 一個雙精度型別變數,表示 X 方向上的高斯核心標準差。

示例

以下程式演示瞭如何在影像上執行高斯模糊操作。

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

public class GaussianTest {
   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 ="C:/EXAMPLES/OpenCV/sample.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();
    
      // Applying GaussianBlur on the Image
      Imgproc.GaussianBlur(src, dst, new Size(45, 45), 0);

      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap9/Gaussian.jpg", dst);
      System.out.println("Image Processed");
   }
}

假設以上程式中指定的輸入影像為 sample.jpg

Sample Image

輸出

執行程式後,您將獲得以下輸出:

Image Processed

如果您開啟指定的路徑,您可以觀察到輸出影像如下所示:

Gaussian Blur
廣告