2021-02-13

https://opencv.org/releases/

http://dl.ipaycenter.cn/csdn/down1.aspx?sign=9EC8243399689788E63778504806A8F6&cdk=A7053333564D5E8B90F3AF98B7ACACBE&fileid=E18209DA8A3DB6AEEBE956A2F580F849&nstr=637544598458877158

http://dl.ipaycenter.cn/csdn/down1.aspx?sign=84050D075241C9BF60BB62FE77F71029&cdk=1FB03A4C6503D5C49C7AF2A4A5084DB9&fileid=F76A60AC0CB721B973F23E8CBEC119D9&nstr=637544598730317635
https://lsy.cowtransfer.com/s/c6bfe7645c944d
您好,文件已下载并上传完毕,请用浏览器打开链接下载
阅读代码文件, 了解矩阵和向量的基本操作与数学运算,包括矩阵与向量的创建,元素的访问,矩阵向量的乘法,矩阵的奇异值分解等操作。后面的任务中需要用到这些基本的操作。

代码解析:

// Created by caoqi on 2018/8/30.
#include <iostream>
#include <math/matrix_svd.h> //前两个是标准库文件
#include "math/matrix.h"
#include "math/vector.h"  //后两个是自定义头文件

int main(int argc, char *argv[])
{
  矩阵  
    /*构建一个维度为4x5的矩阵,数据类型为double的矩阵*/
    math::Matrix<double, 4, 5> A;  //这里用了头文件math/matrix的头文件,构建一个4*5的矩阵

    /*矩阵元素的设置和访问*/
    //在构建了矩阵A之后,使用for循环进行赋值和读取
    int id=0;
    for(int i=0; i< A.rows; i++){
        for(int j=0; j< A.cols; j++){
            A(i,j) = ++id;
            std::cout<<A(i, j)<<" "; 
        }
        std::cout<<std::endl;  //每输出一个rows后进行回车
    }
    std::cout<<std::endl;
      /*执行之后结果为:
       1 2 3 4 5 
       6 7 8 9 10 
      11 12 13 14 15 
      16 17 18 19 20 */

    /*取矩阵的列元素*/  
    //这里调用了"math/vector.h" 的头文件, vector 是关于向量的
    
    math::Vector<double, 4> col4 = A.col(4); // 取第5列col(4)元素
    std::cout<<"col4: "<<col4<<std::endl;

    /*取矩阵的行元素*/
    math::Vector<double, 5> row2 = A.row(2); // 取第3行row(2)元素
    std::cout<<"row2: "<<row2<<std::endl;

//   向量  /
    /*向量的创建*/
    math::Vector<double, 5> v1;
    for(int i=0; i<v1.dim; i++){
        v1[i] = i;
    }

    std::cout<<"v1: ";
    for(int i=0; i<v1.dim; i++){
        std::cout<<v1[i]<<" ";
    }
    std::cout<<std::endl<<std::endl;

    //奇异值分解
    math::Matrix<double, 4, 5>U;
    math::Matrix<double, 5, 5> S, V;
    math::matrix_svd<double, 4, 5> (A,&U, &S, &V);
    std::cout<<"U: "<<U<<std::endl;
    std::cout<<"S: "<<S<<std::endl;
    std::cout<<"V: "<<V<<std::endl;

    return 0;
}

参考slides中针孔相机模型部分,完成examples/task1/task1-2_test_camera.cc中相机的3个类函数

//
// Created by caoqi on 2018/9/5.
//
#include <iostream>
#include "math/vector.h"
class Camera{

public:

    // constructor
    Camera(){

        // 采用归一化坐标,不考虑图像尺寸
        c_[0]=c_[1] = 0.0;
    }

    // 相机投影过程
    math::Vec2d projection(math::Vec3d const & p3d){

        math::Vec2d p;
        /** TODO HERE
         *
         */
        return p;

        /**  Reference
        // 世界坐标系到相机坐标系
        double xc = R_[0] * p3d[0] + R_[1] * p3d[1] + R_[2]* p3d[2] + t_[0];
        double yc = R_[3] * p3d[0] + R_[4] * p3d[1] + R_[5]* p3d[2] + t_[1];
        double zc = R_[6] * p3d[0] + R_[7] * p3d[1] + R_[8]* p3d[2] + t_[2];

        // 相机坐标系到像平面
        double x = xc/zc;
        double y = yc/zc;

        // 径向畸变过程
        double r2 = x*x + y*y;
        double distort_ratio = 1+ dist_[0]* r2+ dist_[1]*r2*r2;

        // 图像坐标系到屏幕坐标系
        math::Vec2d p;
        p[0] = f_* distort_ratio*x + c_[0];
        p[1] = f_* distort_ratio*y + c_[1];

        return p;

         **/

    }

    // 相机在世界坐标中的位置 -R^T*t
    math::Vec3d pos_in_world(){

        math::Vec3d pos;
        pos[0] = R_[0]* t_[0] + R_[3]* t_[1] + R_[6]* t_[2];
        pos[1] = R_[1]* t_[0] + R_[4]* t_[1] + R_[7]* t_[2];
        pos[2] = R_[2]* t_[0] + R_[5]* t_[1] + R_[8]* t_[2];
        return -pos;
    }

    // 相机在世界坐标中的方向
    math::Vec3d dir_in_world(){

        math::Vec3d  dir (R_[6], R_[7],R_[8]);
        return dir;
    }
public:

    // 焦距f
    double f_;

    // 径向畸变系数k1, k2
    double dist_[2];

    // 中心点坐标u0, v0
    double c_[2];

    // 旋转矩阵
    /*
     * [ R_[0], R_[1], R_[2] ]
     * [ R_[3], R_[4], R_[5] ]
     * [ R_[6], R_[7], R_[8] ]
     */
    double R_[9];

    // 平移向量
    double t_[3];
};

int main(int argc, char* argv[]){


    Camera cam;

    //焦距
    cam.f_ = 0.920227;

    // 径向畸变系数
    cam.dist_[0] = -0.106599; cam.dist_[1] = 0.104385;

    // 平移向量
    cam.t_[0] = 0.0814358; cam.t_[1] =  0.937498;   cam.t_[2] = -0.0887441;

    // 旋转矩阵
    cam.R_[0] = 0.999796 ; cam.R_[1] = -0.0127375;  cam.R_[2] =  0.0156807;
    cam.R_[3] = 0.0128557; cam.R_[4] =  0.999894 ;  cam.R_[5] = -0.0073718;
    cam.R_[6] = -0.0155846; cam.R_[7] = 0.00757181; cam.R_[8] = 0.999854;

    // 三维点坐标
    math::Vec3d p3d ={1.36939, -1.17123, 7.04869};

    /*计算相机的投影点*/
    math::Vec2d p2d = cam.projection(p3d);
    std::cout<<"projection coord:\n "<<p2d<<std::endl;
    std::cout<<"result should be:\n 0.208188 -0.035398\n\n";

    /*计算相机在世界坐标系中的位置*/
    math::Vec3d pos = cam.pos_in_world();
    std::cout<<"cam position in world is:\n "<< pos<<std::endl;
    std::cout<<"result should be: \n -0.0948544 -0.935689 0.0943652\n\n";

    /*计算相机在世界坐标系中的方向*/
    math::Vec3d dir = cam.dir_in_world();
    std::cout<<"cam direction in world is:\n "<<dir<<std::endl;
    std::cout<<"result should be: \n -0.0155846 0.00757181 0.999854\n";
}
  1. *int main(int argc, char argv[])
    argc为整数,argv指针的指针(argv是一个指针数字)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值