Changing the Brightness and Contrast level of any image is the most basic thing everyone does with an image. It is meant to change the value of each and every pixel of an image it can be done by either multiplying or dividing the pixels value of an image. In this article, we will see how we can implement our theory in a beautiful code using OpenCV Python,
Before starting let’s try to understand some basic concepts like, what is brightness? What is a contrast? What are pixels? And what is OpenCV?
- Brightness: When the brightness is adjusted, the entire range of tones within the image is raised or lowered accordingly.
- Contrast: When the contrast adjustment is raised, the middle tones are eliminated. The image will have a higher percentage of darks or blacks and whites or highlights with minimal mid-tone.
- Pixels: Pixels are typically used to refer to the display resolution of a computer monitor or screen. The greater the pixels, the greater the detail in the image.
- OpenCV: OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation
Agenda: To learn how to adjust the brightness and contrast level of an image using OpenCV.
pip install openCV
- Import required module.
- Define the main function, Define required data in it.
- Create a function brightness_contrast, to create a track bar to adjust brightness and contrast.
- Create another function to change the brightness and contrast.
- Display the original and edited image.
- Terminate the program with ‘ESC’ or simply close the window.
Let’s implement this step-wise:
Step 1: Here we will load an image and create a trackbar.
Syntax: imread(filename): filename(Name of the image file).
namedWindow(winname): winname(Name of the window).
Step 2: By calling the controller function, it will return the edited image, After that imshow() function will display the affected image.
Syntax: getTrackbarPos(trackbarname, winname): trackbarname(Name of the trackbar), winname( Name of the window)
Step 3: The controller function will control the Brightness and Contrast of an image according to the trackbar position and return the edited image.
Syntax: addWeighted(src1, alpha, src2, beta, gamma)
src1: irst input array.
alpha: (weight of the first array elements.
src2: second input array of the same size and channel number as src1.
beta: weight of the second array elements.
gamma: scalar added to each sum.
putText(img, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin)
text: Text string to be drawn.
org: Bottom-left corner of the text string in the image.
fontFace: Font type, see #HersheyFonts.
fontScale: Font scale factor that is multiplied by the font-specific base size.
color: Text color.
thickness: Thickness of the lines used to draw a text.
lineType: Line type. See #LineTypes.
bottomLeftOrigin: When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
Below is the full Implementation:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.