Prerequisites:
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
package ocv;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
public class GFG {
static int width;
static int height;
static double alpha = 1 ;
static double beta = 50 ;
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());
source.convertTo(destination, - 1 , alpha, beta);
destination);
}
catch (Exception e) {
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 GeeksforGeeks 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 GeeksforGeeks 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.