Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Image Processing in Java – Sharpness Enhancement

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article


 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. 


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


  • 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. 


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


  • 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. 



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. 


Imgcodecs.imwrite(filename, mat_img);


  • 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:



// 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
            // Reading the input image from local directory
            // by creating object of Mat class
            Mat source = Imgcodecs.imread(
            Mat destination
                = new Mat(source.rows(), source.cols(),
            // 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
        // Catch block to handle exceptions
        catch (Exception e) {
            // Display message when exception occurs
            System.out.print("Exception/s Occurred")




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 or mail your article to 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
Last Updated : 03 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials