如何使用 Java 在 OpenCV 中實現中值模糊?
你可以透過使用低通濾波器濾波影像來模糊影像,這會移除影像中的高頻內容(噪聲、邊緣)。
中值模糊是 OpenCV 提供的一種模糊技術,在移除影像中的椒鹽噪聲方面非常有效。這會將中心元素替換為核心區域內所有畫素的中值。
你可以使用 medianBlur() 方法透過此技術濾波/模糊影像,此方法接受
兩個代表源影像和目標影像的 Mat 物件。
一個代表核心大小的 Size 物件。
示例
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
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;
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;
public class MedianBlurExample 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\logo_noise.jpg";
Mat src = Imgcodecs.imread(file);
//Creating destination matrix
Mat dst = new Mat(src.rows(), src.cols(), src.type());
// Applying MedianBlur on the Image
Imgproc.medianBlur(src, dst, 5);
//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("Median Blur Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}輸入影像

輸出
執行後,上述程式會生成以下影像 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP