搜索全网,有很多介绍OpenCV直方图计算函数calcHist()的文章,但对于其最后一个参数accumulate的作用,要么一笔带过,要么直接搬官方文档的原文,看了半天也还是不知道参数accumulate的作用。
官方文档对 accumulate 这个参数的介绍下:
accumulate— Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.
翻译如下:
accumulate—累加标志。如果这个标志值为true,那么上一次调用函数calcHist()所计算出的保存在hist中的数据是不会被清空的,而且会与这次调用的值累加;如果这个标志值为false,那么上一次调用函数calcHist()所计算出的保存在hist中的数据是会被清空的。
具体的效果,还是只有动手写代码试一下。
我们先调用一次calcHist()函数,记录下它的运算结果:
[C++] 纯文本查看 复制代码 //出处:昊虹AI笔记网(hhai.cc)
//用心记录计算机视觉和AI技术
//博主微信/QQ 2487872782
//QQ群 271891601
//欢迎技术交流与咨询
//OpenCV版本 OpenCV3.0
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main( )
{
// 读取源图像并转化为灰度图像
cv::Mat srcImage = cv::imread("F:/material/images/P0027-coin-01.bmp");
// 判断文件是否读入正确
if( !srcImage.data )
return 1;
cv::Mat ImageGray;
cv::cvtColor(srcImage,ImageGray,CV_BGR2GRAY);
// 定义直方图参数
const int channels[1]={0};
const int histSize[1]={256};
float pranges[2]={0,255};
const float* ranges[1]={pranges};
cv::Mat hist;
cv::calcHist(&ImageGray,1,channels,cv::Mat(),hist,1,histSize,ranges,true,false);
std::cout<<hist<<std::endl;
return 0;
}
运行结果如下:
接下来我们对calcHist()函数调用两次,并且第二次调用时把最后一个参数值accumulate的值置为true。
代码如下:
[C++] 纯文本查看 复制代码 //出处:昊虹AI笔记网(hhai.cc)
//用心记录计算机视觉和AI技术
//博主微信/QQ 2487872782
//QQ群 271891601
//欢迎技术交流与咨询
//OpenCV版本 OpenCV3.0
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main( )
{
// 读取源图像并转化为灰度图像
cv::Mat srcImage = cv::imread("F:/material/images/P0027-coin-01.bmp");
// 判断文件是否读入正确
if( !srcImage.data )
return 1;
cv::Mat ImageGray;
cv::cvtColor(srcImage,ImageGray,CV_BGR2GRAY);
// 定义直方图参数
const int channels[1]={0};
const int histSize[1]={256};
float pranges[2]={0,255};
const float* ranges[1]={pranges};
cv::Mat hist;
cv::calcHist(&ImageGray,1,channels,cv::Mat(),hist,1,histSize,ranges,true,false);//第一次调用函数calcHist,并且最后一个参数值accumulate的值置为false
cv::calcHist(&ImageGray,1,channels,cv::Mat(),hist,1,histSize,ranges,true,true);///第二次调用函数calcHist,并且最后一个参数值accumulate的值置为true
std::cout<<hist<<std::endl;
return 0;
}
运行结果如下:
我们把两次的运行结果进行对比,如下:
从上面的运行结果对比中,我们可以看出,第二次的运行结果是对第一次运算结果的累加。看到这里,大家就应该可以非常明确calcHist()的最后一个参数accumulate的作用了吧。
|