Skip to content
Related Articles

Related Articles

Improve Article

Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)

  • Difficulty Level : Medium
  • Last Updated : 20 Jun, 2017

In this article we will learn how to enhance brightness of an image using OpenCv library. 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.
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 here

Methods required for brightness enhancement:

  1. convertTo(destination, rtype, alpha, beta) : This method resides in Mat package of OpenCv.
    sourceImage.convertTo(destination, rtype, alpha, beta);
    destination: is the destination image
    rtype:  is the desired output matrix type
    alpha:  is optional scale factor multiplied 
    to each pixel of source image
    beta:  it is optional beta value added to the scaled values.
  2. imread() – This method is used to read images as Mat objects which are rendered by OpenCV.
    filename: filename of the image file.If the image is 
    in another directory whole path of image must be mentioned.
  3. imwrite() – This method is used to write Mat objects to image file.

    Imgcodecs.imwrite(filename, mat_img);
    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.imgcodecs.Imgcodecs;
public class Main {
   static int width;
   static int height;
   static double alpha = 1;
   static double beta = 50;
   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 =  
          Imgcodecs.imread("E://input.jpg", Imgcodecs.CV_LOAD_IMAGE_COLOR);
         Mat destination = new Mat(source.rows(), source.cols(), source.type());
         // applying brightness enhacement
         source.convertTo(destination, -1, alpha, beta);
         // output image
         Imgcodecs.imwrite("E://output.jpg", destination);
      } catch (Exception e) {
         System.out.println("error: " + e.getMessage());

Note: The code will not work in online ide since it requires image in hard drive.


Alpha = 1, Beta = 50


Alpha = 2, Beta = 50


Alpha = 2, Beta = 25


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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :