Enhancing Image Sharpness

In this chapter we learn to increase the sharpness of an image using Gaussian filter.

First we 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 are described briefly −

Sr.No.Parameter & Description
1sourceIt is source image.
2destinationIt is destination image.
3SizeIt is Gaussian kernel size.
4sigmaXIt is Gaussian kernel standard deviation in X direction.

Further, we 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 are described below −

Sr.No.Parameter & Description
1src1It is first input array.
2alphaIt is weight of the first array elements.
3src2It is second input array of the same size and channel number as src1.
4BetaIt is weight of the second array elements.
5gammaIt is scalar added to each sum.
6dstIt is output array that has the same size and number of channels as the input arrays.

Apart from the GaussianBlur method, there are other methods provided by the Imgproc class. They are described briefly −

Sr.No.Method & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)It converts an image from one color space to another.
2dilate(Mat src, Mat dst, Mat kernel)It dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)It equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta)It convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)It blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)It calculates the integral of an image.

Example

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 ) {
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Highgui.CV_LOAD_IMAGE_COLOR);
         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) {
      }
   }
}

Output

When you execute the given code, the following output is seen −

Original Image

Sharped Image


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *