Java DIP - Enhancing Image Sharpness


Advertisements


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. Parameters
1

source

It is source image.

2

destination

It is destination image.

3

Size

It is Gaussian kernel size.

4

sigmaX

It 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. Parameters
1

src1

It is first input array.

2

alpha

It is weight of the first array elements.

3

src2

It is second input array of the same size and channel number as src1.

4

Beta

It is weight of the second array elements.

5

gamma

It is scalar added to each sum.

6

dst

It 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. Methods
1

cvtColor(Mat src, Mat dst, int code, int dstCn)

It converts an image from one color space to another.

2

dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.

3

equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.

4

filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)

It convolves an image with the kernel.

5

GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)

It blurs an image using a Gaussian filter.

6

integral(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

Enhancing Image Sharpness Tutorial

Sharped Image

Enhancing Image Sharpness Tutorial

Advertisements