/***********************************************
摄像机模型模拟程序
对模拟的参数进行仿真运算:
仿真的三维物点有12个点,对其进行摄像机模型模拟后产生图像像素点坐标;
这些坐标值保存在the result.txt文档中。
************************************************/
#include <iostream.h>
#include <math.h>
#include <fstream>
#define pi 3.1415926
using namespace std;
/*************************************************
求解转换矩阵函数
参数:M1——3x4矩阵,由摄像机内部参数决定
M2——4x4矩阵,由摄像机外部参数决定
结果: M——3x4矩阵,摄像机的转换矩阵
调用形式:transf(float matrix[3][4],float matrix[4][4],float matrix[3][4]);
**************************************************/
void transf (float a[3][4],float b[4][4],float ab[3][4])
{
int i,j,z;
for (i=0;i<3;i++)
for (j=0;j<4;j++)
for (z=0;z<4;z++)
ab[i][z]=ab[i][z]+a[i][j]*b[j][z];/*fulfill multiplication of matrix*/
}
/*************************************************
求解图像像素点坐标函数
参数:M——3x4矩阵,摄像机的转换矩阵
W——4x12矩阵,仿真三维物点的空间坐标
结果:pixel——3x12矩阵,经过摄像机模型得到的图像像素点坐标
调用形式:model(float matrix[3][4],float matrix[4][12],float matrix[3][12]);
**************************************************/
void model (float a[3][4],float b[4][12],float ab[3][12])
{
int i,j,z;
for (i=0;i<3;i++)
for (j=0;j<4;j++)
for (z=0;z<12;z++)
ab[i][z]=ab[i][z]+a[i][j]*b[j][z]; /*fulfill multiplication of matrix*/
}
/*main 主函数开始*/
void main()
{
/*依次开始定义需要的参数矩阵,p数组中存放的是输入参数(旋转矩阵和平移矩阵的参数)*/
float p[1][6]={0,pi/3,0,200,100,-500};
float M1[3][4]={0.1134,0,640,0,
0,0.1134,512,0,
0,0,1,0};
float M2[4][4]={{0},{0},{0},{0,0,0,1}};
M2[0][0]=cos(p[0][0])*cos(p[0][2])-sin(p[0][0])*sin(p[0][1])*sin(p[0][2]);
M2[0][1]=cos(p[0][1])*sin(p[0][2]);
M2[0][2]=sin(p[0][0])*cos(p[0][2])+cos(p[0][0])*sin(p[0][1])*sin(p[0][2]);
M2[0][3]=p[0][3];
M2[1][0]=-cos(p[0][0])*sin(p[0][2])-sin(p[0][0])*sin(p[0][1])*cos(p[0][2]);
M2[1][1]=cos(p[0][1])*cos(p[0][2]);
M2[1][2]=-sin(p[0][1])*sin(p[0][2])+cos(p[0][0])*sin(p[0][1])*cos(p[0][2]);
M2[1][3]=p[0][4];
M2[2][0]=-sin(p[0][0])*cos(p[0][1]);
M2[2][1]=-sin(p[0][1]);
M2[2][2]=cos(p[0][0])*cos(p[0][1]);
M2[2][3]=p[0][5];
float M[3][4]={0};
int i,j;
float W[4][12]={0,300,300,0,0,300,300,0,0,300,300,0,
0,0,400,400,0,0,400,400,0,400,0,400,
0,0,0,0,500,500,500,500,-500,-500,-500,-500,
1,1,1,1,1,1,1,1,1,1,1,1};
float pixel[3][12]={0};
/*调用求解转换矩阵函数*/
transf (M1,M2,M);
/*调用求解图像像素点坐标函数*/
model (M,W,pixel);
/*利用输入输出流在当前文件夹里生成存储结果数据的文档,并将结果存放在该“the result”文档中*/
ofstream outf("the result.txt");
outf<<"the transform matrix M is:"<<endl;
for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
{
outf<<M[i][j]<<'\0';
}
outf<<endl;
}
outf<<endl<<"the matrix pixel is:"<<endl;
for (i=0;i<3;i++)
{
for (j=0;j<12;j++)
{
outf<<pixel[i][j]<<'\0';
}
outf<<endl;
}
cout<<"the result is in the front folder named the result.txt"<<endl;
}
评论9