opencv-adjust image brightness and contrast

Image transformation

  • Pixel transformation-point operation
  • Domain operations-area operations
    Domain operations are mostly used for convolution, feature extraction, image gradient calculation, deeper matching recognition, etc., corner detection, blur, smoothing, etc.

Adjust image brightness and contrast

Adjust the image brightness and contrast, which belongs to the point operation
https://blog.csdn.net/qq_28258885/article/details/112468892
point operation refer to this article

principle

  • The larger the value of RGB, the closer the image is to white. In terms of visual effects, the greater the brightness,
  • The smaller the RGB value, the closer the image is to black. In terms of visual effects, the brightness is smaller,
    so we set the brightness and contrast of the image by adding two coefficients:

Coefficient 1: gain (gain), used to control the contrast of the image, expressed by alpha (α), α>0;

Coefficient 2: Bias, used to control the brightness of the image, expressed in beta (β).

The formula is as follows
Insert picture description here

Related API

  • Mat new_image = Mat::zeros( image.size(), image.type() );
    Create a blank image with the same size and type as the original image, and initialize the pixel value to 0

  • saturate_cast(value) is
    used for pixel range processing to
    ensure that the value range is between 0 and 255

  • Mat.at(y,x)[index]=value
    assign a value to each pixel and each channel

Code demo

#include <opencv2/opencv.hpp>
#include <iostream>

	using namespace cv;
	int main(int argc, char** argv) {
    
    
		Mat src, dst;
		src = imread("C:/Users/86176/Desktop/pics/lena(1).tiff");
		if (!src.data) {
    
    
			printf("could not load image...\n");
			return -1;
		}

		imshow("【before】", src);

		// contrast and brigthtness changes 
		int height = src.rows;
		int width = src.cols;
		dst = Mat::zeros(src.size(), src.type());
		float alpha = 0.4; //   设置对比度
		float beta = 100;   //  设置亮度

		Mat m1;
		src.convertTo(m1, CV_32F);
		//opencv默认8UC,这里将其转换位32F
		for (int row = 0; row < height; row++) {
    
    
			for (int col = 0; col < width; col++) {
    
    
				if (src.channels() == 3) {
    
    
					//获取三通道
					float b = m1.at<Vec3f>(row, col)[0];// blue
					float g = m1.at<Vec3f>(row, col)[1]; // green
					float r = m1.at<Vec3f>(row, col)[2]; // red

														 // output
					dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);
					dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta);
					dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta);
				}
				else if (src.channels() == 1) {
    
    
					float v = src.at<uchar>(row, col);
					dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
				}
			}
		}

		imshow("【after】", dst);

		waitKey(0);
		return 0;
	}

effect

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_28258885/article/details/112554897