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


    积分图介绍

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

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


    二、代码实现

    #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, 3);
        unsigned char *pucImage = (unsigned char*)malloc(pstImage->width * pstImage->height * sizeof(unsigned char));
        unsigned int *puiIntImage = (unsigned int*)malloc((pstImage->width + 1) * (pstImage->height + 1) * sizeof(unsigned int));
    
    
        cvCvtColor(pstImage, pstYUVImage, CV_BGR2YUV);
    
        for(int i = 0; i < pstImage->width * pstImage->height; i++)
        {
            //提取Y分量
            pucImage[i] = pstYUVImage->imageData[i*3];
        }
        
        /* 计算积分图 */
        calcIntImage(pucImage, puiIntImage, pstImage->width, pstImage->height);
    
    
        /* 测试代码,测试是否计算正确 */
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < 5; j++)
            {
                printf("%d ", (int)(pucImage[i*pstImage->width + j]));
            }
            printf("
    ");
        }
        printf("
    ");
        for(int i = 0; i < 6; i++)
        {
            for(int j = 0; j < 6; j++)
            {
                printf("%4d ", (int)(puiIntImage[i*(pstImage->width + 1) + j]));
            }
            printf("
    ");
        }
        /* 测试代码结束 */
    
        cvShowImage("Win", pstImage);
        cvWaitKey(0);
    
        cvReleaseImage(&pstImage);
        cvReleaseImage(&pstYUVImage);
        if(NULL != pucImage)
        {
            free(pucImage);
            pucImage = NULL;
        }
        if(NULL != puiIntImage)
        {
            free(puiIntImage);
            puiIntImage = NULL;
        }
        return 0;
    }
    
    int calcIntImage(unsigned char *pucSrcImage, unsigned int *puiDstImage, int width, int height)
    {
        int i= 0, j = 0, sum = 0;
        int iIntImageWidth = width + 1;
        int iIntImageHeight = height + 1;
        unsigned char *pucSrcImageTmp;
        unsigned int *puiDstImageTmp;
        
        pucSrcImageTmp = pucSrcImage;
    
        /* 存储每列的和 */
        unsigned int *puiImageRow = (unsigned int *)malloc(width * sizeof(unsigned int));
        memset(puiImageRow, 0, width * sizeof(unsigned int));
        puiDstImageTmp = puiDstImage;
    
        /* 积分图第一行清0 */
        memset(puiDstImageTmp, 0, iIntImageWidth);
        puiDstImageTmp += iIntImageWidth;
    
        /* 积分图第一列清0 */
        for(i = 0; i < height; i++)
        {
           *puiDstImageTmp = 0;
           puiDstImageTmp += iIntImageWidth;
        }
    
        puiDstImageTmp = puiDstImage + iIntImageWidth;
        /* 开始计算积分图 */
        for(i = 0; i < height; i++)
        {
            for(j = 0; j < width; j++)
            {
                puiImageRow[j] += pucSrcImageTmp[j];
                puiDstImageTmp[j+1] = puiDstImageTmp[j] + puiImageRow[j];
            }
            puiDstImageTmp += iIntImageWidth;
            pucSrcImageTmp += width;
        }
    
        if(NULL != puiImageRow)
        {
            free(puiImageRow);
            puiImageRow = NULL;
        }
        return 0;
    }

    三、计算结果

    如下图,结果OK~


  • 相关阅读:
    FAN_int2ExcelColChar functions
    How to enables AX email functionality without Outlook
    Global::validateEmail
    Global::time2StrHHMM_DNT
    Global::pickSpecificTable_DNT
    Global::pickSpecificClass_DNT
    Global::pickClassMethod_DNT
    Optimize date2str function
    How to achieve dialog with lookup control
    Optimize str2date function
  • 原文地址:https://www.cnblogs.com/chenpi/p/5128231.html
Copyright © 2020-2023  润新知