Skip to content
Related Articles

Related Articles

Improve Article

Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )

  • Difficulty Level : Medium
  • Last Updated : 20 Jun, 2017

In this article we will learn how to enhance sharpness of an image using OpenCv library.
In order to enhance sharpness we will use Gaussian filter. Gaussian filter reduces the noise in the image and makes it look better (or higher resolution).

At first we need to setup OpenCV for java, we recommend to use eclipse for the same since it is easy to use and setup.For installation refer to

Methods required for sharpness enhancement.

  1. GaussianBlur(source, destination, new Size(0, 0), sigmaX) – This method resides in Imgproc package of OpenCv.
    Imgproc.GaussianBlur(source, destination, new Size(0, 0), sigmaX)
    source - source image
    destination - destination image
    new Size(0, 0) - Gaussian kernel size
    sigmaX - Gaussian kernel standard deviation in X direction
  2. addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst) - This method resides in Core package of OpenCv. Syntax:
    Core.addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst)
    src1 - first input array
    alpha - weight of the first array elements
    src2 - second input array of the same size and channel number as src1
    beta - weight of the second array elements
    gamma - scalar added to each sum
    dst - output array that has the same size and number of channels as the input arrays
  3. imread() - This method is used to read images as Mat objects which are rendered by OpenCV.

    filename: filename of the image file.If the image is 
    in another directory whole path of image must be mentioned.
  4. imwrite() - This method is used to write Mat objects to image file.
    Imgcodecs.imwrite(filename, mat_img);
    filename: filename of the image file.If the image is 
    in another directory whole path of image must be mentioned.
    mat_img: resultant mat object.

package ocv;
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;
public class Main {
   public static void main( String[] args )
         // 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 );
         // Input image
         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
         Imgcodecs.imwrite("E://output.jpg", destination);
      }catch (Exception e) {

Note: The code will not work in online ide since it requires image in hard drive.




Try to notice 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :