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

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 http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/java_eclipse.html

Methods required for sharpness enhancement.



  1. GaussianBlur(source, destination, new Size(0, 0), sigmaX) – This method resides in Imgproc package of OpenCv.
    Syntax:

    Imgproc.GaussianBlur(source, destination, new Size(0, 0), sigmaX)
    parameters:
    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)
    parameters:
    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.
    Syntax:

    Imgcodecs.imread(filename);
    parameters:
    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.
    Syntax:

    Imgcodecs.imwrite(filename, mat_img);
    parameters:
    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.
    
filter_none

edit
close

play_arrow

link
brightness_4
code

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 )
   {
      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 );
  
         // 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) {
      }
   }
}

chevron_right


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

Output:


input.jpg

output.jpg

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 contribute.geeksforgeeks.org or mail your article to contribute@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


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.