How to apply blur to an image using Java



Problem Description

How to apply blur to an image using Java.

Solution

Following is the program to apply blur to an image using Java.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class BlurToAnImage {
   public static void main(String args[]) {
      
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      
      //Reading the Image from the file and storing it in to a Matrix object
      String file = "C:/opencv/logo.jpg";
      Mat src="/?originalUrl=https%3A%2F%2Fdev.tutorialspoint.com%2FImgcodecs.imread(file)%3B%2520%2520%2520%2520%2520%2520%2F%2FCreating%2520an%2520empty%2520matrix%2520to%2520store%2520the%2520result%2520%2520%2520%2520%2520%2520Mat%2520dst%2520%3D%2520new%2520Mat()%3B%2520%2520%2520%2520%2520%2520%2F%2FApplying%2520Blur%2520effect%2520on%2520the%2520Image%2520%2520%2520%2520%2520%2520Imgproc.blur(src%2C%2520dst%2C%2520new%2520Size(45%2C%252045)%2C%2520new%2520Point(20%2C%252030)%2C%2520Core.BORDER_DEFAULT)%3B%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2F%2Fblur(Mat%2520src%2C%2520Mat%2520dst%2C%2520Size%2520ksize%2C%2520Point%2520anchor%2C%2520int%2520borderType)%2520%2520%2520%2520%2520%2520%2F%2FWriting%2520the%2520image%2520%2520%2520%2520%2520%2520Imgcodecs.imwrite("C:/opencv/blurOP.jpg", dst);
      System.out.println("Image processed");
   }
}

Input

OpenCV Copy Input

Output

Blured Image
java_opencv
Advertisements