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:
- convertTo(destination, rtype, alpha, beta) : This method resides in Mat package of OpenCv.
sourceImage.convertTo(destination, rtype, alpha, beta); parameters: 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.
- imread() – This method is used to read images as Mat objects which are rendered by OpenCV.
Imgcodecs.imread(filename); parameters: filename: filename of the image file.If the image is in another directory whole path of image must be mentioned.
- imwrite() – This method is used to write Mat objects to image file.
Imgcodecs.imwrite(filename, mat_img); parameters: 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.
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
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.