Enhancing Image Brightness Tutorial


In this chapter we are going to enhance the brightness of an image by multiplying each pixel of the image with an alpha value and adding another beta value to it.

We are going to use OpenCV function convertTo that does the above operation automatically. It can be found under Mat package. Its syntax is given below:

int alpha = 2;
int beta = 50;
sourceImage.convertTo(destination, rtype , alpha, beta);		 

The parameters along with their description are listed below:

Sr.NoParameters & description
Destination image
Desired output matrix type or, rather, the depth since the number of channels are the same as the input has. if rtype is negative, the output matrix will have the same type as the input.
Optional scale factor.
Optional delta added to the scaled values.

Apart from the convertTo method, there are other methods provide by the Mat class. They are listed below:

Sr.NoMethod & Description
1adjustROI(int dtop, int dbottom, int dleft, int dright)
Adjusts a submatrix size and position within the parent matrix.
2copyTo(Mat m)
Copies the matrix to another one.
Extracts a diagonal from a matrix, or creates a diagonal matrix.
4dot(Mat m)
Computes a dot-product of two vectors.
5reshape(int cn)
Changes the shape and/or the number of channels of a 2D matrix without copying the data.
6submat(Range rowRange, Range colRange)
Extracts a rectangular sub matrix.


The following example demonstrates the use of Mat class to enhance brightness of an image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

   public class Main {
      static int width;
      static int height;
      static double alpha = 2;
      static double beta = 50;
      public static void main( String[] args )
            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
            Mat source = Highgui.imread("digital_image_processing.jpg",
            Mat destination=new Mat(source.rows(),source.cols(),
            source.convertTo(destination, -1, alpha, beta);
            Highgui.imwrite("brightWithAlpha2Beta50.jpg", destination);
           }catch (Exception e) {
              System.out.println("error: " + e.getMessage());


Original Image

Enhancing Image Brightness Tutorial

Enhanced Bright Image (Alpha=1 & Beta=50)

Enhancing Image Brightness Tutorial

Enhanced Bright Image (Alpha=2 & Beta=50)

Enhancing Image Brightness Tutorial