如何使用 Java 在 OpenCV 中實現模糊(平均)?


可以透過使用低通濾波器來模糊影像,這會從影像中去除高頻內容(噪點、邊緣)。

平均化是 OpenCV 提供的一種模糊技術,它將中心元素替換為核心區域中所有畫素的平均值。

你可以使用 **blur()** 或 **boxFilter()** 方法透過此技術濾波/模糊影像,**blur()** 方法接受下列內容:

  • 表示源影像和目標影像的兩個 Mat 物件。

  • 表示核心大小的 Size 物件。

  • 表示錨點的整數變數。

  • 表示用於輸出的邊框型別的整數變數。

舉例

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class NormalizedBoxExample extends Application {
   public void start(Stage stage) throws IOException {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading image data
      String file ="D:\Images\lamma2.jpg";
      Mat src = Imgcodecs.imread(file);
      //Creating destination matrix
      Mat dst = new Mat(src.rows(), src.cols(), src.type());
      // 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);
      //Converting matrix to JavaFX writable image
      Image img = HighGui.toBufferedImage(dst);
      WritableImage writableImage= SwingFXUtils.toFXImage((BufferedImage) img, null);
      //Setting the image view
      ImageView imageView = new ImageView(writableImage);
      imageView.setX(10);
      imageView.setY(10);
      imageView.setFitWidth(575);
      imageView.setPreserveRatio(true);
      //Setting the Scene object
      Group root = new Group(imageView);
      Scene scene = new Scene(root, 595, 400);
      stage.setTitle("Normalized Blur Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]) {
      launch(args);
   }
}

輸入影像

輸出

執行上述程式會生成如下輸出:


更新於: 09-Apr-2020

224 次瀏覽

開啟職業生涯 之路

透過完成課程來獲取認證

開始
廣告
© . All rights reserved.