In OpenCV, changing the contrast of an image is a very basic task to perform. By changing the image contrast, it is meant to change the value of each and every image pixel. This change can be done by either multiplying or dividing (means to multiply each pixel with value < 1) the pixel values of the image, by any constant.
To increase the contrast levels of the image, simply multiply a constant positive value to each and every image pixel.
Similarly if one wishes to decrease the contrast level of the image, then multiply a constant positive value less than 1 for each and every image pixel.
Let’s take an assumption by considering the below-mentioned matrix as our image matrix with values of pixel ranging from 0 to 255 and let’s assume that the data type for this image is CV_8UC3.
Increasing contrast :
So. let’s suppose we wish to increase image contrast by a factor of 2. Then we multiply 2 to each image pixel value but it is strictly forbidden to exceed the pixel range i.e. the allowed maximum limit is 255 and our pixel value should not increase 255. Suppose if in any case, this image pixel value exceeds 255, then it should be given the maximum allowed value only instead of the newly increased value.
Let’s check the image matrix given below, we have multiply this value by factor of 2 but the pixel value “255” has not been increased to 288 as we multiply 2 to 144, it should be 288 but this is not happening here because the maximum image pixel value is allowed to be 255 in this image.
Decreasing contrast :
Here, we are decreasing the contrast values by a factor of 0.5 and to do so 0.5 is multiplied to each and every image pixel in the input image but the only thing important is that the value of any pixel should not go below the minimum allowed pixel.
As here in our output image, the contrast is decreased by a factor of 0.5.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- OpenCV | Hands on Image Contrast
- Image Processing in Java | Set 12 ( Contrast Enhancement )
- OpenCV | Understanding Brightness in an Image
- Node | Jimp | contrast
- Python PIL | ImageEnhance.Color() and ImageEnhance.Contrast() method
- Nodejs | GM contrast() Function
- Pgmagick contrast() method - Python
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- OpenCV C++ Program to create a single colored blank image
- OpenCV C++ Program to blur an image
- OpenCV Python Program to blur an image
- OpenCV Python Program to analyze an image using Histogram
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)
- Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )
- Reading an image in OpenCV using Python
- Python | Detect corner of an image using OpenCV
- Image Pyramid using OpenCV | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.