Enhancing Image Sharpness Tutorial

Advertisements


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
1source
The source image.
2destination
The destination image.
3Size
Gaussian kernel size.
4sigmaX
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
1src1
First input array.
2alpha
The weight of the first array elements.
3src2
The second input array of the same size and channel number as src1.
4beta
The weight of the second array elements.
5gamma
Scalar added to each sum.
6dst
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.

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

Original Image

Enhancing Image Sharpness Tutorial

Sharped Image:

Enhancing Image Sharpness Tutorial

Advertisements
Advertisements