How to implement Bilateral blur in OpenCV using Java?

JavaObject Oriented ProgrammingProgramming

You can blur an image by filtering it using a low-pass filter, this removes high frequency content (noise, edges) from an image. Bilateral Filtering is one of the blurring techniques provided by OpenCV, it −

  • removes noise efficiently

  • keeps the edges sharp

  • Comparatively slow

You can apply the bilateral filter on an image using the bilateralFilter() method, this method accepts

  • Two Mat objects representing the source and destination images.

  • An integer representing the diameter of the pixel neighborhood.

  • Two integer variables of the type integer representing the filter sigma in the color space and coordinate space.

  • An Integer object representing the type of the border used.


import java.awt.Image;
import java.awt.image.BufferedImage;
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);
      //Setting the Scene object
      Group root = new Group(imageView);
      Scene scene = new Scene(root, 595, 400);
      stage.setTitle("Bilateral Filter Example");
   public static void main(String args[]) {

Input Image


On executing, the above program generates the following output −

Updated on 09-Apr-2020 08:42:11