【OpenCV】 基于 ransac 算法的 sift 特征匹配程序(开发环境为OpenCV2.3.1+VS2010)

本文介绍了一种使用OpenCV2.3.1和Visual Studio 2010进行图像拼接的方法,涉及到标定、RANSAC算法和SIFT特征匹配。程序首先对USB摄像头进行标定,然后通过RANSAC算法去除坏点,利用SIFT特征进行匹配,最终实现图像拼接。代码中详细展示了从标定到匹配的步骤,包括内参矩阵和畸变系数的计算。
摘要由CSDN通过智能技术生成

因为前一阵子忙于自己的毕设,所以就没有及时更新日志,今天正好没其他事儿,所以,我就把图像拼接程序写上来了。。。欢迎大家的阅读以及批评和指正。

下面的程序是基于opencv2.3.1+vs2010的搭建的环境下编程的。。。

首先对两个usb通用摄像头进行了标定。然后进行图像拼接,最后进行测距。

这不是最终版,因为最终版是我的论文内容。

所以,要过一阵子才能写上来,因为现在写上来我的论文可能被互联网的查重率坑爹的。。。

呵呵,请大家见谅。。。不过,很多重点代码都是完好无损的。。。。


#include <math.h>
#include <ctime>
#include <cv.h>
#include <highgui.h>
#include <features2d/features2d.hpp>
#include <cvaux.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;


