图像直方图归一化C++源码
时间: 2023-11-30 07:40:51 浏览: 63
C++图像直方图
5星 · 资源好评率100%
根据提供的引用内容,以下是图像直方图归一化的C++源码:
```c++
void histNormalize(Mat& src, Mat& dst)
{
// 计算直方图
int histSize = 256;
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true, accumulate = false;
Mat hist;
calcHist(&src, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);
// 归一化直方图
int rows = hist.rows, cols = hist.cols;
float sum = 0;
for (int i = 0; i < rows; i++)
{
sum += hist.at<float>(i, 0);
}
Mat hist_norm = Mat::zeros(histSize, 1, CV_32FC1);
for (int i = 0; i < rows; i++)
{
hist_norm.at<float>(i, 0) = hist.at<float>(i, 0) / sum;
}
// 计算累积分布函数
Mat cdf = Mat::zeros(histSize, 1, CV_32FC1);
float cdf_sum = 0;
for (int i = 0; i < rows; i++)
{
cdf_sum += hist_norm.at<float>(i, 0);
cdf.at<float>(i, 0) = cdf_sum;
}
// 映射像素值
dst = src.clone();
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
int pixel = src.at<uchar>(i, j);
dst.at<uchar>(i, j) = cdf.at<float>(pixel, 0) * 255;
}
}
}
```
阅读全文