Example #1
0
int main(int argc, char* argv[])
{
	int i = -1;
	int j = -1;

	int arg = 0;
	while( ++arg < argc) 
	{ 
		if( !strcmp(argv[arg], "-i") )
			i = atoi( argv[++arg] );

		if( !strcmp(argv[arg], "-j") )
			j = atoi( argv[++arg] );
	}	

	try
	{
		CFeatureArray set_i, set_j;

		// Step 1: load the extracted features using LoadSiftFromFile()
		char strBuf[128];
		sprintf( strBuf, "%04d.key", i );
		LoadSiftFromFile( set_i, strBuf );
		
		sprintf( strBuf, "%04d.key", j );
		LoadSiftFromFile( set_j, strBuf );

		// Step 2: match features
		MatchArray aryMatch;
		MatchSiftFeatures( aryMatch, set_i, set_j );

		// Step 3: Save the matches
		SaveMatchArray( aryMatch, i, j );
	}
	catch( exception& err )
	{
		printf( "%s\n", err.what() );
	}

	return 0;
}
Example #2
0
int main(int argc, char* argv[])
{
	bool bOpt = false;

	// the number of images in the sequence
	int ib = 0;
	int ie = 0;
	int numImage = 0;
	
	// the intrinsic camera parameters
	double f = 0;
	double u, v, alpha;
	
	// homography inlier tolerance
	float tol = 2.f;

	int arg = 0;
	while( ++arg < argc) 
	{ 
		if( !strcmp(argv[arg], "-ib") )
			ib = atoi( argv[++arg] );

		if( !strcmp(argv[arg], "-ie") )
			ie = atoi( argv[++arg] );

		if( !strcmp(argv[arg], "-f") )
			f = atof( argv[++arg] );

		if( !strcmp(argv[arg], "-u") )
			u = atof( argv[++arg] );

		if( !strcmp(argv[arg], "-v") )
			v = atof( argv[++arg] );

		if( !strcmp(argv[arg], "-alpha") )
			alpha = atof( argv[++arg] );

		if( !strcmp(argv[arg], "-tol") )
			tol = atof( argv[++arg] );
	}

	// an initial guess of the focal length
	if( f == 0 )
		f = u*4;

	// the indices of the image sequence
	numImage = ie-ib+1;
	int* imgId = new int[ numImage ];
	for( int i=0; i<numImage; ++i )
		imgId[i] = ib+i;

	// the feature set for each image
	CFeatureArray* ftSet = new CFeatureArray[ numImage ];

	// the match array for each consecutive image pair
	MatchArray* aryMatch = new MatchArray[ numImage ];

	// the camera poses for each image
	CCamera* cam = new CCamera[ numImage ];

	// the homographies for each consecutive image pair
	CHomography* h**o = new CHomography[ numImage ];

	// the intrinsic camera matrix
	const double dK[9] = {
		f,       0, u,
		0, f*alpha, v,
		0,       0, 1};

	for( int i=0; i<numImage; ++i )
	{
		// load the features for each image
		char strSift[128];
		sprintf( strSift, "%04d.key", imgId[i] );
		LoadSiftFromFile( ftSet[i], strSift );

		// initialize camera intrinsic parameters
		cam[i].SetIntMatrix( dK );
	}	

	// 
	for( int j=1; j<numImage; ++j )
	{
		// index of the previous camera
		int i = j - 1;

		printf("i: %d   j; %d\n", i, j);

		// Step 1: load the homography of image pair < imgId[i-1], imgId[i] >
		LoadHomography( h**o[j], imgId[i], imgId[j] );

		// step 2: load the matches of image pair < imgId[i-1], imgId[i] >
		LoadMatchArray( aryMatch[j], imgId[i], imgId[j] );

		// step 3: estimate camera rotation from the homography 
		double dR[9];
		double dK1[9];
		double dK2[9];
		cam[i].GetIntMatrix( dK1 );
		cam[j].GetIntMatrix( dK2 );
			
		PoseFromHomography( dR, h**o[j], dK1, dK2 );

		// step 4: keep homography inliers
		GetHomographyInliers( aryMatch[j], aryMatch[j], ftSet[i], ftSet[j], h**o[j], tol );
			
		// step 5: perform bundle adjustment to find the best R and f
		//         If this is the first pair, we need to estimate R, f1, and f2.
		//         Otherwise we can fix cam[i-1] and estimate R and f2.
		if( j==0 )
			OptimizePair( cam[i], cam[j], dR, ftSet[i], ftSet[j], aryMatch[j] );

		else
			OptimizeSingle( cam[i], cam[j], dR, ftSet[i], ftSet[j], aryMatch[j] );
	}

	//TODO: optimize the camera poses using bundle adjustment,
	//      and store the result back to cam[i]
	OptimizeSet( cam, h**o, ftSet, aryMatch, numImage );

	// Save camera poses
	for( int i=0; i<numImage; ++i )
		SaveCamera( cam[i], imgId[i] );

	delete [] h**o;
	delete [] cam;
	delete [] aryMatch;
	delete [] ftSet;
	delete [] imgId;

	return 0;
}