Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Image Processing in Java – Sharpness Enhancement

  • Difficulty Level : Medium
  • Last Updated : 03 Feb, 2022

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:

  • Source image
  • Destination image
  • Gaussian kernel size
  • Gaussian kernel standard deviation in X direction

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

Syntax: 

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

Parameters:

  • First input array
  • Weight of the first array elements
  • Second input array of the same size and channel number as src1
  • Weight of the second array elements
  • Scalar added to each sum
  • Output array that has the same size and number of channels as the input arrays

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:

  • Filename of the image file. If the image is in another directory whole path of an image must be mentioned.
  • Resultant mat object.

Implementation: The input image is as follows:

Example

Java




// 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

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!