How to Change Contrast in OpenCV using C++?

OpenCVC++Server Side ProgrammingProgramming

Changing brightness and contrast are frequent editing effect in image processing.Here, we will learn how to change the contrast of images. Contrast controls the sharpness of the image. Higher the contrast the sharper the image, lower the contrast the smother the image.

Changing the contrast means increasing the weight of the pixels. More the contrast, sharper the image is. To change the contrast, multiply the pixel values with some constant. For example, if multiply all the pixel values of an image by 2, then the pixel's value will be doubled, and the image will look sharper.

The following program demonstrates how to change the contrast of an image in OpenCV.

Example

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
   Mat original;//Declaring a matrix to load the original image//
   Mat contrast;//Declaring a matrix to load the image after changing the brightness//
   namedWindow("Original");//Declaring window to show the original image//
   namedWindow("Contrast");//Declaring window for edited image//
   original = imread("mountain.jpg");//loading the image
   original.convertTo(contrast, -1, 2, 0);//changing contrast//
   imshow("Original", original);//showing original image//
   imshow("Contrast", contrast);//showing edited image//
   waitKey(0);//wait for keystroke//
   return(0);
}

Output

raja
Published on 10-Mar-2021 08:18:34
Advertisements