void integral( InputArray src, OutputArray sum, int sdepth=-1 );
第三个参数sdepth,定义积分图的深度(depth),32位整型或者32位、64位浮点型。注意图像的深度跟图像的通道数是无关的,相关概念可以参看这里:Opencv Mat矩阵中data、size、depth、elemSize、step等属性的理解 ;
所以在使用sum之前,声明即可,可以不事先定义Mat矩阵的大小和数据类型。
#include "highgui/highgui.hpp"
#include "imgproc/imgproc.hpp"
using namespace cv;
int main(int argc,char *argv[])
{
Mat image=imread(argv[1]);
//cvtColor(image,image,CV_RGB2GRAY); //原图像是三通道,积分图也是三通道
Mat imageIntegral;
integral(image,imageIntegral,CV_32F); //计算积分图
normalize(imageIntegral,imageIntegral,0,255,CV_MINMAX); //归一化,方便显示
Mat imageIntegralNorm;
convertScaleAbs(imageIntegral,imageIntegralNorm); //精度转换为8位int整型
imshow("Source Image",image);
imshow("Integral Image",imageIntegralNorm);
waitKey();
}