
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to alter the brightness of an image using Java OpenCV library?
The convertTo() method of the org.opencv.core.Mat class performs the required calculations on the given matrix to alter the contrast and brightness of an image. This method accepts 4 parameters −
mat − Empty matrix to hold the result with the same size and type as the source matrix.
rtype − integer value specifying the type of the output matrix. If this value is negative, the type will be the same as the source.
alpha − Gain value, which must be greater than 0 (default value 1).
beta − Bias value (default value 0).
Altering the brightness of an image using OpenCV Java library
As mentioned the beta value passed to this method alters the brightness of an image, if the chosen value for this parameter is a negative value (0 to -255) the brightness of the image is reduced. Similarly, if it is greater than 0 (0 to 255) the brightness of the image is increased.
To alter the contrast of an image invokes the convertTo() method by passing the empty matrix, -1 (to get the same type), 1 as alpha value and, beta value to increase or decrease the brightness.
Example
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; public class AlteringBrightness { public static void main (String[] args) { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the Image from the file String file ="D:\Images\tiger.jpg"; Mat src = Imgcodecs.imread(file, Imgcodecs.IMREAD_COLOR); //Creating an empty matrix Mat dest = new Mat(src.rows(), src.cols(), src.type()); //Increasing the brightness of an image src.convertTo(dest, -1, 1, 100); // Writing the image Imgcodecs.imwrite("D:\Images\altering_brightness_100.jpg", dest); } }
Input image
Following are the various output images for different alpha values −
α-value: -100
α-value: -50
α-value: 50
α-value: 100
- Related Articles
- How to alter the contrast of an image using Java OpenCV library?
- How to alter the sharpness of an image using Java OpenCV library?
- How to write an image using Java OpenCV library?
- How to flip an image using Java OpenCV library?
- How to change the brightness of an image in OpenCV using C++?
- How to decrease the Brightness of an image in OpenCV using C++?
- How to add noise to an image using Java OpenCV library?
- How to add text to an image using Java OpenCV library?
- How to add borders to an image using Java OpenCV library?
- How to change the color spaces of an image using Java OpenCV library?
- How to Detect the key points of an image using OpenCV Java library?
- JavaFX example to decrease the brightness of an image using OpenCV.
- How to detect faces in an image using Java OpenCV library?
- How to draw markers on an image using Java OpenCV library?
- How to get pixels (RGB values) of an image using Java OpenCV library?
