现在的位置: 首页 > 综合 > 正文

全局Haar-Like特征图像识别的C++实现

2013年10月06日 ⁄ 综合 ⁄ 共 3507字 ⁄ 字号 评论关闭
 cvdirectcascade.h:
#include "cv.h"

struct TWeakClassifier
{
    CvRect Rectangles[
2];
}
;

struct PtrWeakClassifier
{
    
long *tl0, *tr0, *bl0, *br0;
    
long *tl1, *tr1, *bl1, *br1;
    
int offset, size0, size1;
    
int Threshold;
}
;

struct ListObjectType
{
    
int x, y, width, height;
    
int w;
    
bool use;
    ListObjectType 
*next;
}
;

struct TLearnedObject
{
    TWeakClassifier 
*Classifiers;
    PtrWeakClassifier 
*ScaleClassifiers;
    
int ObjectWidth, ObjectHeight;
    
int NClassifiers;
}
;

struct TDetectSettings
{
    
double ScaleRatio, OffsetX, OffsetY;
    
int Ignore, MinW;
    
int StartSubWindow, EndSubWindow;
}
;

bool LoadDefaultSetting(char *FileName, TDetectSettings &DS);
void LearnClassifiers(unsigned char *IptImage, TLearnedObject &target, int IptWidth, int IptHeight);
int DetectObject(TLearnedObject &target, unsigned char *FrameImage, int Width, int Height, TDetectSettings &DS, CvRect *&Objects);

cvdirectcascade.cpp:

#include <iostream.h>
#include 
<stdio.h>
#include 
<math.h>
#include 
<string.h>
#include 
<ctype.h>
#include 
<stdlib.h>

#include 
"cv.h"
#include 
"cvdirectcascade.h"

bool Overlapped(ListObjectType *obj1, ListObjectType *obj2)
{
    
int cx = obj1->+ obj1->width / 2;
    
int cy = obj1->+ obj1->height / 2;
    
if ((cx > obj2->x) && (cx < obj2->+ obj2->width) && (cy > obj2->y) && (cy < obj2->+ obj2->height))
        
return true;
    cx 
= obj2->+ obj2->width / 2;
    cy 
= obj2->+ obj2->height / 2;
    
if ((cx > obj1->x) && (cx < obj1->+ obj1->width) && (cy > obj1->y) && (cy < obj1->+ obj1->height))
        
return true;
    
return false;
}


void RestoreImage(unsigned char *IptImage, long *&IntegralImage, int IptWidth, int IptHeight)
{
    IntegralImage 
= new long [(IptWidth + 1* (IptHeight + 1)];

    
long *pic = IntegralImage;
    
for (int i = 0; i < IptWidth + 1; i++)
    
{
        
*pic = 0; pic++;
    }

    
for (int j = 0; j < IptHeight; j++)
    
{
        
*pic = 0; pic+=IptWidth + 1;
    }


    unsigned 
char *pc = IptImage;
    
long *picx, *picy, *picxy;
    picxy 
= IntegralImage;
    pic 
= picxy + IptWidth + 2;
    picx 
= picxy + IptWidth + 1;
    picy 
= picxy + 1;
    
for (i = 0; i < IptHeight; i++)
    
{
        
for (j = 0; j < IptWidth; j++)
        
{
            
*pic = *pc + *picx + *picy - *picxy;
            pic
++; picx++; picy++; picxy++;
            pc
++;
        }

        pic
++; picx++; picy++; picxy++;
    }

}


int CalculateXBoundary(unsigned char *IptImage, int IptWidth, int IptHeight, int x, int y, int width, int height)
{
    
int T, NewT = width / 2, C1 = 0, C2 = 0, D1 = 0, D2 = 0;
    unsigned 
char *ptr, *oldptr;
    oldptr 
= IptImage + IptWidth * y + x;
    
int step = IptWidth - width;
    
do{
        T 
= NewT;
        C1 
= 0; C2 = 0; D1 = 0; D2 = 0;
        ptr 
= oldptr;
        
for (int j = y; j < y + height; j++)
        
{
            
for (int i = x; i < x + T; i++)
            
{
                C1 
+= (i - x) * *ptr;
                D1 
+= *ptr;
                ptr
++;
            }

            
for (i = x + T; i < x + width; i++)
            
{
                C2 
+= (i - x) * *ptr;
                D2 
+= *ptr;
                ptr
++;
            }

            ptr
+=step;
        }

        C1 
= C1 / D1; C2 = C2 / D2;
        NewT 
= (C1 + C2) / 2;
    }
while (NewT != T);
    
return T;
}


int CalculateYBoundary(unsigned char *IptImage, int IptWidth, int IptHeight, int x, int y, int width, int height)
{
    
int T, NewT = height / 2, C1 = 0, C2 = 0, D1 = 0, D2 = 0;
    unsigned 
char *ptr, *oldptr;
    oldptr 
= IptImage + IptWidth * y + x;
    
int step = IptWidth - width;

抱歉!评论已关闭.