OpenCV - 模糊處理(平均)



模糊處理(平滑)是常用的影像處理操作,用於減少影像噪聲。此過程去除影像中的高頻內容(如邊緣),使其平滑。

通常,模糊處理是透過使用低通濾波器核心對影像進行卷積(影像的每個元素與其區域性鄰居相加,並由核心加權)來實現的。

模糊處理(平均)

在此操作期間,影像與方框濾波器(歸一化)進行卷積。在此過程中,影像的中心元素被核心區域中所有畫素的平均值所替換。

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

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

此方法接受以下引數:

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

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

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

  • **anchor** - 一個整數型別變數,表示錨點。

  • **borderType** - 一個整數型別變數,表示要用於輸出的邊界型別。

示例

以下程式演示瞭如何在影像上執行平均(模糊)操作。

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 ="C:/EXAMPLES/OpenCV/sample.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("E:/OpenCV/chap9/blur.jpg", dst);
      System.out.println("Image processed");
   }
}

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

Sample Image

輸出

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

Image Processed

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

Blur (Averaging)
廣告
© . All rights reserved.