### Change Brightness

Changing brightness of an image is a commonly used point operation. In this operation, the value of each and every pixels in an image should be increased/decreased by a constant. To change the brightness of a video, the same operation should be performed on each frame in the video.

If you want to increase the brightness of an image, you have to add some positive constant value to each and every pixel in the image.

new_image (i, j) = image(i, j) + c

If you want to decrease the brightness of an image, you have to subtract some positive constant value from each and every pixel in the image.

new_image (i, j) = image(i, j) - c

e.g- Say, this is your original image. Let's assume the data type of the image is CV_8U. (i.e. - Pixels in the image is 8 bit unsigned. Please refer OpenCV C++ API for more information.) Hence the valid value range of every pixels in the image should be 0 - 255.
 Original Image

Say, you want to increase the brightness of the original image by 60. Therefore you should add 60 to each pixels in the original image. You must make sure that pixel values in the output image should not exceed the maximum allowable limit by adding 60 to the original image. If it exceeds the maximum limit, you must assign the maximum value instead of the correct value.

Here is the output image of which the brightness is increased by 60. You may have already noticed that the pixel value at (3, 1) location is 255, although it should be 260 after adding 60 to the 200. It is because the maximum allowable pixel value of this image is 255.

 Image of which the brightness is increased by 60

Say, you want to decrease the brightness of the original image by 20. Therefore you should subtract 20 from each pixels in the original image. You must make sure that pixel values in the output image should not go below the minimum allowable limit after subtracting 20 from the original image. If it goes below the minimum limit, you must assign the minimum value instead of the correct value.

Here is the output image of which the brightness is decreased by 20. You may have already noticed that the pixel value at (0, 0) location is 0, although it should be -8 after subtracting 20 from 12. It is because the minimum allowable pixel value is 0 for images with unsigned data types.
 Image of which brightness is decreased by 20

### Change the Brightness of an Image with OpenCV

Now I am going to show you how to increase and decrease the brightness of an image using an OpenCV C++ example.

It is recommended to go through the Load & Display Image first in order to understand the following example better.
```//Uncomment the following line if you are compiling this code in Visual Studio
//#include "stdafx.h"

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
Mat image = imread("D:/My OpenCV Website/My Guitar.jpg");

// Check for failure
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //wait for any key press
return -1;
}

Mat imageBrighnessHigh50;
image.convertTo(imageBrighnessHigh50, -1, 1, 50); //increase the brightness by 50

Mat imageBrighnessHigh100;
image.convertTo(imageBrighnessHigh100, -1, 1, 100); //increase the brightness by 100

Mat imageBrighnessLow50;
image.convertTo(imageBrighnessLow50, -1, 1, -50); //decrease the brightness by 50

Mat imageBrighnessLow100;
image.convertTo(imageBrighnessLow100, -1, 1, -100); //decrease the brightness by 100

//Defining window names for above images
String windowNameOriginalImage = "Original Image";
String windowNameBrightnessHigh50 = "Brightness Increased by 50";
String windowNameWithBrightnessHigh100 = "Brightness Increased by 100";
String windowNameBrightnessLow50 = "Brightness Decreased by 50";
String windowNameBrightnessLow100 = "Brightness Decreased by 100";

//Create and open windows for above images
namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
namedWindow(windowNameBrightnessHigh50, WINDOW_NORMAL);
namedWindow(windowNameWithBrightnessHigh100, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow50, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow100, WINDOW_NORMAL);

//Show above images inside the created windows.
imshow(windowNameOriginalImage, image);
imshow(windowNameBrightnessHigh50, imageBrighnessHigh50);
imshow(windowNameWithBrightnessHigh100, imageBrighnessHigh100);
imshow(windowNameBrightnessLow50, imageBrighnessLow50);
imshow(windowNameBrightnessLow100, imageBrighnessLow100);

waitKey(0); // Wait for any key stroke

destroyAllWindows(); //destroy all open windows

return 0;
}
```
Copy and paste the above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/My Guitar.jpg" in the code with a valid location to an image in your computer. Then you should see a set of images like the below.

 Brightness Increased

 Brightness Decreased

#### Explanation

Let's go through the above example line by line.

```// Read the image file
Mat image = imread("D:/My OpenCV Website/My Guitar.jpg");

// Check for failure
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //wait for any key press
return -1;
}
```
This code segment loads an image from the specified file. If it is failed to load the image, the program will exit.

```Mat imageBrighnessHigh50;
image.convertTo(imageBrighnessHigh50, -1, 1, 50); //increase the brightness by 50

Mat imageBrighnessHigh100;
image.convertTo(imageBrighnessHigh100, -1, 1, 100); //increase the brightness by 100

Mat imageBrighnessLow50;
image.convertTo(imageBrighnessLow50, -1, 1, -50); //decrease the brightness by 50

Mat imageBrighnessLow100;
image.convertTo(imageBrighnessLow100, -1, 1, -100); //decrease the brightness by 100
```
The above code segment will increase pixel values by the specified amount and store it in the given output image. If the specified value is positive, the brightness of the output image will be increased. If the specified value is negative, the brightness of the output image will be decreased.

void Mat::convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const

This function converts the each pixel value to the target data type and changes the value as per the following formula.

pixel_value_of_output_image(x, y) = pixel_value_of_input_image(x, y) * alpha + beta;
1. m - Output image. This data structure will be reallocated if required.

2. rtype - Type of the output image. If rtype is a negative value, the type of the output image will be same as the input image.

3. alpha - Each pixels in the input image will be multiplied by this number before assigning to the output image.

4. beta - This value will be added to each pixels in the input image and assigned to the output image.

```//Defining window names for above images
String windowNameOriginalImage = "Original Image";
String windowNameBrightnessHigh50 = "Brightness Increased by 50";
String windowNameWithBrightnessHigh100 = "Brightness Increased by 100";
String windowNameBrightnessLow50 = "Brightness Decreased by 50";
String windowNameBrightnessLow100 = "Brightness Decreased by 100";

//Create and open windows for above images
namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
namedWindow(windowNameBrightnessHigh50, WINDOW_NORMAL);
namedWindow(windowNameWithBrightnessHigh100, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow50, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow100, WINDOW_NORMAL);

//Show above images inside the created windows.
imshow(windowNameOriginalImage, image);
imshow(windowNameBrightnessHigh50, imageBrighnessHigh50);
imshow(windowNameWithBrightnessHigh100, imageBrighnessHigh100);
imshow(windowNameBrightnessLow50, imageBrighnessLow50);
imshow(windowNameBrightnessLow100, imageBrighnessLow100);
```
The above code snippet will create windows and show images in them.

```waitKey(0); // Wait for any key stroke

destroyAllWindows(); //destroy all open windows
```
The above code segment will wait until any key is pressed. After the key is pressed, all created windows will be destroyed.

#### Summary

With the above example, you have learnt

• How to load an image from a file
• How to increase and decrease the brightness of an image
• How to create windows and show images in them
• How to keep you program waiting for a key press
• How to destroy all opened windows

### Change the Brightness of a Video with OpenCV

Now I am going to show you how to increase and decrease the brightness of a video using an OpenCV C++ example. This is pretty much similar to the previous example.

