www.pudn.com > 3AdaBoost.rar > HarrFeature.cpp, change:2005-02-23,size:18582b


///////////////////////////////////////////////////////////////////// 
// 
// LBPFeature.cpp: implementation of the LBPFeature class. 
// 
////////////////////////////////////////////////////////////////////// 
#include "stdafx.h" 
#include "HarrFeature.h" 
#include "Image.h" 
#include "direct.h" 
#include "math.h" 
#include "Define.h" 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
HarrFeature::HarrFeature() 
{ 
        imageSize.x = SRCIMAGEX; 
        imageSize.y = SRCIMAGEY; 
        featureNum = TOTAL_FEATURE_NUM; 
        FaceImageNum = 0; 
        NonFaceImageNum = 0; 
} 
 
HarrFeature::~HarrFeature() 
{ 
 
} 
 
DWORD HarrFeature::GetFaceImageNum() 
{ 
        return FaceImageNum; 
} 
 
DWORD HarrFeature::GetNonfaceImageNum() 
{ 
        return NonFaceImageNum; 
} 
 
DWORD HarrFeature::GetFeatureNum(CString strFeature, bool isFace) 
{ 
        DWORD   FileSizeHigh;   
        DWORD   FileSizeLow; 
 
        HANDLE fil = CreateFile(strFeature, 
                GENERIC_READ|GENERIC_WRITE, 
                FILE_SHARE_WRITE, 
                NULL, 
                OPEN_EXISTING, 
                FILE_ATTRIBUTE_NORMAL, 
                NULL); 
        FileSizeLow = GetFileSize (fil, &FileSizeHigh); 
        DWORD imageNum = FileSizeLow / (sizeof(float) * featureNum); 
        if (isFace == true) FaceImageNum = imageNum; 
        else NonFaceImageNum = imageNum; 
 
        CloseHandle ((HANDLE)fil); 
        return imageNum; 
} 
 
void HarrFeature::SaveAllFeatures(CString strIntg, CString strFeature, bool isFaceData) 
{ 
        int     i, m, n; 
        DWORD   FileSizeHigh;   
        DWORD   FileSizeLow; 
        DWORD   bytesread; 
        float   *pTempData; 
        float   *pTempFeature; 
        //计算出人脸训练图象的数目 
        HANDLE fil = CreateFile(strIntg, 
                GENERIC_READ|GENERIC_WRITE, 
                FILE_SHARE_WRITE, 
                NULL, 
                OPEN_EXISTING, 
                FILE_ATTRIBUTE_NORMAL, 
                NULL); 
        FileSizeLow = GetFileSize (fil, &FileSizeHigh); 
        DWORD imageNum = FileSizeLow / (imageSize.y * imageSize.x * sizeof(float)); 
        if (isFaceData == true) FaceImageNum = imageNum; 
        else NonFaceImageNum = imageNum; 
 
        //读入训练图象的积分图象数据 
        if (isFaceData) 
                printf("Reading eye integral data......\n"); 
        else  
                printf("Reading noneye integral data......\n"); 
 
        float *pIntgData = new float[imageNum * imageSize.x * imageSize.y]; 
        ReadFile (fil, pIntgData, imageNum * imageSize.y * imageSize.x * sizeof(float), 
                &bytesread, NULL); 
        CloseHandle ((HANDLE)fil); 
 
        //计算出所有人脸训练图像的特征 
        if (isFaceData) 
                printf("Computing eye harr features......\n"); 
        else 
                printf("Computing noneye harr features......\n"); 
 
        pTempData = pIntgData; 
        float *pFeature = new float[imageNum * featureNum]; 
        pTempFeature = pFeature; 
        for (i=0; i<imageNum; i++) 
        { 
                GetFeatures(pTempData, pTempFeature); 
                pTempData += imageSize.x * imageSize.y; 
                pTempFeature += featureNum; 
        } 
        delete []pIntgData; 
        //重新排列特征的顺序 
        FILE *f_Feature = fopen(strFeature, "wb"); 
        if (isFaceData == true) 
                printf("Re-ordering eye features......\n"); 
        else  
                printf("Re-ordering noneye features......\n"); 
 
        float *pFinalFeature = new float[imageNum]; 
        for (m=0; m<featureNum; m++) 
        { 
                printf("feaid = %d\r", m); 
                for (n=0; n<imageNum; n++) 
                { 
                        pFinalFeature[n] = pFeature[n * featureNum + m]; 
                } 
                //将特征写入文件 
                fwrite(pFinalFeature, sizeof(float), imageNum, f_Feature); 
        } 
        fclose(f_Feature); 
 
        delete []pFinalFeature; 
        delete []pFeature; 
} 
 