void main()
{
ofstream fout("Result.txt");


float f_left[2],f_right[2];
IplImage * show; //RePlay图像指针
    cvNamedWindow("RePlay",1);
    int number_image_copy=7; //复制图像帧数
    CvSize board_size=cvSize(9,6); //标定板角点数
    CvSize2D32f square_size=cvSize2D32f(10,10); //假设我的每个标定方格长宽都是1.82厘米
    float square_length=square_size.width; //方格长度
    float square_height=square_size.height; //方格高度
    int board_width=board_size.width; //每行角点数
    int board_height=board_size.height; //每列角点数
    int total_per_image=board_width*board_height; //每张图片角点总数
    int count; //存储每帧图像中实际识别的角点数
    int found; //识别标定板角点的标志位
    int step; //存储步长,step=successes*total_per_image;
    int successes=0; //存储成功找到标定板上所有角点的图像帧数
    int a=1; //临时变量,表示在操作第a帧图像
const int NImages = 7;//图片总数


CvMat *rotation_vectors;
    CvMat *translation_vectors;
    CvPoint2D32f * image_points_buf = new CvPoint2D32f[total_per_image]; //存储角点图像坐标的数组
    CvMat * image_points=cvCreateMat(NImages*total_per_image,2,CV_32FC1);//存储角点的图像坐标的矩阵
    CvMat * object_points=cvCreateMat(NImages*total_per_image,3,CV_32FC1);//存储角点的三维坐标的矩阵
    CvMat * point_counts=cvCreateMat(NImages,1,CV_32SC1);//存储每帧图像的识别的角点数
    CvMat * intrinsic_matrix=cvCreateMat(3,3,CV_32FC1);//内参数矩阵
    CvMat * distortion_coeffs=cvCreateMat(5,1,CV_32FC1);//畸变系数
    rotation_vectors = cvCreateMat(NImages,3,CV_32FC1);//旋转矩阵
    translation_vectors = cvCreateMat(NImages,3,CV_32FC1);//平移矩阵
ifstream fin("calibdata1.txt"); /* 定标所用图像文件的路径 */


    while(a<=number_image_copy)
    {
        //sprintf_s (filename,"%d.jpg",a);
string filename;
getline(fin,filename);
        show=cvLoadImage(filename.c_str(),1);
        //寻找棋盘图的内角点位置
        found=cvFindChessboardCorners(show,board_size,image_points_buf,&count,
        CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);


        if (found==0)
        { //如果没找到标定板角点时
            cout<<"第"<<a<<"帧图片无法找到棋盘格所有角点!\n\n";
fout<<"第"<<a<<"帧图片无法找到棋盘格所有角点!\n\n";
            cvNamedWindow("RePlay",1);
            cvShowImage("RePlay",show);
            cvWaitKey(2000);


        }
        else
        { //找到标定板角点时
            cout<<"第"<<a<<"帧图像成功获得"<<count<<"个角点...\n";
fout<<"第"<<a<<"帧图像成功获得"<<count<<"个角点...\n";
            cvNamedWindow("RePlay",1);
            IplImage * gray_image= cvCreateImage(cvGetSize(show),8,1);
            cvCvtColor(show,gray_image,CV_BGR2GRAY);
            cout<<"获取源图像灰度图过程完成...\n";
fout<<"获取源图像灰度图过程完成...\n";


            cvFindCornerSubPix(gray_image,image_points_buf,count,cvSize(11,11),cvSize(-1,-1),
            cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
            cout<<"灰度图亚像素化过程完成...\n";
fout<<"灰度图亚像素化过程完成...\n";
            cvDrawChessboardCorners(show,board_size,image_points_buf,count,found);
            cout<<"在源图像上绘制角点过程完成...\n\n";
fout<<"在源图像上绘制角点过程完成...\n\n";
            cvShowImage("RePlay",show);
            cvWaitKey(500);
        }


        if(total_per_image==count)
        {
            step=successes*total_per_image; //计算存储相应坐标数据的步长
            for(int i=step,j=0;j<total_per_image;++i,++j)
            {
                CV_MAT_ELEM(*image_points, float,i,0)=image_points_buf[j].x;
                CV_MAT_ELEM(*image_points, float,i,1)=image_points_buf[j].y;
                CV_MAT_ELEM(*object_points,float,i,0)=(float)((j/board_width)*square_length);
                CV_MAT_ELEM(*object_points,float,i,1)=(float)((j%board_width)*square_height);
                CV_MAT_ELEM(*object_points,float,i,2)=0.0f;
            }
            CV_MAT_ELEM(*point_counts,int,successes,0)=total_per_image;
            successes++;
        }
cout<<"hahahahh======"<<a<<endl;
fout<<"hahahahh======"<<a<<endl;
        a++;
    }


    cvReleaseImage(&show);
    cvDestroyWindow("RePlay");




    cout<<"*********************************************\n";
fout<<"*********************************************\n";
    cout<<NImages<<"帧图片中,标定成功的图片为"<<successes<<"帧...\n";
fout<<NImages<<"帧图片中,标定成功的图片为"<<successes<<"帧...\n";
    cout<<NImages<<"帧图片中,标定失败的图片为"<<NImages-successes<<"帧...\n\n";
fout<<NImages<<"帧图片中,标定失败的图片为"<<NImages-successes<<"帧...\n\n";
    cout<<"*********************************************\n\n";
fout<<"*********************************************\n\n";


    cout<<"按任意键开始计算摄像机内参数...\n\n";
fout<<"按任意键开始计算摄像机内参数...\n\n";




    /*CvCapture* capture1;
    capture1 = cvCreateCameraCapture(0);*/
    IplImage * show_colie;
    show_colie = cvLoadImage("F:\\graduatelunwen\\opencvprojects\\june\\opecv_ransac_sift_cameraCalibration\\dinggao_wanchengban_sift_ransac\\left_43.jpg",1);




    CvMat * object_points2 = cvCreateMat(successes*total_per_image,3,CV_32FC1);
    CvMat * image_points2  = cvCreateMat(successes*total_per_image,2,CV_32FC1);
    CvMat * point_counts2  = cvCreateMat(successes,1,CV_32SC1);




    for(int i=0;i<successes*total_per_image;++i)
    {
        CV_MAT_ELEM(*image_points2, float,i,0)=CV_MAT_ELEM(*image_points, float,i,0);
        CV_MAT_ELEM(*image_points2, float,i,1)=CV_MAT_ELEM(*image_points, float,i,1);
        CV_MAT_ELEM(*object_points2,float,i,0)=CV_MAT_ELEM(*object_points,float,i,0);
        CV_MAT_ELEM(*object_points2,float,i,1)=CV_MAT_ELEM(*object_points,float,i,1);
        CV_MAT_ELEM(*object_points2,float,i,2)=CV_MAT_ELEM(*object_points,float,i,2);
    }


    for(int i=0;i<successes;++i)
    {
        CV_MAT_ELEM(*point_counts2,int,i,0) = CV_MAT_ELEM(*point_counts,int,i,0);
    }




    cvReleaseMat(&object_points);
    cvReleaseMat(&image_points);
    cvReleaseMat(&point_counts);




    CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0f;
    CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=1.0f;



    cvCalibrateCamera2(object_points2,image_points2,point_counts2,cvGetSize(show_colie),
    intrinsic_matrix,distortion_coeffs,rotation_vectors,translation_vectors,0);


    cout<<"摄像机内参数矩阵为:\n";
fout<<"摄像机内参数矩阵为:\n";
    cout<<CV_MAT_ELEM(*intrinsic_matrix,float,0,0)<<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,0,1)
    <<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,0,2)
    <<"\n\n";
    cout<<CV_MAT_ELEM(*intrinsic_matrix,float,1,0)<<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,1,1)
    <<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,1,2)
    <<"\n\n";
    cout<<CV_MAT_ELEM(*intrinsic_matrix,float,2,0)<<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,2,1)
    <<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,2,2)
    <<"\n\n";


