1.直方图匹配
直方图匹配又叫直方图规定化(Histogram Normalization/Matching)是指对一副图像进行变换,使其直方图与另一幅图像的直方图或特定函数形式的直方图进行匹配。应用场景如不同光照条件下的两幅图像,我们可以在比较两幅图像前先进行匹配变化。
参考[【OpenCV】直方图应用:直方图均衡化,直方图匹配,对比直方图]中将一幅图像的直方图与特定函数形式的直方图匹配方法,这里将一副图像的直方图与另一幅图像的直方图进行匹配。
//输入想要进行匹配的图像、目标直方图、想要进行匹配的图像的直方图
//返回匹配到目标直方图的图像
Mat HistMatch(const Mat& adjPlane, const MatND& standHist, const MatND& adjHist)
{
double val_1 = 0.0;
double val_2 = 0.0;
uchar T[256] = { 0 };
double S[256] = { 0 };
double G[256] = { 0 };
for (int index = 0; index<256; ++index)
{
val_1 += adjHist.at<float>(index);
val_2 += standHist.at<float>(index);
G[index] = val_2;
S[index] = val_1;
}
double min_val = 0.0;
int PG = 0;
for (int i = 0; i<256; ++i)
{
min_val =1.0;
for (int j = 0; j<256; ++j)
{
if ((G[j] - S[i]) < min_val && (G[j] - S[i]) >= 0)
{
min_val = (G[j] - S[i]);
PG = j;
}
}
T[i] = (uchar)PG;
}
Mat match(adjPlane.size(),adjPlane.type());
for (int rowCount = 0; rowCount < adjPlane.rows; rowCount++)
{
uchar* matchPoint = match.ptr<uchar>(rowCount);
const uchar* rowPoint = adjPlane.ptr<uchar>(rowCount);
for (int colCount = 0; colCount < adjPlane.cols; colCount++)
matchPoint[colCount] = T[rowPoint[colCount]];
}
return match;
}
2.实验结果
image:目标图像;adj:进行匹配的图像;match:匹配后的图像
从结果看,多幅影像间色彩差距不大时,直方图匹配可用于影像间的匀光。