In this article we will learn how to enhance sharpness of an image using OpenCv library.
In order to enhance sharpness we will use Gaussian filter. Gaussian filter reduces the noise in the image and makes it look better (or higher resolution).
At first we need to setup OpenCV for java, we recommend to use eclipse for the same since it is easy to use and setup.For installation refer to http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/java_eclipse.html
Methods required for sharpness enhancement.
-
GaussianBlur(source, destination, new Size(0, 0), sigmaX) – This method resides in Imgproc package of OpenCv.
Syntax:Imgproc.GaussianBlur(source, destination, new Size(0, 0), sigmaX) parameters: source - source image destination - destination image new Size(0, 0) - Gaussian kernel size sigmaX - Gaussian kernel standard deviation in X direction
-
addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst) - This method resides in Core package of OpenCv.
Syntax:
Core.addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst) parameters: src1 - first input array alpha - weight of the first array elements src2 - second input array of the same size and channel number as src1 beta - weight of the second array elements gamma - scalar added to each sum dst - output array that has the same size and number of channels as the input arrays
- imread() - This method is used to read images as Mat objects which are rendered by OpenCV.
Syntax:
Imgcodecs.imread(filename); parameters: filename: filename of the image file.If the image is in another directory whole path of image must be mentioned.
- imwrite() - This method is used to write Mat objects to image file.
Syntax:Imgcodecs.imwrite(filename, mat_img); parameters: filename: filename of the image file.If the image is in another directory whole path of image must be mentioned. mat_img: resultant mat object.
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 Main { public static void main( String[] args ) { try { // For proper execution of native libraries // Core.NATIVE_LIBRARY_NAME must be loaded before // calling any of the opencv methods System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Input image Mat source = Mat destination = new Mat(source.rows(), source.cols(), source.type()); // filtering Imgproc.GaussianBlur(source, destination, new Size( 0 , 0 ), 10 ); Core.addWeighted(source, 1.5 , destination, - 0.5 , 0 , destination); // writing output image } catch (Exception e) { } } } |
Note: The code will not work in online ide since it requires image in hard drive.
Output:
input.jpg
![]()
output.jpg
Try to notice minor improvement in resolution
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments