Wednesday, July 3, 2024
HomeLanguagesJavaImage Processing in Java – Brightness Enhancement

Image Processing in Java – Brightness Enhancement

Prerequisites:

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. 

Syntax: 

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

Parameters:

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

Syntax: 

Imgcodecs.imread(filename);

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. 

Syntax:

Imgcodecs.imwrite(filename, mat_img); 

Parameters:

  • 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




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


Output: 

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 Lazyroar and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the Lazyroar 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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments