Prerequisites:
- Image Processing in Java – Read and Write
- Image Processing In Java – Get and Set Pixels
- Image Processing in Java – Colored Image to Grayscale Image Conversion
- Image Processing in Java – Colored Image to Negative Image Conversion
- Image Processing in Java – Colored to Red Green Blue Image Conversion
- Image Processing in Java – Colored Image to Sepia Image Conversion
- Image Processing in Java – Creating a Random Pixel Image
- Image Processing in Java – Creating a Mirror Image
- Image Processing in Java – Face Detection
- Image Processing in Java – Watermarking an Image
- Image Processing in Java – Changing Orientation of Image
- Image Processing in Java – Contrast Enhancement
The brightness of an image can be enhanced by multiplying each pixel of the image with an alpha value and then adding a beta value to it.
Enhancing the Brightness of an Image Using Java
At first, we need to set up OpenCV for Java, we recommend using eclipse for the same since it is easy to use and set up. Now let us discuss a specific method. been used for brightness enhancement are as follows:
Method 1: convertTo(destination, rtype, alpha, beta): It resides in Mat package of OpenCV.
Syntax:
sourceImage.convertTo(destination, rtype, alpha, beta);
Parameters:
- Destination image
- Desired output matrix type
- Optional scale factor multiplied to each pixel of source image
- Optional beta value added to the scaled values.
Method 2: 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 2: imwrite(): This method 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: We will illustrate images alongside output images in order to showcase the difference.
Java
// Java Program to Enhance Brightness of An Image // Using OpenCV Library // Importing package module to this code fragment package ocv; // Importing required classes import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; // Main class public class GFG { // Initializing variables for an image static int width; static int height; static double alpha = 1 ; static double beta = 50 ; // Main driver method public static void main(String[] args) { // Try block to check for exceptions 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); // Getting input image by // creating object of Mat class from local // directory Mat source = Imgcodecs.imread( Imgcodecs.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(), source.cols(), source.type()); // Applying brightness enhancement source.convertTo(destination, - 1 , alpha, beta); // Output image destination); } // Catch block to handle exceptions catch (Exception e) { // Print the exception on console // using getMessage() method System.out.println( "error: " + e.getMessage()); } } } |
Output:
Brightness Enhancement
Use-case 1: Input image
Alpha = 1, Beta = 50
Output 1:
Alpha = 2, Beta = 50
Output 2:
Alpha = 2, Beta = 25
Output 3:
This article is contributed by Pratik Agarwal. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.