///////////////////////////////////////////////////////////////////////////////// 
void HarrFeature::GetFeatures(float *pInterData, float *pFeatures) 
{  
        int    FeatureType; 
        POINT  FeatureStPos, FeatureScale; 
        POINT  imageSize; 
        imageSize.x = SRCIMAGEX; 
        imageSize.y = SRCIMAGEY; 
 
        long feaCount = 0; 
        FeatureStPos.y = 1; 
        while (FeatureStPos.y < imageSize.y-1)//行扫描 
        { 
                FeatureStPos.x = 1; 
                while (FeatureStPos.x < imageSize.x-1)//列扫描 
                { 
                        //====== 计算A类特征 ====== 
                        FeatureScale.y = 2; 
                        FeatureType = 0; 
                        while (FeatureStPos.y + FeatureScale.y < imageSize.y)//特征y方向放大 
                        { 
                                FeatureScale.x = 2;  
                                while (FeatureStPos.x + 2 * FeatureScale.x < imageSize.x)//特征x方向放大 
                                { 
                                        pFeatures[feaCount] = GetOneFeatureValue 
                                                (FeatureType,  
                                                FeatureStPos,  
                                                FeatureScale, 
                                                pInterData); 
                                        feaCount++; 
                                        FeatureScale.x += 1; 
                                } 
                                FeatureScale.y += 1; 
                        } 
                        //========= 计算B类特征 ========== 
                        FeatureScale.y = 2; 
                        FeatureType = 1; 
                        while (FeatureStPos.y + 2 * FeatureScale.y < imageSize.y)//特征y方向放大 
                        { 
                                FeatureScale.x = 2;  
                                while (FeatureStPos.x + FeatureScale.x < imageSize.x)//特征x方向放大 
                                { 
                                        pFeatures[feaCount] = GetOneFeatureValue 
                                                (FeatureType,  
                                                FeatureStPos,  
                                                FeatureScale, 
                                                pInterData); 
                                        feaCount++; 
                                        FeatureScale.x += 1; 
                                } 
                                FeatureScale.y += 1; 
                        } 
                        //========= 计算C类特征 ========== 
                        FeatureScale.y = 2; 
                        FeatureType = 2; 
                        while (FeatureStPos.y + FeatureScale.y < imageSize.y) 
                        { 
                                FeatureScale.x = 2;  
                                while (FeatureStPos.x + 3 * FeatureScale.x < imageSize.x) 
                                { 
                                        pFeatures[feaCount] = GetOneFeatureValue 
                                                (FeatureType,  
                                                FeatureStPos,  
                                                FeatureScale, 
                                                pInterData); 
                                        feaCount++; 
                                        FeatureScale.x += 1; 
                                } 
                                FeatureScale.y += 1; 
                        } 
                        //========= 计算D类特征 ========== 
                        FeatureScale.y = 2; 
                        FeatureType = 3; 
                        while (FeatureStPos.y + 2 * FeatureScale.y < imageSize.y) 
                        { 
                                FeatureScale.x = 2; 
                                while (FeatureStPos.x + 2 * FeatureScale.x < imageSize.x) 
                                { 
                                        pFeatures[feaCount] = GetOneFeatureValue 
                                                (FeatureType,  
                                                FeatureStPos,  
                                                FeatureScale, 
                                                pInterData); 
                                        feaCount++; 
                                        FeatureScale.x += 1; 
                                } 
                                FeatureScale.y += 1; 
                        } 
                        //========= 计算E类特征 ========== 
                        FeatureScale.y = 2; 
                        FeatureType = 4; 
                        while (FeatureStPos.y + 3 * FeatureScale.y < imageSize.y) 
                        { 
                                FeatureScale.x = 2;  
                                while (FeatureStPos.x + FeatureScale.x < imageSize.x) 
                                { 
                                        pFeatures[feaCount] = GetOneFeatureValue 
                                                (FeatureType,  
                                                FeatureStPos,  
                                                FeatureScale, 
                                                pInterData); 
                                        feaCount++; 
                                        FeatureScale.x += 1; 
                                } 
                                FeatureScale.y += 1; 
                        } 
                        FeatureStPos.x += 1; 
                } 
                FeatureStPos.y += 1; 
        } 
 
        return; 
} 
 
 
///////////////////////////////////////////////////////////////////////////////////////// 
//针对每一个特定的分类器计算出它的分类错误率 
float HarrFeature::GetOneFeatureValue(BYTE FeatureType, POINT FeatureStPos, POINT FeatureScale, 
                                      float* pFaceData) 
{ 
        float    *pDataFromFile;//一个训练图象的积分图象数据 
        int      posLeftTop0, posRightBottom0, posRightTop0, posLeftBottom0, 
                posMiddleTop0, posMiddleBottom0; 
 
        int      posLeftTop1, posRightBottom1, posRightTop1, posLeftBottom1, 
                posLeftMiddle1, posRightMiddle1; 
 
        int      posLeftTop2, posRightBottom2, posRightTop2, posLeftBottom2, 
                posMiddleLTop2, posMiddleRTop2, posMiddleLBottom2, posMiddleRBottom2; 
 
        int      posLeftTop3, posMiddleTop3, posRightTop3, 
                posLeftMiddle3, posMiddleMiddle3, posRightMiddle3, 
                posLeftBottom3, posMiddleBottom3, posRightBottom3; 
 
        int      posLeftTop4, posRightTop4, posLeftTMiddle4, posRightTMiddle4, 
                posLeftBMiddle4, posRightBMiddle4, posLeftBottom4, posRightBottom4; 
        float    result; 
 
        //***********************// 
        //  如果计算的是A类特征  //  
        //***********************// 
        if (FeatureType == 0) 
        { 
                posLeftTop0 = imageSize.x * (FeatureStPos.y) + (FeatureStPos.x); 
                posMiddleTop0 = posLeftTop0 + FeatureScale.x; 
                posRightTop0 = posMiddleTop0 + FeatureScale.x; 
                posLeftBottom0 = imageSize.x * (FeatureStPos.y+FeatureScale.y) + (FeatureStPos.x); 
                posMiddleBottom0 = posLeftBottom0 + FeatureScale.x; 
                posRightBottom0 = posMiddleBottom0 + FeatureScale.x; 
 
                pDataFromFile = pFaceData; 
 
                result = pDataFromFile [posLeftBottom0]  
                + pDataFromFile [posRightBottom0]  
                - pDataFromFile [posLeftTop0]  
                - pDataFromFile [posRightTop0]  
                + 2 * (pDataFromFile [posMiddleTop0]  
                - pDataFromFile [posMiddleBottom0]); 
        } 
        //***********************// 
        //  如果计算的是B类特征  //  
        //***********************// 
        if (FeatureType == 1) 
        { 
                posLeftTop1 = imageSize.x * (FeatureStPos.y) + (FeatureStPos.x); 
                posRightTop1 = posLeftTop1 + FeatureScale.x; 
                posLeftMiddle1 = imageSize.x * (FeatureStPos.y + FeatureScale.y) + (FeatureStPos.x); 
                posRightMiddle1 = posLeftMiddle1 + FeatureScale.x; 
                posLeftBottom1 = imageSize.x * (FeatureStPos.y + 2*FeatureScale.y) + (FeatureStPos.x); 
                posRightBottom1 = posLeftBottom1 + FeatureScale.x; 
 
                pDataFromFile = pFaceData; 
                result = pDataFromFile [posLeftTop1]  
                + pDataFromFile [posLeftBottom1]  
                - pDataFromFile [posRightTop1]  
                + 2 * (pDataFromFile [posRightMiddle1]  
                - pDataFromFile [posLeftMiddle1])  
                        - pDataFromFile [posRightBottom1]; 
        } 
        //***********************// 
        //  如果计算的是C类特征  //  
        //***********************// 
        if(FeatureType == 2) 
        { 
                posLeftTop2 = imageSize.x * (FeatureStPos.y) + FeatureStPos.x; 
                posMiddleLTop2 = posLeftTop2 + FeatureScale.x; 
                posMiddleRTop2 = posMiddleLTop2 + FeatureScale.x; 
                posRightTop2 = posMiddleRTop2 + FeatureScale.x; 
                posLeftBottom2 = imageSize.x * (FeatureStPos.y + FeatureScale.y) + FeatureStPos.x; 
                posMiddleLBottom2 = posLeftBottom2 + FeatureScale.x; 
                posMiddleRBottom2 = posMiddleLBottom2 + FeatureScale.x; 
                posRightBottom2 = posMiddleRBottom2 + FeatureScale.x; 
 
                pDataFromFile = pFaceData; 
 
                result = (pDataFromFile [posRightTop2] + pDataFromFile [posLeftBottom2])  
                        - (pDataFromFile [posLeftTop2] + pDataFromFile [posRightBottom2])  
                        + 3*(pDataFromFile [posMiddleLTop2] + pDataFromFile [posMiddleRBottom2])  
                        - 3*(pDataFromFile [posMiddleRTop2] + pDataFromFile [posMiddleLBottom2]); 
        } 
        //***********************// 
        //  如果计算的是D类特征  //  
        //***********************// 
        if (FeatureType == 3) 
        { 
                posLeftTop3 = imageSize.x * (FeatureStPos.y) + FeatureStPos.x; 
                posMiddleTop3 = posLeftTop3 + FeatureScale.x; 
                posRightTop3 = posMiddleTop3 + FeatureScale.x; 
                posLeftMiddle3 = imageSize.x * (FeatureStPos.y + FeatureScale.y) + FeatureStPos.x; 
                posMiddleMiddle3 = posLeftMiddle3 + FeatureScale.x; 
                posRightMiddle3 = posMiddleMiddle3 + FeatureScale.x; 
                posLeftBottom3 = imageSize.x * (FeatureStPos.y + 2 * FeatureScale.y) + FeatureStPos.x; 
                posMiddleBottom3 = posLeftBottom3 + FeatureScale.x; 
                posRightBottom3 = posMiddleBottom3 + FeatureScale.x; 
 
                pDataFromFile = pFaceData; 
 
                result = 2 * (pDataFromFile [posMiddleTop3]  
                + pDataFromFile [posLeftMiddle3]  
                + pDataFromFile [posRightMiddle3]  
                + pDataFromFile [posMiddleBottom3])  
                        - (pDataFromFile [posLeftTop3]  
                        + pDataFromFile [posRightTop3]  
                        + pDataFromFile [posLeftBottom3]  
                        + pDataFromFile [posRightBottom3]  
                        + 4 * pDataFromFile [posMiddleMiddle3]); 
        } 
        //***********************// 
        //  如果计算的是E类特征  //  
        //***********************// 
        if (FeatureType == 4) 
        { 
                posLeftTop4 = imageSize.x * (FeatureStPos.y) + FeatureStPos.x; 
                posRightTop4 = posLeftTop4 + FeatureScale.x; 
 
                posLeftTMiddle4 = imageSize.x * (FeatureStPos.y + FeatureScale.y) + FeatureStPos.x; 
                posRightTMiddle4 = posLeftTMiddle4 + FeatureScale.x; 
 
                posLeftBMiddle4 = imageSize.x * (FeatureStPos.y + 2 * FeatureScale.y) + FeatureStPos.x; 
                posRightBMiddle4 = posLeftBMiddle4 + FeatureScale.x; 
 
                posLeftBottom4 = imageSize.x * (FeatureStPos.y + 3 * FeatureScale.y) + FeatureStPos.x; 
                posRightBottom4 = posLeftBottom4 + FeatureScale.x; 
 
                pDataFromFile = pFaceData; 
 
                result = 3*(pDataFromFile [posLeftTMiddle4] + pDataFromFile [posRightBMiddle4]) 
                        - 3*(pDataFromFile [posRightTMiddle4] + pDataFromFile [posLeftBMiddle4]) 
                        - (pDataFromFile [posLeftTop4] + pDataFromFile [posRightBottom4]) 
                        + (pDataFromFile [posRightTop4] + pDataFromFile [posLeftBottom4]);  
        } 
        return result; 
} 
 
