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
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
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.