高斯双边滤波
高斯双边滤波属于非线性滤波,该类滤波被称为图像边缘保留滤波,又称EPF方法,其作用类似于现在的“美颜”、“美图”类产品所呈现的效果。在OpenCV中该类的实现依赖于bilateralFilter() 函数。下面是该函数的声明:
bilateralFilter(src, dst, d, sigmaColor, sigmaSpace);
各参数解释
-
src
表示此操作的源(输入图像)的Mat对象。 -
dst
表示此操作的目标(输出图像)的Mat对象。 -
d
kernal直径大小,一般取0。为0则意味着从sigmaColor自动计算。 -
sigmaColor
颜色权重计算时的参数。 -
sigmaSpace
空间权重计算时所需参数。
通常情况下sigmaColor取100 ~ 150,sigmaSpace取10 ~ 25,其综合效果表现较好。但实际上取值仍依赖于部署情况与物理情况,
Java代码(JavaFX Controller层)
public class Controller{
@FXML private Text fxText;
@FXML private ImageView imageView;
@FXML public void handleButtonEvent(ActionEvent actionEvent) throws IOException {
Node source = (Node) actionEvent.getSource();
Window theStage = source.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPG Files(*.jpg)", "*.jpg"));
File file = fileChooser.showOpenDialog(theStage);
runInSubThread(file.getPath());
}
private void runInSubThread(String filePath){
new Thread(new Runnable() {
@Override
public void run() {
try {
WritableImage writableImage = filterOfBilateral(filePath);
Platform.runLater(new Runnable() {
@Override
public void run() {
imageView.setImage(writableImage);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private WritableImage filterOfBilateral(String filePath) throws IOException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Imgcodecs.imread(filePath);
Mat dst = new Mat();
Imgproc.bilateralFilter(src, dst, 0,150,15);
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", dst, matOfByte);
byte[] bytes = matOfByte.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage bufImage = ImageIO.read(in);
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
}
运行图