【AdaBoost算法】积分图代码实现

时间:2021-02-07 18:01:07

积分图介绍

定义:图像左上方的像素点值的和;

在Adaboost算法中可用于加速计算Haar或MB-LBP特征值,如下图:

【AdaBoost算法】积分图代码实现

二、代码实现

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc_c.h> using namespace cv; int calcIntImage(unsigned char *pucSrcImage, unsigned int *pucDstImage, int width, int height); int main(int argv, char **argc)
{
IplImage *pstImage = cvLoadImage("D:\\test01.JPG", CV_LOAD_IMAGE_COLOR);
IplImage *pstYUVImage = cvCreateImage(cvSize(pstImage->width, pstImage->height), IPL_DEPTH_8U, );
unsigned char *pucImage = (unsigned char*)malloc(pstImage->width * pstImage->height * sizeof(unsigned char));
unsigned int *puiIntImage = (unsigned int*)malloc((pstImage->width + ) * (pstImage->height + ) * sizeof(unsigned int)); cvCvtColor(pstImage, pstYUVImage, CV_BGR2YUV); for(int i = ; i < pstImage->width * pstImage->height; i++)
{
//提取Y分量
pucImage[i] = pstYUVImage->imageData[i*];
} /* 计算积分图 */
calcIntImage(pucImage, puiIntImage, pstImage->width, pstImage->height); /* 测试代码,测试是否计算正确 */
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
printf("%d ", (int)(pucImage[i*pstImage->width + j]));
}
printf("\n");
}
printf("\n");
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
printf("%4d ", (int)(puiIntImage[i*(pstImage->width + ) + j]));
}
printf("\n");
}
/* 测试代码结束 */ cvShowImage("Win", pstImage);
cvWaitKey(); cvReleaseImage(&pstImage);
cvReleaseImage(&pstYUVImage);
if(NULL != pucImage)
{
free(pucImage);
pucImage = NULL;
}
if(NULL != puiIntImage)
{
free(puiIntImage);
puiIntImage = NULL;
}
return ;
} int calcIntImage(unsigned char *pucSrcImage, unsigned int *puiDstImage, int width, int height)
{
int i= , j = , sum = ;
int iIntImageWidth = width + ;
int iIntImageHeight = height + ;
unsigned char *pucSrcImageTmp;
unsigned int *puiDstImageTmp; pucSrcImageTmp = pucSrcImage; /* 存储每列的和 */
unsigned int *puiImageRow = (unsigned int *)malloc(width * sizeof(unsigned int));
memset(puiImageRow, , width * sizeof(unsigned int));
puiDstImageTmp = puiDstImage; /* 积分图第一行清0 */
memset(puiDstImageTmp, , iIntImageWidth);
puiDstImageTmp += iIntImageWidth; /* 积分图第一列清0 */
for(i = ; i < height; i++)
{
*puiDstImageTmp = ;
puiDstImageTmp += iIntImageWidth;
} puiDstImageTmp = puiDstImage + iIntImageWidth;
/* 开始计算积分图 */
for(i = ; i < height; i++)
{
for(j = ; j < width; j++)
{
puiImageRow[j] += pucSrcImageTmp[j];
puiDstImageTmp[j+] = puiDstImageTmp[j] + puiImageRow[j];
}
puiDstImageTmp += iIntImageWidth;
pucSrcImageTmp += width;
} if(NULL != puiImageRow)
{
free(puiImageRow);
puiImageRow = NULL;
}
return ;
}

三、计算结果

如下图,结果OK~

【AdaBoost算法】积分图代码实现