
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change Contrast and Brightness of an Image Using OpenCV in Python
In OpenCV, to change the contrast and brightness of an image we could use cv2.convertScaleAbs(). The syntax we use for this method is as follows ?
cv2.convertScaleAbs(image, alpha, beta)
Where
image is the original input image.
alpha is the contrast value. To lower the contrast, use 0 < alpha < 1. And for higher contrast use alpha > 1.
beta is the brightness value. A good range for brightness value is [-127, 127]
We could also apply the cv2.addWeighted() function to change the contrast and brightness of an image. We have discussed it in example 2.
Steps
To change the contrast and brightness of an image, you could follow the steps given below ?
Import the required library OpenCV. Make sure you have already installed it.
Read the input image using cv2.imread() method. Specify the full path of the image.
Define alpha (it controls contrast) and beta (it controls brightness) and call convertScaleAbs() function to change the contrast and brightness of the image. This function returns the image with adjusted contrast and brightness. Alternatively, we can also use the cv2.addWeighted() method to change contrast and brightness
Display the contrast and brightness adjusted image.
Let's see the examples to change the contrast and brightness of an image.
Input Image
We will use the following image as the input file in the examples below.
Example
In this Python program, we change the contrast and brightness of the input image using cv2.convertScaleAbs() method.
# import the required library import cv2 # read the input image image = cv2.imread('food1.jpg') # define the alpha and beta alpha = 1.5 # Contrast control beta = 10 # Brightness control # call convertScaleAbs function adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta) # display the output image cv2.imshow('adjusted', adjusted) cv2.waitKey() cv2.destroyAllWindows()
Output
When you execute the above code it will produce the following output window -
Example
In this Python program, we change the contrast and brightness of the input image using cv2.addWeighted() method.
# import required library import cv2 # read the input image img = cv2.imread('food1.jpg') # define the contrast and brightness value contrast = 5. # Contrast control ( 0 to 127) brightness = 2. # Brightness control (0-100) # call addWeighted function. use beta = 0 to effectively only operate on one image out = cv2.addWeighted( img, contrast, img, 0, brightness) # display the image with changed contrast and brightness cv2.imshow('adjusted', out) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you execute the above code, it will produce the following output window.