目标双站定位仿真C++代码
point-position2 初步完善版。
不再使用eigen库,行列式直接计算得出结果。判断共面异面分别处理。
先提取双站获得图像的匹配特征点,由双站位置信息解析目标位置。
// point-position2.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/nonfree/features2d.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/legacy/legacy.hpp"
#include<math.h>
using namespace cv; int main( int argc, char** argv )
{ Mat img_1 = imread("book_in_scene.png");
Mat img_2 = imread("book2.png"); if( !img_1.data || !img_2.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -; } //-- Step 1: Detect the keypoints using SURF Detector
int minHessian = ; SiftFeatureDetector detector( minHessian );
//SurfFeatureDetector detector( minHessian ); vector<KeyPoint> keypoints_1, keypoints_2; detector.detect( img_1, keypoints_1 );
detector.detect( img_2, keypoints_2 ); //-- Step 2: Calculate descriptors (feature vectors)
SiftDescriptorExtractor extractor;
//SurfDescriptorExtractor extractor; Mat descriptors_1, descriptors_2; extractor.compute( img_1, keypoints_1, descriptors_1 );
extractor.compute( img_2, keypoints_2, descriptors_2 ); //-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches ); double max_dist = ; double min_dist = ; //-- Quick calculation of max and min distances between keypoints
for( int i = ; i < descriptors_1.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
} //printf("-- Max dist : %f \n", max_dist );
//printf("-- Min dist : %f \n", min_dist ); //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist )
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches; for( int i = ; i < descriptors_1.rows; i++ )
{ if( matches[i].distance < *min_dist )
{ good_matches.push_back( matches[i]); }
} //-- Draw only "good" matches
Mat img_matches;
drawMatches( img_1, keypoints_1, img_2, keypoints_2,
good_matches, img_matches
); //-- Show detected matches
//imshow( "Good Matches", img_matches );
//imwrite("Lena_match_surf.jpg",img_matches);
//imwrite("Lena_match_sift.jpg",img_matches);
//good_matches[i].queryIdx保存着第一张图片匹配点的序号,keypoints_1[good_matches[i].queryIdx].pt.x 为该序号对应的点的x坐标。y坐标同理
//good_matches[i].trainIdx保存着第二张图片匹配点的序号,keypoints_2[good_matches[i].trainIdx].pt.x 为为该序号对应的点的x坐标。y坐标同理
printf( "--Keypoint 1:%f,%f: %d -- Keypoint 2:%f,%f: %d \n",
keypoints_1[good_matches[].queryIdx].pt.x,keypoints_1[good_matches[].queryIdx].pt.y,good_matches[].queryIdx,
keypoints_2[good_matches[].trainIdx].pt.x,keypoints_2[good_matches[].trainIdx].pt.y,good_matches[].trainIdx );
/*_______________________________________________________________________________________________________________________________*/ double x_inImage1,y_inImage1,x_inImage2,y_inImage2,y,X,Y,alpha,gamma;//像面坐标(x,y)和图像尺寸(X,Y)以及成像视场角(alpha,gamma)
double x1,y1,z1,x2,y2,z2;//双站坐标
double alpha1,gamma1;//双站俯仰角和偏转角
double alpha2,gamma2; //赋予初始值
alpha1=;
gamma1=;
alpha2=;
gamma2=; X=;
Y=;
double FOVx=;
double FOVy=FOVx*Y/X;
x1=,y1=,z1=;
x2=,y2=,z2=; /* //测角偏差补偿
x_inImage1=keypoints_1[good_matches[0].queryIdx].pt.x;//目标点坐标由匹配所得
y_inImage1=keypoints_1[good_matches[0].queryIdx].pt.y;
x_inImage2=keypoints_2[good_matches[0].queryIdx].pt.x;
y_inImage2=keypoints_2[good_matches[0].queryIdx].pt.y; double deviation_alpha1=(x_inImage1-X/2)/X*FOVx;
double deviation_alpha2=(x_inImage2-X/2)/X*FOVx;
double deviation_gamma1=(y_inImage1-Y/2)/X*FOVy;
double deviation_gamma2=(y_inImage2-Y/2)/X*FOVy; alpha1=alpha1+deviation_alpha1;
alpha2=alpha2+deviation_alpha2;
gamma1=gamma1+deviation_gamma1;
gamma2=gamma2+deviation_gamma2;
*/
//开始计算
double pi=*(atan(1.0/))-*atan(1.0/);//精确定义圆周率
std::cout<<"pi为:"<<pi<<std::endl;
alpha1=alpha1*pi/;//角度弧度转换
gamma1=gamma1*pi/;
alpha2=alpha2*pi/;
gamma2=gamma2*pi/; // std::cout<<"cos(alpha1)为:"<<cos(alpha1)<<std::endl;
// std::cout<<"cos(gamma1)为:"<<cos(gamma1)<<std::endl;
double m1=(cos(alpha1))*(cos(gamma1));
double n1=(sin(alpha1))*(cos(gamma1));
double p1=sin(gamma1);
double m2=(cos(alpha2))*(cos(gamma2));
double n2=(sin(alpha2))*(cos(gamma2));
double p2=sin(gamma2); std::cout<<"方向向量1为:"<<m1<<","<<n1<<","<<p1<<std::endl;
std::cout<<"方向向量2为:"<<m2<<","<<n2<<","<<p2<<std::endl; double coplane;//共面判断
coplane=(x2-x1)*(n1*p2-n2*p1)-(y2-y1)*(m1*p2-m2*p1)+(z2-z1)*(m1*n2-m2*n1);//coplane=0共面
if(coplane)
{
//计算公垂线方向向量A1、B1、C1
double A1=n1*p2-n2*p1;
double B1=p1*m2-p2*m1;
double C1=m1*n2-m2*n1;
//
double A2=n2*C1-p2*B1;
double B2=p2*A1-m2*C1;
double C2=m2*B1-n2*A1; double A3=n1*C1-p1*B1;
double B3=p1*A1-m1*C1;
double C3=m1*B1-n1*A1; double delta1=n1*(B1*C2-B2*C1)+m1*(A1*C2-A2*C1);
double delta2=n2*(B1*C3-B3*C1)+m2*(A1*C3-A3*C1);
double D1=A2*(x2-x1)+B2*(y2-y1)+C2*(z2-z1);
double D2=A3*(x1-x2)+B3*(y1-y2)+C3*(z1-z2); double Xg,Yg,Zg,Xh,Yh,Zh,Xtarget,Ytarget,Ztarget;//两直线垂足G和H点坐标,目标点在其中点位置。
Xg=x1-(D1*m1*C1)/delta1;
Yg=y1-(D1*n1*C1)/delta1;
Zg=z1+D1*(A1*m1+B1*n1)/delta1;
Xh=x2-(D2*m2*C1)/delta2;
Yh=y2-(D2*n2*C1)/delta2;
Zh=z2+D2*(A1*m2+B1*n2)/delta2; Xtarget=(Xg+Xh)/;
Ytarget=(Yg+Yh)/;
Ztarget=(Zg+Zh)/; std::cout<<"目标坐标为:"<<Xtarget<<","<<Ytarget<<","<<Ztarget<<std::endl<<std::endl;
}
else//两线共面且相交,引入参数t
{
double t;
t=(p2*(y1-y2)+n2*(z2-z1))/(n2*p1-p2*n1);
double Xtarget,Ytarget,Ztarget;
Xtarget=x1+m1*t;
Ytarget=y1+n1*t;
Ztarget=z1+p1*t;
std::cout<<"目标坐标为:"<<Xtarget<<","<<Ytarget<<","<<Ztarget<<std::endl<<std::endl;
}
getchar();
//waitKey(0);
return ;
}
目标双站定位仿真C++代码的更多相关文章
- iOS定位到崩溃代码行数
不知道大家是不是在代码调试过程中经常遇到项目崩溃的情况: 比如: 数组越界: 没有实现方法选择器: 野指针: 还有很多很多情况.......昨天学到了一种可以直接定位到崩溃代码行数的一个命令,记录一下 ...
- dump文件定位程序崩溃代码行
1.dump文件 2.程序对应的pdb 步骤一:安装windbg 步骤二:通过windbg打开crash dump文件 步骤三:设置pdb文件路径,即符号表路径 步骤四:运行命令!analyze -v ...
- linux设备驱动程序第四部分:从如何定位oops对代码的调试方法,驱动线
在一个我们谈到了如何编写一个简单的字符设备驱动程序,我们不是神,编写肯定会失败的代码,在这个过程中,我们需要继续写代码调试.在普通c应用.我们经常使用printf输出信息.或者使用gdb要调试程序,然 ...
- 使用MAP文件快速定位程序崩溃代码行 (转)
使用MAP文件快速定位程序崩溃代码行 =========================================================== 作者: lzmfeng(http://lz ...
- uboot搬移部分和重定位部分的代码分析
来看一下搬移部分和重定位部分的代码: relocate: /* 把U-BOOT重新定位到RAM*/ //r0=0; adr r0, _start /* r0是代码的当前位置*/ ld ...
- linux设备驱动第四篇:从如何定位oops的代码行谈驱动调试方法
上一篇我们大概聊了如何写一个简单的字符设备驱动,我们不是神,写代码肯定会出现问题,我们需要在编写代码的过程中不断调试.在普通的c应用程序中,我们经常使用printf来输出信息,或者使用gdb来调试程序 ...
- matlab中双站异面直线法定位目标
calc.m %% 参数信息初始化 [x1,y1,z1]=deal(); [x2,y2,z2]=deal(,,); m1=/; n1=/; p1=^(/)/; m2=; n2=-^(/)/; p2=^ ...
- 目标检测之Faster-RCNN的pytorch代码详解(模型训练篇)
本文所用代码gayhub的地址:https://github.com/chenyuntc/simple-faster-rcnn-pytorch (非本人所写,博文只是解释代码) 好长时间没有发博客了 ...
- VS快速定位文件、代码插件——DPack
之前用Myeclipse开发一个Java项目,发现其中“Open Resource”(Ctrl+Shirft+R)的功能比较好用,回到.Net后就找了找VS相应的功能,试了几个后觉得Dpack比较好用 ...
随机推荐
- MySQL 主从同步架构中你不知道的“坑”
以下操作征对指定不同步库 binlog-format=ROW模式 1 查看主从的binlog模式 mysql> show slave status\G ********************* ...
- LNMP完整安装教程
软件下载地址 https://lnmp.org/install.html 本环境与外网生产环境一致(MySQL 5.6 + PHP 7.1 + CentOS + Nginx 1.12 ) 上图红色 ...
- 缺少库libresolv
"_dns_free_resource_record", referenced from: -[xxxx processRecord:length:] in xxx.a(xxx.o ...
- ajax传输数组类型的参数
$.ajax({ type:"post", traditional: true, data:{arr:arr}, url:"${pageContext.request.c ...
- 第四小节之Java 集合类
Java的集合类就像一个容器,专门用来存储Java类的对象.这些类可以存储任意类型的对象,并且长度可变,统称为集合,这些类位于java.util包中,数组也可以保存多个对象,但在某些情况下无法确定到底 ...
- SpringBoot02——A Simple SpringBoot Project&Hot Deployment
1.简单的Controller映射 1.新建一个controller包,包一定在启动器的下一层级 2.可以在application.properties中进行调整端口和context-path参数 s ...
- springboot 关于log4j日志配置
自动加载配置文件:(1)如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解.log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找log ...
- SpringMVC @RequestParam
案例来说明 @RequestMapping("user/add") public String add(@RequestParam("name") String ...
- 【NOIP2016提高A组集训第3场10.31】高维宇宙
题解 分析 因为只有奇数和偶数配对才有可能得出质数, 暴力求出每一对\(a_i+a_j\)为质数,将其中的奇数想偶数连一条边. 二分图匹配,匈牙利算法. #include <cmath> ...
- 算法——得到数据流中前K大的数
用优先队列 public PriorityQueue<Integer> kthLargest(int k, int[]a) { PriorityQueue<Integer> q ...