OpenCV - 方框濾波器



方框濾波操作類似於平均模糊操作;它將雙邊影像應用於濾波器。在這裡,您可以選擇方框是否應該被歸一化。

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

boxFilter(src, dst, ddepth, ksize, anchor, normalize, borderType)

此方法接受以下引數:

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

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

  • ddepth − 表示輸出影像深度的整數型別變數。

  • ksize − 表示模糊核大小的Size物件。

  • anchor − 表示錨點的整數型別變數。

  • normalize − 布林型別變數,指定是否應歸一化核心。

  • 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 BoxFilterTest {
   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 = "E:/OpenCV/chap11/filter_input.jpg";
      Mat src = Imgcodecs.imread(file);

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

      // Creating the objects for Size and Point
      Size size = new Size(45, 45);
      Point point = Point(-1, -1);

      // Applying Box Filter effect on the Image
      Imgproc.boxFilter(src, dst, 50, size, point, true, Core.BORDER_DEFAULT);

      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap11/boxfilterjpg", dst);

      System.out.println("Image Processed");
   }
}

假設上述程式中指定了以下輸入影像filter_input.jpg

Filter Input

輸出

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

Image Processed

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

Box Filter
廣告
© . All rights reserved.