Two functions that change image contrast and brightness

Two functions to change image contrast and brightness:
srcImg.cv::convertTo(dstImg,-1,alpha,beta) // Histogram translation: f(i,j)=a*f(i,j) + b;

gamaT() // First construct a lookup table, the value of i is 0-255. Each pixel takes the corresponding r correction value LUT[i]; traverses the image and transforms the value of each pixel according to the equation, that is, query the LUT value corresponding to the pixel value, and then replace it.

The gamaT function is as follows:
Insert picture description here
Reference URL:

https://docs.opencv.org/master/d3/dc1/tutorial_basic_linear_transform.html

Show results

The first original picture, the second srcImg.cv::convertTo(dstImg,-1,1.1,30), the third gama correction, r=0.8;

Source code: GamaT.cpp

#include <iostream>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <cmath>
#include "GamaT.h"

using namespace cv;
using namespace std;

Mat GamaT(Mat &srcImg, float kFactor)
{
    
    
	unsigned char LUT[256];
	for (int i = 0; i < 256; i++)
	{
    
    
		// 构造一个查询表,i的取值0-255。每个像素取对应的r校正值。
		float f = i / 255.0f;
		f = (float)pow(f, kFactor);
		LUT[i] = saturate_cast<uchar>(f*255.0f);
	}
	Mat rImg1;
	Mat rImg2 = srcImg.clone();

	// 方法一:直方图平移:f(i,j)=a*f(i,j) + b;
	//srcImg.convertTo(rImg1, -1, 1.1, 30);
	//imshow("converTo", rImg1);

	// gamaT校正,调节kFactor,在调节明度和对比度的同时最大限度的保留图像细节。
	if (srcImg.channels() == 1)
	{
    
    
		MatIterator_<uchar> iterator = rImg2.begin<uchar>();
		MatIterator_<uchar> iteratorend = rImg1.end<uchar>();
		for (; iterator != iterator; iterator++)
		{
    
    
			//像素范围是有限的0-255,像素个数时rows X cols.
			*iterator = LUT[*iterator];	//当遇到每一个像素时,直接查表,速度更快。
		}
	}
	else
	{
    
    
		MatIterator_<Vec3b> iterator = rImg2.begin<Vec3b>();
		MatIterator_<Vec3b> iteratorend = rImg1.end<Vec3b>();
		for (; iterator != iteratorend; iterator++)
		{
    
    
			(*iterator)[0] = LUT[((*iterator))[0]];	//blue
			(*iterator)[1] = LUT[((*iterator))[1]];	//green
			(*iterator)[2] = LUT[((*iterator))[2]];	//red
		}
	}
	imshow("r", rImg2);
	waitKey();
	return rImg2;
}

Finally, thanks to the blogger Change Coin:

https://blog.csdn.net/linqianbi/article/details/78617615

Guess you like

Origin blog.csdn.net/beauthy/article/details/105120209