Enhancing Image Sharpness Tutorial


In this chapter we are going to increase the sharpness of an image using gaussian filter.

First we are going to use OpenCV function GaussianBlur. It can be found under Imgproc package. Its syntax is given below:

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

The parameters along with their description are listed below:

Sr.NoParameters & description
The source image.
The destination image.
Gaussian kernel size.
Gaussian kernel standard deviation in X direction.

After that We are going to use OpenCV function addWeighted to apply image watermark to image. It can be found under Core package. Its syntax is given below:

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

The parameters of this function along with their description are listed below:

Sr.NoParamaters & Description
First input array.
The weight of the first array elements.
The second input array of the same size and channel number as src1.
The 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.

Apart from the GaussianBlur method, there are other methods provide by the Imgproc class. They are listed below:

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
Calculates the integral of an image.


The following example demonstrates the use of Imgproc and Core class to apply sharpening to an image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Main {
   public static void main( String[] args )
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         Imgproc.GaussianBlur(source, destination, new Size(0,0), 10);
         Core.addWeighted(source, 1.5, destination, -0.5, 0, destination);
         Highgui.imwrite("sharp.jpg", destination);
        }catch (Exception e) {


Original Image

Enhancing Image Sharpness Tutorial

Sharped Image:

Enhancing Image Sharpness Tutorial