如何使用 Java 在 OpenCV 中實現高斯模糊?


你可以使用低通濾波器對影像進行濾波以實現模糊效果,它從影像中去除了高頻內容(噪聲、邊緣)。

高斯模糊是 OpenCV 提供的一種模糊技術,它在去除影像噪聲方面非常高效。它用核心區域中所有畫素的平均值替換中心元素。

你可以使用 GaussianBlur() 方法透過這種技術對影像進行濾波/模糊處理,此方法接受 -

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

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

  • 一個表示 X 方向上的高斯核心標準差的型別 double 的變數。

示例

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.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class GausianBlurExample extends Application {
   public void start(Stage stage) throws IOException {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      String file ="D:\Images\elephant.jpg";
      Mat src = Imgcodecs.imread(file);
      //Creating destination matrix
      Mat dst = new Mat(src.rows(), src.cols(), src.type());
      //Applying GaussianBlur on the Image
      Imgproc.GaussianBlur(src, dst, new Size(15, 15), 0);
      //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("Gaussian Blur Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]) {
      launch(args);
   }
}

輸入影像

輸出

執行以上程式會生成以下輸出 -

更新時間: 2020-04-09

1K+ 瀏覽次數

啟動您的職業

完成課程獲得認證

開始
廣告