Open In App

Image Processing in Java – Sharpness Enhancement

Prerequisites:

 Tip: It is recommended to use eclipse for the same since it is easy to use and set up.



Here, we will use a Gaussian filter. This filter reduces the noise in the image and makes it look better (or higher resolution). Go through the pre-requisite of installing OpenCV and setup as per OS in your local machine in order to write code as we will be importing libraries. So let us discuss prior methods required for sharpness enhancement. 

Method 1: GaussianBlur(): This method resides in Imgproc package of OpenCv. 



Syntax: 

Imgproc.GaussianBlur(source, destination, new Size(0, 0), sigmaX)

Parameters:

Method 2: addWeighted(): This method resides in the Core package of OpenCV. 

Syntax: 

Core.addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst)

Parameters:

Method 3: imread(): It is used to read images as Mat objects which are rendered by OpenCV. 

Syntax: 

Imgcodecs.imread(filename);

Parameters: Filename of the image file. If the image is in another directory whole path of the image must be mentioned.

Method 4: imwrite():  It is used to write Mat objects to an image file. 

Syntax: 

Imgcodecs.imwrite(filename, mat_img);

Parameters:

Implementation: The input image is as follows:

Example




// Java Program to Enhance Sharpness in An Image
// Using OpenCV Library
 
// Importing package module
package ocv;
 
// Importing require classes
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
 
            // For proper execution of native libraries
            // Core.NATIVE_LIBRARY_NAME must be loaded
            // before calling any of the opencv methods
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 
            // Reading the input image from local directory
            // by creating object of Mat class
            Mat source = Imgcodecs.imread(
                "E://input.jpg",
                Imgcodecs.CV_LOAD_IMAGE_COLOR);
           
            Mat destination
                = new Mat(source.rows(), source.cols(),
                          source.type());
 
            // Filtering
            Imgproc.GaussianBlur(source, destination,
                                 new Size(0, 0), 10);
            Core.addWeighted(source, 1.5, destination, -0.5,
                             0, destination);
 
            // Writing output image to directory in the local
            // system
            Imgcodecs.imwrite("E://output.jpg",
                              destination);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Display message when exception occurs
            System.out.print("Exception/s Occurred")
        }
    }
}

 
 

Output:

 

Note: Do notice a minor improvement in resolution

 


Article Tags :