在参考了博主遥感服务提供的思路后,进行该思路的实现,注意:目前只能实现tif或png格式拉伸,主要代码如下:
Cstretch函数:
cv::Mat CStretch(const cv::Mat img, const float ratio)
{
//影像分RGB计算灰度直方图
cv::Mat mimg = img.clone();
cv::Mat bgr[3];
split(mimg, bgr);
std::vector hb = CHistogram(bgr[0]);
std::vector hg = CHistogram(bgr[1]);
std::vector hr = CHistogram(bgr[2]);
//统计直方图累计频率ratio值对应的灰度
uint minb = MinRatio(hb, ratio);
uint ming = MinRatio(hg, ratio);
uint minr = MinRatio(hr, ratio);
uint maxb = MaxRatio(hb, ratio);
uint maxg = MaxRatio(hg, ratio);
uint maxr = MaxRatio(hr, ratio);
//开始拉伸工作
cv::Mat b, g, r;
b = CStretchDeal(bgr[0], minb, maxb);
g = CStretchDeal(bgr[1], ming, maxg);
r = CStretchDeal(bgr[2], minr, maxr);
//合并拉伸结果
cv::Mat newbgr;
newbgr.create(img.rows, img.cols, CV_32FC3);
cv::Mat nbgr[3] = { b, g, r };
merge(nbgr, 3, newbgr);
return newbgr;
}
CHistogram函数:
std::vector CHistogram(const cv::Mat img)
{
cv::Mat mimg = img.clone();
int rows = mimg.rows;
int cols = mimg.cols;
//统计每个灰度出现的次数
std::vector hisnum(256);
for (int i(0); i < rows; ++i)
{
for (int j(0); j < cols; ++j)
{
uint gv = mimg.at(i, j);
++hisnum[gv];
}
}
//开始计算灰度频率
long int pnum = rows * cols;
std::vector hisp;
for (int i(0); i < hisnum.size(); ++i)
{
float p = (float)hisnum[i] / pnum;
hisp.push_back(cv::Point2f(i, p));
}
return hisp;
}
MinRatio函数:
uint MinRatio(const std::vector data, const float ratio)
{
float minp(0);
uint outnum(0);
for (int i(0); i < data.size(); ++i)
{
minp += data[i].y;
if (minp >= ratio)
{
outnum = i;
break;
}
}
return outnum;
}
MaxRatio函数:
uint MaxRatio(const std::vector data, const float ratio)
{
float maxp(0);
uint outnum(0);
for (int i(data.size() - 1); i >= 0; --i)
{
maxp += data[i].y;
if (maxp >= ratio)
{
outnum = i;
break;
}
}
return outnum;
}
CStretchDeal函数:
cv::Mat CStretchDeal(const cv::Mat img, const uint minnum, const uint maxnum)
{
cv::Mat mimg = img.clone();
int rows = mimg.rows;
int cols = mimg.cols;
cv::Mat nimg = cv::Mat::zeros(rows, cols, CV_8U);
//归一化参数
float dertnum = maxnum - minnum;
//开始处理
for (int i(0); i < rows; ++i)
{
for (int j(0); j < cols; ++j)
{
uint a = mimg.at(i, j);
//小于ratio对应像素值取0
if (a <= minnum)
nimg.at(i, j) = 0;
//大于ratio值对应像素值取255
else if (a >= maxnum)
nimg.at(i, j) = 255;
//中间值拉伸到0-255
else
nimg.at(i, j) = 255*(mimg.at(i, j) - minnum) / dertnum;
}
}
return nimg;
}
调用测试:
#include
#include
int main()
{
//影像路径
string imgfile = "F:/groundationproject/PicturesCut2000+2000SameLeftTopAll/f1.tif";
cv::Mat oimg = cv::imread(imgfile);
//show原图
cv::namedWindow("before stretch", 0);
cv::imshow("before stretch", oimg);
//拉伸
cv::Mat nimg = CStretch(oimg, 0.01);
//show新图
cv::namedWindow("after stretch", 0);
cv::imshow("after stretch", nimg);
cv::waitKey();
}
拉伸效果展示:
参考文献:
影像linear 2%实现