#include "cv.h"
#include "highgui.h"
using namespace cv;
#define PI 3.1415926
#define RADIAN(angle) ((angle)*PI/180.0)
//对比度扩展
void ContrastStretch(Mat& mat,float stretchRatio,int firstPoint,int secondPoint)
{
int M=mat.rows;
int N=mat.cols;
//计算第一段和第三段的斜率
float a=(float)((255.0-stretchRatio*(secondPoint-firstPoint))/(255.0-(secondPoint-firstPoint)));
float g1=a*firstPoint;
float g2=stretchRatio*(secondPoint-firstPoint)+g1;
uchar table[256]={0};
for(int i=0;i<256;i++)
{
float g;
if(i<firstPoint)
g=(float)(a*i);
else if(i<secondPoint)
g=g1+stretchRatio*(i-firstPoint);
else
g=g2+a*(i-secondPoint);
table[i]=(uchar)g;
}
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
mat.at<uchar>(i,j)=table[mat.at<uchar>(i,j)];
}
int main()
{
//读取图像
Mat image;
image=imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
int M=image.rows;
int N=image.cols;
ContrastStretch(image,0.5,100,200);
cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
imshow("test",image);
waitKey(0);
return 0;
}
图像处理之对比度拉伸
最新推荐文章于 2022-11-12 00:00:00 发布