OpenCV直方图计算函数calcHist()的最后一个参数accumulate的作用

搜索全网,有很多介绍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()函数,记录下它的运算结果:

//出处:昊虹AI笔记网(hhai.cc)
//用心记录计算机视觉和AI技术

//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。
代码如下:

//出处:昊虹AI笔记网(hhai.cc)
//用心记录计算机视觉和AI技术


//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的作用了吧。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值