【OpenCV学习】极几何约束 .

简介: 2.Ransac是一种非常简单的算法 用于在一群样本中去掉噪声样本,得到有效的样本采用随机抽样验证的方法,以下节选自wikipedia,选有用的贴了过来RANSACRANSAC is an abbreviation for "RANdom SAmple Consensus".




2.Ransac
是一种非常简单的算法

用于在一群样本中去掉噪声样本,得到有效的样本
采用随机抽样验证的方法,以下节选自wikipedia,选有用的贴了过来

RANSAC
RANSAC is an abbreviation for "RANdom SAmple Consensus". It is an algorithm to estimate parameters

of a mathematical model from a set of observed data which contains outliers . The algorithm was first

published by Fischler and Bolles in 1981.
A basic assumption is that the data consists of "inliers", i.e., data
points which can be explained by some set of model parameters, and
"outliers" which are data points that do not fit the model. In addition
to this, the data points can be subject to noise. The outliers can
come, e.g., from extreme values of the noise or from erroneous
measurements or incorrect hypotheses about the interpretation of data.
RANSAC also assumes that, given a (usually small) set of inliers, there
exists a procedure which can estimate the parameters of a model that
optimally explains or fits this data.

Example
A
simple example is fitting of a 2D line to set of observations. Assuming
that this set contains both inliers, i.e., points which approximately
can be fitted to a line, and outliers, points which cannot be fitted to
this line, a simple least squares method for line fitting will in
general produce a line with a bad fit to the inliers. The reason is
that it is optimally fitted to all points, including the outliers.
RANSAC, on the other hand, can produce a model which is only computed
from the inliers, provided that the probability of choosing only
inliers in the selection of data points is sufficiently high. There is
no guarantee for this situation, however, and there are a number of
algorithm parameters which must be carefully chosen to keep the level
of probability reasonably high.


Overview
The
input to the RANSAC algorithm is a set of observed data values, a
parameterized model which can explain or be fitted to the observations,
and some confidence parameters.
RANSAC achieves its goal by
iteratively selecting a random subset of the original data points.
These points are hypothetical inliers and this hypothesis is then
tested as follows. A model is fitted to the hypothetical inliers, that
is, all free parameters of the model are reconstructed from the point
set. All other data points are then tested against the fitted model,
that is, for every point of the remaining set, the algorithm determines
how well the point fits to the estimated model. If it fits well, that
point is also considered as a hypothetical inlier. If sufficiently many
points have been classified as hypothetical inliers relative to the
estimated model, then we have a model which is reasonably good.
However, it has only been estimated from the initial set of
hypothetical inliers, so we reestimate the model from the entire set of
point's hypothetical inliers. At the same time, we also estimate the
error of the inliers relative to the model.
This procedure is then
repeated a fixed number of times, each time producing either a model
which is rejected because too few points are classified as inliers or a
refined model together with a corresponding error measure. In the
latter case, we keep the refined model if its error is lower than the
last saved model.

Algorithm
The generic RANSAC algorithm works as follows:

input:
data - a set of observed data points
model - a model that can be fitted to data points
n - the minimum number of data values required to fit the model
k - the maximum number of iterations allowed in the algorithm
t - a threshold value for determining when a data point fits a model
d - the number of close data values required to assert that a model fits well to data
output:
bestfit - model parameters which best fit the data (or nil if no good model is found)
iterations := 0
bestfit := nil
besterr := infinity
while iterations <> d
(this implies that we may have found a good model now test
how good it is)
bettermodel := model parameters fitted to all points in maybeinliers and alsoinliers
thiserr := a measure of how well model fits these points
if thiserr <>
increment iterations
return bestfit


While
the parameter values of t and d have to be calculated from the
individual requirements it can be experimentally determined. The
interesting parameter of the RANSAC algorithm is k.
To calculate the
parameter k given the known probability w of a good data value, the
probability z of seeing only bad data values is used:
which leads to
To
gain additional confidence, the standard deviation or multiples thereof
can be added to k. The standard deviation of k is defined as
A
common case is that w is not well known beforehand, but some rough
value can be given. If n data values are given, the probability of
success is wn.


Advantages and disadvantages
An advantage
of RANSAC is its ability to do robust estimation of the model
parameters, i.e., it can estimate the parameters with a high degree of
accuracy even when outliers are present in the data set. A disadvantage
of RANSAC is that there is no upper bound on the time it takes to
compute these parameters. If an upper time bound is used, the solution
obtained may not be the most optimal one.
RANSAC can only estimate
one model for a particular data set. As for any one-model approach when
more two (or more) models exist, RANSAC may fail to find either one.

Applications
The
RANSAC algorithm is often used in computer vision, e.g., to
simultaneously solve the correspondence problem and estimate the
fundamental matrix related to a pair of stereo cameras.

References
M.
A. Fischler and R. C. Bolles (June 1981). "Random Sample Consensus: A
Paradigm for Model Fitting with Applications to Image Analysis and
Automated Cartography". Comm. of the ACM 24: 381--395.
doi:10.1145/358669.358692.
David A. Forsyth and Jean Ponce (2003). Computer Vision, a modern approach. Prentice Hall. ISBN ISBN 0-13-085198-1.
Richard
Hartley and Andrew Zisserman (2003). Multiple View Geometry in Computer
Vision, 2nd edition, Cambridge University Press.