fout<<CV_MAT_ELEM(*intrinsic_matrix,float,0,0)<<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,0,1)
    <<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,0,2)
    <<"\n\n";
    fout<<CV_MAT_ELEM(*intrinsic_matrix,float,1,0)<<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,1,1)
    <<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,1,2)
    <<"\n\n";
    fout<<CV_MAT_ELEM(*intrinsic_matrix,float,2,0)<<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,2,1)
    <<" "<<CV_MAT_ELEM(*intrinsic_matrix,float,2,2)
    <<"\n\n";


f_left[0]=CV_MAT_ELEM(*intrinsic_matrix,float,0,0);
f_left[1]=CV_MAT_ELEM(*intrinsic_matrix,float,1,1);




    cout<<"畸变系数矩阵为:\n";
    cout<<CV_MAT_ELEM(*distortion_coeffs,float,0,0)<<" "<<CV_MAT_ELEM(*distortion_coeffs,float,1,0)
    <<" "<<CV_MAT_ELEM(*distortion_coeffs,float,2,0)
    <<" "<<CV_MAT_ELEM(*distortion_coeffs,float,3,0)
    <<" "<<CV_MAT_ELEM(*distortion_coeffs,float,4,0)
    <<"\n\n";


fout<<"畸变系数矩阵为:\n";
    fout<<CV_MAT_ELEM(*distortion_coeffs,float,0,0)<<" "<<CV_MAT_ELEM(*distortion_coeffs,float,1,0)
    <<" "<<CV_MAT_ELEM(*distortion_coeffs,float,2,0)
    <<" "<<CV_MAT_ELEM(*distortion_coeffs,float,3,0)
    <<" "<<CV_MAT_ELEM(*distortion_coeffs,float,4,0)
    <<"\n\n";


    cvSave("Intrinsics.xml",intrinsic_matrix);
    cvSave("Distortion.xml",distortion_coeffs);


    cout<<"摄像机矩阵、畸变系数向量已经分别存储在名为Intrinsics.xml、Distortion.xml文档中\n\n";
    fout<<"摄像机矩阵、畸变系数向量已经分别存储在名为Intrinsics.xml、Distortion.xml文档中\n\n";


for(int ii = 0; ii < NImages; ++ii)
{ float tranv[3] = {0.0};
float rotv[3] = {0.0};


for ( int i = 0; i < 3; i++)
{
tranv[i] = ((float*)(translation_vectors->data.ptr+ii*translation_vectors->step))[i];
rotv[i] = ((float*)(rotation_vectors->data.ptr+rotation_vectors->step))[i];
}

cout << "第" << ii+1 << "幅图的外参数" << endl;
fout << "第" << ii+1 << "幅图的外参数" << endl;
printf("ROTATION VECTOR 旋转向量 : \n");
printf("[ %6.4f %6.4f %6.4f ] \n", rotv[0], rotv[1], rotv[2]);
printf("TRANSLATION VECTOR 平移向量: \n");
printf("[ %6.4f %6.4f %6.4f ] \n", tranv[0], tranv[1], tranv[2]);
printf("-----------------------------------------\n");


fout<<"ROTATION VECTOR 旋转向量 : \n"<<endl;
fout<< rotv[0]<<"  "<< rotv[1]<<"  "<< rotv[2]<<endl;
fout<<"TRANSLATION VECTOR 平移向量: \n"<<endl;
fout<< tranv[0]<<"  "<< tranv[1]<< "  "<<tranv[2]<<endl;
fout<<"-----------------------------------------\n"<<endl;
}
    CvMat * intrinsic=(CvMat *)cvLoad("Intrinsics.xml");
    CvMat * distortion=(CvMat *)cvLoad("Distortion.xml");

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值