# How can I change brightness&contrast of image

**URL:** <https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965>\
**Category:** C++\
**Tags:** highgui, imgproc\
**Created:** [February 13, 2023, 4:57am UTC](https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965 "2023-02-13T04:57:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schwalbe](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@schwalbe](https://forum.opencv.org/u/schwalbe)\
**Post date:** [February 13, 2023, 4:57am UTC](https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965/1 "2023-02-13T04:57:54Z")

</div>

Hi everyone. I have an 8UC1 image and I want to change its brightness and contrast. I have gui for it and there is 2 sliders in gui:

```auto
cv::Mat img = ... ;

sliderBrightness(int value){
cv::Mat imgB = img + cv::Scalar(value * 255/100.0); //I am currently changing brightness like this but not sure if its correct way.
...
}
sliderContrast(int value){}

```

Here value is in 0-99 range according to slider’s position. Which functions should i use for these operations?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 13, 2023, 8:24am UTC](https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965/2 "2023-02-13T08:24:22Z")

</div>

> [@schwalbe](#):
>
> I have gui for it and there is 2 sliders in gui:

you’re putting the cart before the horse …

and no, we wont write your program,  
instead, please study tutorials, before asking :

[https://docs.opencv.org/4.x/d7/da8/tutorial\_table\_of\_content\_imgproc.html](https://docs.opencv.org/4.x/d7/da8/tutorial_table_of_content_imgproc.html)

---

<div class="post-metadata">

**Author:** ![schwalbe](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@schwalbe](https://forum.opencv.org/u/schwalbe)\
**Post date:** [February 13, 2023, 9:29am UTC](https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965/3 "2023-02-13T09:29:01Z")

</div>

I didn’t ask to you anywhere to write my program. I just asked for what should i do for changing these two things. And you are pointing wrong link. Right doc : [OpenCV: Changing the contrast and brightness of an image!](https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html)  
I did this and still have a question but this website looks kinda dead and you seem like you don’t know more than me so i am passing my questions.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 13, 2023, 1:45pm UTC](https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965/4 "2023-02-13T13:45:33Z")

</div>

if you choose to go with OpenCV’s “highgui”, you will need:

- a “display” function that gets the positions of both trackbars, prepares what you want to show, and calls imshow() to present this
- namedWindow()
- two trackbars in that window
- installation of callbacks for both trackbars, which call the display() function
- an initial call to display() before the main loop to present _something_
- a main loop running waitKey()

please research what “brightness” and “contrast” mean in terms of implementation (numerically). less brutal operations are gain and gamma adjustment. RGB space isn’t linear.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 13, 2023, 1:54pm UTC](https://forum.opencv.org/t/how-can-i-change-brightness-contrast-of-image/11965/5 "2023-02-13T13:54:29Z")

</div>

> [@schwalbe](#):
>
> `cv::Mat imgB = img + cv::Scalar(value * 255/100.0);`

careful ! this will only initialize the 1st (b) component of the Scalar (the g,r ones will be zero !) for 3 channel images, you need all 3, like:

```
ìmg + Scalar(x,y,z);

```
