📄 cvhaar.cpp
字号:
}
}
}
}
else
{
cvIntegral( img, sum, sqsum, tilted );
if( do_canny_pruning )
{
sumcanny = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
cvCanny( img, temp, 0, 50, 3 );
cvIntegral( temp, sumcanny );
}
if( (unsigned)split_stage >= (unsigned)cascade->count ||
cascade->hid_cascade->is_tree )
{
split_stage = cascade->count;
npass = 1;
}
for( factor = 1; factor*cascade->orig_window_size.width < img->cols - 10 &&
factor*cascade->orig_window_size.height < img->rows - 10;
factor *= scale_factor )
{
const double ystep = MAX( 2, factor );
CvSize win_size = { cvRound( cascade->orig_window_size.width * factor ),
cvRound( cascade->orig_window_size.height * factor )};
CvRect equ_rect = { 0, 0, 0, 0 };
int *p0 = 0, *p1 = 0, *p2 = 0, *p3 = 0;
int *pq0 = 0, *pq1 = 0, *pq2 = 0, *pq3 = 0;
int pass, stage_offset = 0;
int stop_height = cvRound((img->rows - win_size.height) / ystep);
if( win_size.width < min_size.width || win_size.height < min_size.height )
continue;
cvSetImagesForHaarClassifierCascade( cascade, sum, sqsum, tilted, factor );
cvZero( temp );
if( do_canny_pruning )
{
equ_rect.x = cvRound(win_size.width*0.15);
equ_rect.y = cvRound(win_size.height*0.15);
equ_rect.width = cvRound(win_size.width*0.7);
equ_rect.height = cvRound(win_size.height*0.7);
p0 = (int*)(sumcanny->data.ptr + equ_rect.y*sumcanny->step) + equ_rect.x;
p1 = (int*)(sumcanny->data.ptr + equ_rect.y*sumcanny->step)
+ equ_rect.x + equ_rect.width;
p2 = (int*)(sumcanny->data.ptr + (equ_rect.y + equ_rect.height)*sumcanny->step) + equ_rect.x;
p3 = (int*)(sumcanny->data.ptr + (equ_rect.y + equ_rect.height)*sumcanny->step)
+ equ_rect.x + equ_rect.width;
pq0 = (int*)(sum->data.ptr + equ_rect.y*sum->step) + equ_rect.x;
pq1 = (int*)(sum->data.ptr + equ_rect.y*sum->step)
+ equ_rect.x + equ_rect.width;
pq2 = (int*)(sum->data.ptr + (equ_rect.y + equ_rect.height)*sum->step) + equ_rect.x;
pq3 = (int*)(sum->data.ptr + (equ_rect.y + equ_rect.height)*sum->step)
+ equ_rect.x + equ_rect.width;
}
cascade->hid_cascade->count = split_stage;
for( pass = 0; pass < npass; pass++ )
{
#ifdef _OPENMP
#pragma omp parallel for num_threads(max_threads), schedule(dynamic)
#endif
for( int _iy = 0; _iy < stop_height; _iy++ )
{
int iy = cvRound(_iy*ystep);
int _ix, _xstep = 1;
int stop_width = cvRound((img->cols - win_size.width) / ystep);
uchar* mask_row = temp->data.ptr + temp->step * iy;
for( _ix = 0; _ix < stop_width; _ix += _xstep )
{
int ix = cvRound(_ix*ystep); // it really should be ystep
if( pass == 0 )
{
int result;
_xstep = 2;
if( do_canny_pruning )
{
int offset;
int s, sq;
offset = iy*(sum->step/sizeof(p0[0])) + ix;
s = p0[offset] - p1[offset] - p2[offset] + p3[offset];
sq = pq0[offset] - pq1[offset] - pq2[offset] + pq3[offset];
if( s < 100 || sq < 20 )
continue;
}
result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy), 0 );
if( result > 0 )
{
if( pass < npass - 1 )
mask_row[ix] = 1;
else
{
CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
#ifndef _OPENMP
cvSeqPush( seq, &rect );
#else
cvSeqPush( seq_thread[omp_get_thread_num()], &rect );
#endif
}
}
if( result < 0 )
_xstep = 1;
}
else if( mask_row[ix] )
{
int result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy),
stage_offset );
if( result > 0 )
{
if( pass == npass - 1 )
{
CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
#ifndef _OPENMP
cvSeqPush( seq, &rect );
#else
cvSeqPush( seq_thread[omp_get_thread_num()], &rect );
#endif
}
}
else
mask_row[ix] = 0;
}
}
}
stage_offset = cascade->hid_cascade->count;
cascade->hid_cascade->count = cascade->count;
}
}
}
#ifdef _OPENMP
// gather the results
for( i = 0; i < max_threads; i++ )
{
CvSeq* s = seq_thread[i];
int j, total = s->total;
CvSeqBlock* b = s->first;
for( j = 0; j < total; j += b->count, b = b->next )
cvSeqPushMulti( seq, b->data, b->count );
}
#endif
if( min_neighbors != 0 )
{
// group retrieved rectangles in order to filter out noise
int ncomp = cvSeqPartition( seq, 0, &idx_seq, (CvCmpFunc)is_equal, 0 );
CV_CALL( comps = (CvAvgComp*)cvAlloc( (ncomp+1)*sizeof(comps[0])));
memset( comps, 0, (ncomp+1)*sizeof(comps[0]));
// count number of neighbors
for( i = 0; i < seq->total; i++ )
{
CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
int idx = *(int*)cvGetSeqElem( idx_seq, i );
assert( (unsigned)idx < (unsigned)ncomp );
comps[idx].neighbors++;
comps[idx].rect.x += r1.x;
comps[idx].rect.y += r1.y;
comps[idx].rect.width += r1.width;
comps[idx].rect.height += r1.height;
}
// calculate average bounding box
for( i = 0; i < ncomp; i++ )
{
int n = comps[i].neighbors;
if( n >= min_neighbors )
{
CvAvgComp comp;
comp.rect.x = (comps[i].rect.x*2 + n)/(2*n);
comp.rect.y = (comps[i].rect.y*2 + n)/(2*n);
comp.rect.width = (comps[i].rect.width*2 + n)/(2*n);
comp.rect.height = (comps[i].rect.height*2 + n)/(2*n);
comp.neighbors = comps[i].neighbors;
cvSeqPush( seq2, &comp );
}
}
// filter out small face rectangles inside large face rectangles
for( i = 0; i < seq2->total; i++ )
{
CvAvgComp r1 = *(CvAvgComp*)cvGetSeqElem( seq2, i );
int j, flag = 1;
for( j = 0; j < seq2->total; j++ )
{
CvAvgComp r2 = *(CvAvgComp*)cvGetSeqElem( seq2, j );
int distance = cvRound( r2.rect.width * 0.2 );
if( i != j &&
r1.rect.x >= r2.rect.x - distance &&
r1.rect.y >= r2.rect.y - distance &&
r1.rect.x + r1.rect.width <= r2.rect.x + r2.rect.width + distance &&
r1.rect.y + r1.rect.height <= r2.rect.y + r2.rect.height + distance &&
(r2.neighbors > MAX( 3, r1.neighbors ) || r1.neighbors < 3) )
{
flag = 0;
break;
}
}
if( flag )
{
cvSeqPush( result_seq, &r1 );
/* cvSeqPush( result_seq, &r1.rect ); */
}
}
}
__END__;
#ifdef _OPENMP
for( i = 0; i < max_threads; i++ )
{
if( seq_thread[i] )
cvReleaseMemStorage( &seq_thread[i]->storage );
}
#endif
cvReleaseMemStorage( &temp_storage );
cvReleaseMat( &sum );
cvReleaseMat( &sqsum );
cvReleaseMat( &tilted );
cvReleaseMat( &temp );
cvReleaseMat( &sumcanny );
cvReleaseMat( &norm_img );
cvReleaseMat( &img_small );
cvFree( &comps );
return result_seq;
}
static CvHaarClassifierCascade*
icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
{
int i;
CvHaarClassifierCascade* cascade = icvCreateHaarClassifierCascade(n);
cascade->orig_window_size = orig_window_size;
for( i = 0; i < n; i++ )
{
int j, count, l;
float threshold = 0;
const char* stage = input_cascade[i];
int dl = 0;
/* tree links */
int parent = -1;
int next = -1;
sscanf( stage, "%d%n", &count, &dl );
stage += dl;
assert( count > 0 );
cascade->stage_classifier[i].count = count;
cascade->stage_classifier[i].classifier =
(CvHaarClassifier*)cvAlloc( count*sizeof(cascade->stage_classifier[i].classifier[0]));
for( j = 0; j < count; j++ )
{
CvHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
int k, rects = 0;
char str[100];
sscanf( stage, "%d%n", &classifier->count, &dl );
stage += dl;
classifier->haar_feature = (CvHaarFeature*) cvAlloc(
classifier->count * ( sizeof( *classifier->haar_feature ) +
sizeof( *classifier->threshold ) +
sizeof( *classifier->left ) +
sizeof( *classifier->right ) ) +
(classifier->count + 1) * sizeof( *classifier->alpha ) );
classifier->threshold = (float*) (classifier->haar_feature+classifier->count);
classifier->left = (int*) (classifier->threshold + classifier->count);
classifier->right = (int*) (classifier->left + classifier->count);
classifier->alpha = (float*) (classifier->right + classifier->count);
for( l = 0; l < classifier->count; l++ )
{
sscanf( stage, "%d%n", &rects, &dl );
stage += dl;
assert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
for( k = 0; k < rects; k++ )
{
CvRect r;
int band = 0;
sscanf( stage, "%d%d%d%d%d%f%n",
&r.x, &r.y, &r.width, &r.height, &band,
&(classifier->haar_feature[l].rect[k].weight), &dl );
stage += dl;
classifier->haar_feature[l].rect[k].r = r;
}
sscanf( stage, "%s%n", str, &dl );
stage += dl;
classifier->haar_feature[l].tilted = strncmp( str, "tilted", 6 ) == 0;
for( k = rects; k < CV_HAAR_FEATURE_MAX; k++ )
{
memset( classifier->haar_feature[l].rect + k, 0,
sizeof(classifier->haar_feature[l].rect[k]) );
}
sscanf( stage, "%f%d%d%n", &(classifier->threshold[l]),
&(classifier->left[l]),
&(classifier->right[l]), &dl );
stage += dl;
}
for( l = 0; l <= classifier->count; l++ )
{
sscanf( stage, "%f%n", &(classifier->alpha[l]), &dl );
stage += dl;
}
}
sscanf( stage, "%f%n", &threshold, &dl );
stage += dl;
cascade->stage_classifier[i].threshold = threshold;
/* load tree links */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -