Computer >> Máy Tính >  >> Lập trình >> Java

Làm thế nào để triển khai làm mờ Gaussian trong OpenCV bằng Java?


Bạn có thể làm mờ hình ảnh bằng cách lọc hình ảnh đó bằng bộ lọc thông thấp, điều này sẽ loại bỏ nội dung tần số cao (nhiễu, các cạnh) khỏi hình ảnh.

Gaussian Blurring là một trong những kỹ thuật làm mờ được cung cấp bởi OpenCV, nó có hiệu quả cao trong việc loại bỏ nhiễu của hình ảnh. Điều này thay thế phần tử trung tâm bằng giá trị trung bình của tất cả các pixel trong vùng nhân.

Bạn có thể lọc / làm mờ hình ảnh bằng kỹ thuật này bằng cách sử dụng GaussianBlur () phương thức này chấp nhận -

  • Hai đối tượng Mat đại diện cho hình ảnh nguồn và hình ảnh đích.

  • Một đối tượng Size đại diện cho kích thước của hạt nhân.

  • Một biến kiểu double đại diện cho độ lệch chuẩn của hạt nhân Gaussian theo hướng X.

Ví dụ

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);
   }
}

Hình ảnh đầu vào

Làm thế nào để triển khai làm mờ Gaussian trong OpenCV bằng Java?

Đầu ra

Khi thực thi, chương trình trên tạo ra kết quả sau -

Làm thế nào để triển khai làm mờ Gaussian trong OpenCV bằng Java?