Image Processing in Java – Sharpness Enhancement
Last Updated :
03 Feb, 2022
Prerequisites:
Tip: It is recommended to use eclipse for the same since it is easy to use and set up.
Here, we will use a Gaussian filter. This filter reduces the noise in the image and makes it look better (or higher resolution). Go through the pre-requisite of installing OpenCV and setup as per OS in your local machine in order to write code as we will be importing libraries. So let us discuss prior methods required for sharpness enhancement.
Method 1: GaussianBlur(): This method resides in Imgproc package of OpenCv.
Syntax:
Imgproc.GaussianBlur(source, destination, new Size(0, 0), sigmaX)
Parameters:
- Source image
- Destination image
- Gaussian kernel size
- Gaussian kernel standard deviation in X direction
Method 2: addWeighted(): This method resides in the Core package of OpenCV.
Syntax:
Core.addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst)
Parameters:
- First input array
- Weight of the first array elements
- Second input array of the same size and channel number as src1
- 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
Method 3: imread(): It is used to read images as Mat objects which are rendered by OpenCV.
Syntax:
Imgcodecs.imread(filename);
Parameters: Filename of the image file. If the image is in another directory whole path of the image must be mentioned.
Method 4: imwrite(): It is used to write Mat objects to an image file.
Syntax:
Imgcodecs.imwrite(filename, mat_img);
Parameters:
- Filename of the image file. If the image is in another directory whole path of an image must be mentioned.
- Resultant mat object.
Implementation: The input image is as follows:
Example
Java
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 GFG {
public static void main(String[] args)
{
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat source = Imgcodecs.imread(
Imgcodecs.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);
destination);
}
catch (Exception e) {
System.out.print( "Exception/s Occurred" )
}
}
}
|
Output:
Note: Do notice a minor improvement in resolution
Share your thoughts in the comments
Please Login to comment...