如何在 Java 中使用 OpenCV 實現雙邊模糊?


您可以使用低通濾波器對影像進行濾波以模糊影像,這將從影像中去除高頻成分(噪聲、邊緣)。雙邊濾波是 OpenCV 提供的一種模糊技術,它 −

  • 可有效去除噪聲

  • 保持邊緣清晰

  • 相對較慢

您可以使用 bilateralFilter() 方法對影像應用雙邊濾波,該方法接受

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

  • 一個表示畫素鄰域直徑的整數。

  • 兩個表示顏色空間和座標空間中濾波 sigma 的整數變數。

  • 一個表示所用邊框型別的整數物件。

示例

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.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class BilateralFilterExample 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\window.jpg";
      Mat src = Imgcodecs.imread(file);
      //Creating destination matrix
      Mat dst = new Mat(src.rows(), src.cols(), src.type());
      //Applying Bilateral filter
      Imgproc.bilateralFilter(src, dst, 25, 75, 75, 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("Bilateral Filter Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]) {
      launch(args);
   }
}

輸入影像

輸出

執行上述程式將生成以下輸出 −

更新於: 2020 年 4 月 9 日

297 次瀏覽

開啟你的 事業

完成課程獲得認證

開始
廣告