3.基础矩阵的概念:
基础矩阵把左边图像的一个点的图像坐标与它右边图像中的对应点的图像联系起来,他是一个3x3的退化矩阵,描述了两个立体图像对的外极限几何关系,其计算依赖于在两个图像中相对应的一组点。

#include <iostream>
#include <cv.h>
#include <highgui.h>

//------------
各種外部変数 ----------//
double first[12][2] =
{
{488.362, 169.911},
{449.488, 174.44},
{408.565, 179.669},
{364.512, 184.56},
{491.483, 122.366},
{451.512, 126.56},
{409.502, 130.342},
{365.5, 134},
{494.335, 74.544},
{453.5, 76.5},
{411.646, 79.5901},
{366.498, 81.6577}
};

double second[12][2] =
{
{526.605, 213.332},
{470.485, 207.632},
{417.5, 201},
{367.485, 195.632},
{530.673, 156.417},
{473.749, 151.39},
{419.503, 146.656},
{368.669, 142.565},
{534.632, 97.5152},
{475.84, 94.6777},
{421.16, 90.3223},
{368.5, 87.5}
};

//----
支持功能 ---//
double GetYCoord(double x, double a,double b,double c)
{
return -(a*x+c)/b;
}


int main(int argc,char *argv[])
{
CvMat *firstM = cvCreateMat(12,2,CV_64FC1);
cvSetData(firstM,first,firstM->step);

CvMat *secondM = cvCreateMat(12,2,CV_64FC1);
cvSetData(secondM,second,secondM->step);

CvMat *FMat= cvCreateMat(3,3,CV_64FC1);

if(cvFindFundamentalMat(firstM,secondM,FMat,CV_FM_RANSAC,1.00,0.99) == 0){ //
获取基础矩阵F
std::cerr << "Can't Get F Mat/n";
return -1;
}


CvMat *lines = cvCreateMat(12,3,CV_64FC1);
cvComputeCorrespondEpilines(firstM,1,FMat,lines); //
绘制外极线


IplImage *imgB = cvLoadImage( "second.png", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
IplImage *imgA = cvLoadImage( "first.png", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(imgB == NULL || imgA == NULL){
std::cout<<"Can't Load Image ./n";
return -1;
}


//
绘制外极线
cvLine(imgB,
cvPoint( //
始点
0, // x
cvRound(// y
GetYCoord(0,CV_MAT_ELEM(*lines,double,11,0),
CV_MAT_ELEM(*lines,double,11,1),CV_MAT_ELEM(*lines,double,11,2)))
),
cvPoint( //
終点
imgB->width,// x
cvRound( // y
GetYCoord(imgB->width,CV_MAT_ELEM(*lines,double,11,0),
CV_MAT_ELEM(*lines,double,11,1),
CV_MAT_ELEM(*lines,double,11,2)))
),
CV_RGB(255,0,0),5);

//
提请第三点
cvDrawRect(imgA,
cvPoint(cvRound(CV_MAT_ELEM(*firstM,double,11,0)),cvRound(CV_MAT_ELEM(*firstM,double,11,1))),
cvPoint(cvRound(CV_MAT_ELEM(*firstM,double,11,0)),cvRound(CV_MAT_ELEM(*firstM,double,11,1))),
CV_RGB(0,255,0),
8);



cvNamedWindow("second",CV_WINDOW_AUTOSIZE);
cvShowImage("second",imgB);


cvNamedWindow("first",CV_WINDOW_AUTOSIZE);
cvShowImage("first",imgA);

cvReleaseMat(&firstM);
cvReleaseMat(&secondM);
cvReleaseMat(&FMat);
cvReleaseImage( &imgA );
cvReleaseImage( &imgB);
cvReleaseMat(&lines);


cvWaitKey(0);

cvDestroyAllWindows();


return EXIT_SUCCESS;
}




结果:



               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

相关实践学习
基于Hologres轻量实时的高性能OLAP分析
本教程基于GitHub Archive公开数据集,通过DataWorks将GitHub中的项⽬、行为等20多种事件类型数据实时采集至Hologres进行分析,同时使用DataV内置模板,快速搭建实时可视化数据大屏,从开发者、项⽬、编程语⾔等多个维度了解GitHub实时数据变化情况。
阿里云实时数仓实战 - 用户行为数仓搭建
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求:熟练掌握 SQL 语法熟悉 Linux 命令,对 Hadoop 大数据体系有一定的了解 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
目录
相关文章
|
9天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23436 10
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
13天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
4582 15
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
14天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
5490 13
|
1月前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
24400 65
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
|
2天前
|
前端开发 API 内存技术
对比claude code等编程cli工具与deepseek v4的适配情况
DeepSeek V4发布后,多家编程工具因未适配其强制要求的`reasoning_content`字段而报错。本文对比Claude Code、GitHub Copilot、Langcli、OpenCode及DeepSeek-TUI等主流工具的兼容性:Claude Code需按官方方式配置;Langcli表现最佳,开箱即用且无报错;Copilot与OpenCode暂未修复问题;DeepSeek-TUI尚处早期阶段。
659 2
对比claude code等编程cli工具与deepseek v4的适配情况