📄 cvcalibinit.cpp
字号:
if( q0->neighbors[k] == q )
{
q0->neighbors[k] = 0;
q0->count--;
break;
}
break;
}
}
}
// remove the quad
quad_count--;
quad_group[min_box_area_index] = quad_group[quad_count];
centers[min_box_area_index] = centers[quad_count];
}
__END__;
cvReleaseMemStorage( &temp_storage );
cvFree( ¢ers );
return quad_count;
}
//=====================================================================================
static int
icvFindConnectedQuads( CvCBQuad *quad, int quad_count, CvCBQuad **out_group,
int group_idx, CvMemStorage* storage )
{
CvMemStorage* temp_storage = cvCreateChildMemStorage( storage );
CvSeq* stack = cvCreateSeq( 0, sizeof(*stack), sizeof(void*), temp_storage );
int i, count = 0;
// Scan the array for a first unlabeled quad
for( i = 0; i < quad_count; i++ )
{
if( quad[i].count > 0 && quad[i].group_idx < 0)
break;
}
// Recursively find a group of connected quads starting from the seed quad[i]
if( i < quad_count )
{
CvCBQuad* q = &quad[i];
cvSeqPush( stack, &q );
out_group[count++] = q;
q->group_idx = group_idx;
while( stack->total )
{
cvSeqPop( stack, &q );
for( i = 0; i < 4; i++ )
{
CvCBQuad *neighbor = q->neighbors[i];
if( neighbor && neighbor->count > 0 && neighbor->group_idx < 0 )
{
cvSeqPush( stack, &neighbor );
out_group[count++] = neighbor;
neighbor->group_idx = group_idx;
}
}
}
}
cvReleaseMemStorage( &temp_storage );
return count;
}
//=====================================================================================
static int
icvCheckQuadGroup( CvCBQuad **quad_group, int quad_count,
CvCBCorner **out_corners, CvSize pattern_size )
{
const int ROW1 = 1000000;
const int ROW2 = 2000000;
const int ROW_ = 3000000;
int result = 0;
int i, out_corner_count = 0, corner_count = 0;
CvCBCorner** corners = 0;
CV_FUNCNAME( "icvCheckQuadGroup" );
__BEGIN__;
int j, k, kk;
int width = 0, height = 0;
int hist[5] = {0,0,0,0,0};
CvCBCorner* first = 0, *first2 = 0, *right, *cur, *below, *c;
CV_CALL( corners = (CvCBCorner**)cvAlloc( quad_count*4*sizeof(corners[0]) ));
// build dual graph, which vertices are internal quad corners
// and two vertices are connected iff they lie on the same quad edge
for( i = 0; i < quad_count; i++ )
{
CvCBQuad* q = quad_group[i];
/*CvScalar color = q->count == 0 ? cvScalar(0,255,255) :
q->count == 1 ? cvScalar(0,0,255) :
q->count == 2 ? cvScalar(0,255,0) :
q->count == 3 ? cvScalar(255,255,0) :
cvScalar(255,0,0);*/
for( j = 0; j < 4; j++ )
{
//cvLine( debug_img, cvPointFrom32f(q->corners[j]->pt), cvPointFrom32f(q->corners[(j+1)&3]->pt), color, 1, CV_AA, 0 );
if( q->neighbors[j] )
{
CvCBCorner *a = q->corners[j], *b = q->corners[(j+1)&3];
// mark internal corners that belong to:
// - a quad with a single neighbor - with ROW1,
// - a quad with two neighbors - with ROW2
// make the rest of internal corners with ROW_
int row_flag = q->count == 1 ? ROW1 : q->count == 2 ? ROW2 : ROW_;
if( a->row == 0 )
{
corners[corner_count++] = a;
a->row = row_flag;
}
else if( a->row > row_flag )
a->row = row_flag;
if( q->neighbors[(j+1)&3] )
{
if( a->count >= 4 || b->count >= 4 )
EXIT;
for( k = 0; k < 4; k++ )
{
if( a->neighbors[k] == b )
EXIT;
if( b->neighbors[k] == a )
EXIT;
}
a->neighbors[a->count++] = b;
b->neighbors[b->count++] = a;
}
}
}
}
if( corner_count != pattern_size.width*pattern_size.height )
EXIT;
for( i = 0; i < corner_count; i++ )
{
int n = corners[i]->count;
assert( 0 <= n && n <= 4 );
hist[n]++;
if( !first && n == 2 )
{
if( corners[i]->row == ROW1 )
first = corners[i];
else if( !first2 && corners[i]->row == ROW2 )
first2 = corners[i];
}
}
// start with a corner that belongs to a quad with a signle neighbor.
// if we do not have such, start with a corner of a quad with two neighbors.
if( !first )
first = first2;
if( !first || hist[0] != 0 || hist[1] != 0 || hist[2] != 4 ||
hist[3] != (pattern_size.width + pattern_size.height)*2 - 8 )
EXIT;
cur = first;
right = below = 0;
out_corners[out_corner_count++] = cur;
for( k = 0; k < 4; k++ )
{
c = cur->neighbors[k];
if( c )
{
if( !right )
right = c;
else if( !below )
below = c;
}
}
if( !right || right->count != 2 && right->count != 3 ||
!below || below->count != 2 && below->count != 3 )
EXIT;
cur->row = 0;
//cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,255,0), -1, 8, 0 );
first = below; // remember the first corner in the next row
// find and store the first row (or column)
for(j=1;;j++)
{
right->row = 0;
out_corners[out_corner_count++] = right;
//cvCircle( debug_img, cvPointFrom32f(right->pt), 3, cvScalar(0,255-j*10,0), -1, 8, 0 );
if( right->count == 2 )
break;
if( right->count != 3 || out_corner_count >= MAX(pattern_size.width,pattern_size.height) )
EXIT;
cur = right;
for( k = 0; k < 4; k++ )
{
c = cur->neighbors[k];
if( c && c->row > 0 )
{
for( kk = 0; kk < 4; kk++ )
{
if( c->neighbors[kk] == below )
break;
}
if( kk < 4 )
below = c;
else
right = c;
}
}
}
width = out_corner_count;
if( width == pattern_size.width )
height = pattern_size.height;
else if( width == pattern_size.height )
height = pattern_size.width;
else
EXIT;
// find and store all the other rows
for( i = 1; ; i++ )
{
if( !first )
break;
cur = first;
first = 0;
for( j = 0;; j++ )
{
cur->row = i;
out_corners[out_corner_count++] = cur;
//cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,0,255-j*10), -1, 8, 0 );
if( cur->count == 2 + (i < height-1) && j > 0 )
break;
right = 0;
// find a neighbor that has not been processed yet
// and that has a neighbor from the previous row
for( k = 0; k < 4; k++ )
{
c = cur->neighbors[k];
if( c && c->row > i )
{
for( kk = 0; kk < 4; kk++ )
{
if( c->neighbors[kk] && c->neighbors[kk]->row == i-1 )
break;
}
if( kk < 4 )
{
right = c;
if( j > 0 )
break;
}
else if( j == 0 )
first = c;
}
}
if( !right )
EXIT;
cur = right;
}
if( j != width - 1 )
EXIT;
}
if( out_corner_count != corner_count )
EXIT;
// check if we need to transpose the board
if( width != pattern_size.width )
{
CV_SWAP( width, height, k );
memcpy( corners, out_corners, corner_count*sizeof(corners[0]) );
for( i = 0; i < height; i++ )
for( j = 0; j < width; j++ )
out_corners[i*width + j] = corners[j*height + i];
}
// check if we need to revert the order in each row
{
CvPoint2D32f p0 = out_corners[0]->pt, p1 = out_corners[pattern_size.width-1]->pt,
p2 = out_corners[pattern_size.width]->pt;
if( (p1.x - p0.x)*(p2.y - p1.y) - (p1.y - p0.y)*(p2.x - p1.x) < 0 )
{
if( width % 2 == 0 )
{
for( i = 0; i < height; i++ )
for( j = 0; j < width/2; j++ )
CV_SWAP( out_corners[i*width+j], out_corners[i*width+width-j-1], c );
}
else
{
for( j = 0; j < width; j++ )
for( i = 0; i < height/2; i++ )
CV_SWAP( out_corners[i*width+j], out_corners[(height - i - 1)*width+j], c );
}
}
}
result = corner_count;
__END__;
if( result <= 0 && corners )
{
corner_count = MIN( corner_count, pattern_size.width*pattern_size.height );
for( i = 0; i < corner_count; i++ )
out_corners[i] = corners[i];
result = -corner_count;
}
cvFree( &corners );
return result;
}
//=====================================================================================
static void icvFindQuadNeighbors( CvCBQuad *quads, int quad_count )
{
const float thresh_scale = 1.f;
int idx, i, k, j;
float dx, dy, dist;
// find quad neighbors
for( idx = 0; idx < quad_count; idx++ )
{
CvCBQuad* cur_quad = &quads[idx];
// choose the points of the current quadrangle that are close to
// some points of the other quadrangles
// (it can happen for split corners (due to dilation) of the
// checker board). Search only in other quadrangles!
// for each corner of this quadrangle
for( i = 0; i < 4; i++ )
{
CvPoint2D32f pt;
float min_dist = FLT_MAX;
int closest_corner_idx = -1;
CvCBQuad *closest_quad = 0;
CvCBCorner *closest_corner = 0;
if( cur_quad->neighbors[i] )
continue;
pt = cur_quad->corners[i]->pt;
// find the closest corner in all other quadrangles
for( k = 0; k < quad_count; k++ )
{
if( k == idx )
continue;
for( j = 0; j < 4; j++ )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -