Open In App
Related Articles

Image Processing in Java – Brightness Enhancement

Improve Article
Save Article
Like Article


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. 


sourceImage.convertTo(destination, rtype, alpha, beta);


  • 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. 



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. 


Imgcodecs.imwrite(filename, mat_img); 


  • 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 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
            // Getting input image by
            // creating object of Mat class from local
            // directory
            Mat source = Imgcodecs.imread(
            Mat destination
                = new Mat(source.rows(), source.cols(),
            // Applying brightness enhancement
            source.convertTo(destination, -1, alpha, beta);
            // Output image
        // Catch block to handle exceptions
        catch (Exception e) {
            // Print the exception on console
            // using getMessage() method
            System.out.println("error: " + e.getMessage());


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 or mail your article to 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.

Last Updated : 14 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials