OPENCV中直方图的归一化
Normalization of histogram in OPENCV
我在 OpenCV 项目期间遇到有关规范化过程的问题。我应该使用的函数叫做cvNormalizeHist,但是我不知道如何直接在代码中使用它。
C: void cvNormalizeHist(CvHistogram* hist, double factor)
Parameters:
hist – Pointer to the histogram.
factor – Normalization factor.
The function normalizes the histogram bins by scaling them so that the sum of the bins becomes equal to factor.
我想做的是:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst;
/// Load image
src = imread(argv[1], 1);
if (!src.data)
{
return -1;
}
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(src, bgr_planes);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
cvNormalizeHist(b_hist, 5); // (1)
CvHistogram histogram(); // (2)
...
waitKey(0);
return 0;
}
我正在尝试使用这样的函数,但是错误提示我应该使用 CvHistogram 而不是 Mat 对象。而且我不知道如何在这些对象之间进行转换。
任何人都可以给我如何做或任何建议?
更多可能有用的信息是:
Windows 10,Visual Studio 2013,OpenCV 3.0
此致
您应该避免使用过时的 C 函数。
您可以使用 normalize,其中 alpha
等于您的因数,NORM_L1
。
例如:
double factor = 25;
normalize(b_hist, b_hist, factor, 0, NORM_L1);
cout << sum(b_hist)[0];
您会看到 bin 的总和等于因子。
我在 OpenCV 项目期间遇到有关规范化过程的问题。我应该使用的函数叫做cvNormalizeHist,但是我不知道如何直接在代码中使用它。
C: void cvNormalizeHist(CvHistogram* hist, double factor)
Parameters:
hist – Pointer to the histogram.
factor – Normalization factor.
The function normalizes the histogram bins by scaling them so that the sum of the bins becomes equal to factor.
我想做的是:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst;
/// Load image
src = imread(argv[1], 1);
if (!src.data)
{
return -1;
}
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(src, bgr_planes);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
cvNormalizeHist(b_hist, 5); // (1)
CvHistogram histogram(); // (2)
...
waitKey(0);
return 0;
}
我正在尝试使用这样的函数,但是错误提示我应该使用 CvHistogram 而不是 Mat 对象。而且我不知道如何在这些对象之间进行转换。
任何人都可以给我如何做或任何建议?
更多可能有用的信息是: Windows 10,Visual Studio 2013,OpenCV 3.0
此致
您应该避免使用过时的 C 函数。
您可以使用 normalize,其中 alpha
等于您的因数,NORM_L1
。
例如:
double factor = 25;
normalize(b_hist, b_hist, factor, 0, NORM_L1);
cout << sum(b_hist)[0];
您会看到 bin 的总和等于因子。