main

int main(/*const IplImage* image_l,const IplImage *image_r*/)
{
//assert(argc==3);
const char* left_filename="C:\\OpenCV2.1\\samples\\c\\scene_l.bmp";
const char* right_filename="C:\\OpenCV2.1\\samples\\c\\scene_r.bmp";
clock_t begin,end;
float time;
int count=0;
int i;
MIpoint *m=(MIpoint*)malloc(1000*sizeof(MIpoint));
FHParam param={4/*octaves*/,4/*intervals*/,2/*init_sample*/,0.0001f/*thres*/};/////////////参数设定
mImage *img_l;
mImage *img_r;
image_left = cvLoadImage(left_filename);
image_right = cvLoadImage(right_filename);

if (image_right==NULL || image_right==NULL) return -1;
CvSize image_size = cvGetSize(image_left);
img_l=ConvertIplImage(image_left);
img_r=ConvertIplImage(image_right);
begin=clock();
SurfImageMatch(img_l,img_r,&param,m,&count);
end=clock();
time=(float)(end-begin);
printf("time need:%gms\n",time);
CvMat* left_points_mat = cvCreateMat(count, 2, CV_32F);
CvMat* right_points_mat = cvCreateMat(count, 2, CV_32F);
for (i=0; i<count; ++i)
{
cvmSet(left_points_mat, i, 0, m[i].MIp1.x);
cvmSet(left_points_mat, i, 1, m[i].MIp1.y);
cvmSet(right_points_mat, i, 0, m[i].MIp2.x);
cvmSet(right_points_mat, i, 1, m[i].MIp2.y);
}

// 4. rectify
double _K[] = {800, 0, image_size.width/2, 0, 800, image_size.height/2, 0, 0, 1};
CvMat K = cvMat(3,3, CV_64F, _K);
// 4.1 Rotation and translation
double _rot[9], _tran[3], _R1[9], _R2[9], _P1[12], _P2[12], _F[9];
decompose_fundamental_matrix(_K, _F, _rot, _tran, left_points_mat, right_points_mat);

// 4.2 homography transform
stereo_rectify(_K, image_size, _rot, _tran, _R1, _R2, _P1, _P2);

CvMat R1 = cvMat(3,3, CV_64F, _R1);
CvMat R2 = cvMat(3,3, CV_64F, _R2);
CvMat P1 = cvMat(3,4, CV_64F, _P1);
CvMat P2 = cvMat(3,4, CV_64F, _P2);

// 5. warp images
double _KBig[] = {400, 0, image_size.width, 0, 400, image_size.height, 0, 0, 1};
CvSize bigsize = cvSize(image_size.width*2, image_size.height*2);
CvMat KBig = cvMat(3,3, CV_64F, _KBig);
CvMat* mx1 = cvCreateMat( image_size.height*2,
image_size.width*2, CV_32F );
CvMat* my1 = cvCreateMat( image_size.height*2,
image_size.width*2, CV_32F );
CvMat* mx2 = cvCreateMat( image_size.height*2,
image_size.width*2, CV_32F );
CvMat* my2 = cvCreateMat( image_size.height*2,
image_size.width*2, CV_32F );
cvInitUndistortRectifyMap(&K,NULL,&R1,&KBig,mx1,my1);
cvInitUndistortRectifyMap(&K,NULL,&R2,&KBig,mx2,my2);

IplImage* stereo_left = cvCreateImage(bigsize, 8, 3);
IplImage* stereo_right = cvCreateImage(bigsize, 8, 3);

cvRemap(image_left, stereo_left, mx1, my1);
cvRemap(image_right, stereo_right, mx2, my2);

cvSaveImage("stereo_left.jpg", stereo_left);
cvSaveImage("stereo_right.jpg", stereo_right);
//system("pause");
return 0;
}

posted on 2011-11-03 17:13  伪君  阅读(347)  评论(0编辑  收藏  举报

导航