下图是典型的对比度拉伸变换。点(r1,s1)和(r2,s2)的位置控制变换函数的形状。
如果r1=r2,s1=s2,则变换为一线性函数;
若r1=r2,s1=0且s2=L-1,则是阈值处理函数,产生一幅二值图像;
处理一幅8bit低对比度图像,(r1,s1)=(r_min,0),(r2,s2)=(r_max,L-1);其中r_min,r_max是图像中最小和最大灰度级;
因此,变换函数把灰度级由原范围线性的拉伸至整个范围[0,L-1]
代码实现:
/*
* 对比度拉伸
*/
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
cv::Mat contrastStretch1(cv::Mat srcImage)
{
cv::Mat resultImage = srcImage.clone();
int nRows = resultImage.rows;
int nCols = resultImage.cols;
// 图像连续性判断
if(resultImage.isContinuous()){
nCols = nCols * nRows;
nRows = 1;
}
// 计算图像的最大最小值
double pixMin,pixMax;
cv::minMaxLoc(resultImage,&pixMin,&pixMax);
std::cout << "min_a=" << pixMin << " max_b=" << pixMax << std::endl;
// 对比度拉伸映射
for(int j = 0; j < nRows; j +&#