//////////////////////////////////////////////////////////////////////// 
void HarrFeature::ReOrderFeatures(CString strSrcPath, CString strDstPath) 
{ 
        DWORD   FileSizeHigh;   
        DWORD   FileSizeLow; 
        DWORD   bytesread; 
        printf("Re-ordering features......\n"); 
        HANDLE fil = CreateFile(strSrcPath, 
                GENERIC_READ|GENERIC_WRITE, 
                FILE_SHARE_WRITE, 
                NULL, 
                OPEN_EXISTING, 
                FILE_ATTRIBUTE_NORMAL, 
                NULL); 
        FileSizeLow = GetFileSize (fil, &FileSizeHigh); 
        DWORD imageNum = FileSizeLow / (sizeof(float) * featureNum); 
 
        float *pSrcData = new float[imageNum * featureNum]; 
        ReadFile(fil, pSrcData, imageNum * featureNum * sizeof(float), 
                &bytesread, NULL); 
 
        CloseHandle((HANDLE)fil); 
 
        //重新排列特征的顺序 
        FILE *f_Feature = fopen(strDstPath, "wb"); 
 
        float *pFinalFeature = new float[imageNum]; 
        for (int m=0; m<featureNum; m++) 
        { 
                printf("feaid = %d\r", m); 
                for (int n=0; n<imageNum; n++) 
                { 
                        pFinalFeature[n] = pSrcData[n * featureNum + m]; 
                } 
                //将特征写入文件 
                fwrite(pFinalFeature, sizeof(float), imageNum, f_Feature); 
        } 
        fclose(f_Feature); 
 
        delete []pSrcData; 
        delete []pFinalFeature; 
}