It is recommended to go through the Play Video from File or Camera first in order to understand the following example better.
```//Uncomment the following line if you are compiling this code in Visual Studio
//#include "stdafx.h"

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
//open the video file for reading
VideoCapture cap("D:/My OpenCV Website/A Herd of Deer Running.mp4");

// if not success, exit program
if (cap.isOpened() == false)
{
cout << "Cannot open the video file" << endl;
cin.get(); //wait for any key press
return -1;
}

//Defining window names
String windowNameOriginalVideo = "Original Video";
String windowNameBrightnessHigh50 = "Brightness Increased by 50";
String windowNameWithBrightnessHigh100 = "Brightness Increased by 100";
String windowNameBrightnessLow50 = "Brightness Decreased by 50";
String windowNameBrightnessLow100 = "Brightness Decreased by 100";

//Create and open windows for above window names
namedWindow(windowNameOriginalVideo, WINDOW_NORMAL);
namedWindow(windowNameBrightnessHigh50, WINDOW_NORMAL);
namedWindow(windowNameWithBrightnessHigh100, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow50, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow100, WINDOW_NORMAL);

while (true)
{
Mat frame;

//Breaking the while loop at the end of the video
if (bSuccess == false)
{
cout << "Found the end of the video" << endl;
break;
}

Mat frameBrighnessHigh50;
frame.convertTo(frameBrighnessHigh50, -1, 1, 50); //increase the brightness by 50

Mat frameBrighnessHigh100;
frame.convertTo(frameBrighnessHigh100, -1, 1, 100); //increase the brightness by 100

Mat frameBrighnessLow50;
frame.convertTo(frameBrighnessLow50, -1, 1, -50); //decrease the brightness by 50

Mat frameBrighnessLow100;
frame.convertTo(frameBrighnessLow100, -1, 1, -100); //decrease the brightness by 100

//Show above frames inside the created windows.
imshow(windowNameOriginalVideo, frame);
imshow(windowNameBrightnessHigh50, frameBrighnessHigh50);
imshow(windowNameWithBrightnessHigh100, frameBrighnessHigh100);
imshow(windowNameBrightnessLow50, frameBrighnessLow50);
imshow(windowNameBrightnessLow100, frameBrighnessLow100);

//wait for for 10 ms until any key is pressed.
//If the 'Esc' key is pressed, break the while loop.
//If the any other key is pressed, continue the loop
//If any key is not pressed withing 10 ms, continue the loop
if (waitKey(10) == 27)
{
cout << "Esc key is pressed by user. Stoppig the video" << endl;
break;
}
}

return 0;
}
```
Copy and paste the above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/A Herd of Deer Running.mp4" in the code with a valid location to a video in your computer. Then you should see a  set of videos with their brightness increased and decreased along with the original video.

Next Tutorial : Change Contrast of Images and Videos

1. I used the same logic in android and it worked except decreasing the brightness is not perfect and it feels like its changing other value or something.

1. When increasing/decreasing brightness, some pixel values may get clipped to keep them within the minimum and maximum limits. Then the color information of your image will be lost.

2. can u tell me about the projects related to this..

3. Looking on the photos, I can see, that it really gives the result. The only bad thing is that most of people will not understand anything here.

4. Thanks for the examples.
Good job!

5. Lol, I never thought there was so much science involved in changing the brightness of an image. Well, this is interesting. I have been looking for a website where I could Buy English Literature Dissertation, so I can hand my dissertation to them. Once I have placed the order, I will research the subject more.

6. WOW! I never expected this, the algorithm just to change the brightness level is insane. I will provide this article to Research Methodology Writing Service, because they are working on my research paper and I want them to add this information in my paper.

7. This is the most impressive article,

8. Thank you for sharing this post and it is very well told that you can easily understand and get it done. Reported calls

9. such a good site Lawyer World

10. This is the most impressive article Jazz net offer

11. Need to Take Your Class Online Now? Take Your Class Online offers best online class help! Just say take my online class for me. We offer do my online class help!

12. Searching for Take My Classes Online or do my online class? Get the best online class help from Online Classes Hub. We take your classes and courses.

13. Need Take My Online Class For Me ​services? Reach out to Online Class Help Now. We have the best online class takers to help your do my online class for me needs.

14. Hello there I am so excited I found your weblog, I really found
you by accident, while I was browsing on Yahoo for something else, Anyways I am here now and would just like to say many thanks for a marvelous post and a
all round enjoyable blog (I also love the theme/design), I don’t have
time to read it all at the moment but I have book-marked it and
deal more, Please do keep up the awesome job. Kursus SEO Terbaik

15. This is also a very good post which I really enjoyed reading.Post Guerilla

16. I know such posts because I have been marketing for ten years, and I am fully aware of our coding to make a new business website. Without utilising some HTML comes, we can not prepare new sites. coursework writing services

17. This article gives the light in which we can notice the truth. Sexy Call Ladies Service in Mumbai This is exceptionally great one and gives top to bottom data. Much obliged for this decent article.

18. koop rijbewijs online geregistreerd in de gemeente binnen 5 tot 7 werkdagen tegen een goedkope prijs zonder examens
Rijbewijs Kopen België

Rijbewijs Kopen

Rijbewijs b Kopen

Rijbewijs Online Kopen