- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 OpenCV 在 Python 和 C++ 中计算视频里程计的基本矩阵。我试图使两个实现中的代码完全相同。但是,我在这两个方面都得到了不同的结果。在 Python 中,它工作正常,而在 C++ 中,它显示完全错误的结果。下面是他们的代码和输出的部分示例(第一个在 Python 中,第二个在 C++ 中)
Python版本代码:
import os
import sys
import cv2
import numpy as np
import math
# Main Function
if __name__ == '__main__':
K = np.matrix([[522.4825, 0, 300.9989],
[0, 522.5723, 258.1389],
[0.0, 0.0, 1.0]])
img1 = cv2.imread(sys.argv[1] + ".jpg")
img2 = cv2.imread(sys.argv[2] + ".jpg")
# sift = cv2.SURF()
detector = cv2.FeatureDetector_create("SURF") # SURF, FAST, SIFT
descriptor = cv2.DescriptorExtractor_create("SURF") # SURF, SIFT
# kp1, des1 = sift.detectAndCompute(img1,None)
# kp2, des2 = sift.detectAndCompute(img2,None)
kp1 = detector.detect(img1)
kp2 = detector.detect(img2)
k1, des1 = descriptor.compute(img1,kp1)
k2, des2 = descriptor.compute(img2,kp2)
# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
good = []
# Apply ratio test
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append(m)
MIN_MATCH_COUNT = 10
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
F, mask = cv2.findFundamentalMat(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
else:
print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT)
matchesMask = None
print F
它的输出:
[[ -3.22706105e-07 1.12585581e-04 -2.86938406e-02]
[ -1.16307090e-04 -5.04244159e-07 5.60714444e-02]
[ 2.98839742e-02 -5.99974406e-02 1.00000000e+00]]
此处为 C++ 版本:
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace std;
int main(int argc,char *argv[]) {
//Define intrinsic matrix
cv::Mat intrinsic = (cv::Mat_<double>(3,3) << 522.4825, 0, 300.9989,
0, 522.5723, 258.1389,
0, 0, 1);
// Read input images
string jpg1 = argv[1];
jpg1.append(".jpg");
string jpg2 = argv[2];
jpg2.append(".jpg");
cv::Mat image1 = cv::imread(jpg1,0);
cv::Mat image2 = cv::imread(jpg2,0);
if (!image1.data || !image2.data)
return 0;
// Display the images
// cv::namedWindow("Image 1");
// cv::imshow("Image 1",image1);
// cv::namedWindow("Image 2");
// cv::imshow("Image 2",image2);
// pointer to the feature point detector object
cv::Ptr<cv::FeatureDetector> detector = new cv::SurfFeatureDetector();
// pointer to the feature descriptor extractor object
cv::Ptr<cv::DescriptorExtractor> extractor = new cv::SurfDescriptorExtractor();
// Detection of the SURF features
vector<cv::KeyPoint> keypoints1, keypoints2;
detector->detect(image1,keypoints1);
detector->detect(image2,keypoints2);
// Extraction of the SURF descriptors
cv::Mat descriptors1, descriptors2;
extractor->compute(image1,keypoints1,descriptors1);
extractor->compute(image2,keypoints2,descriptors2);
// Construction of the matcher
cv::BruteForceMatcher<cv::L2<float> > matcher;
vector<vector<cv::DMatch> > matches;
vector<cv::DMatch> good_matches;
matcher.knnMatch(descriptors1, descriptors2, matches, 2);
for (vector<vector<cv::DMatch> >::iterator matchIterator= matches.begin();
matchIterator!= matches.end(); ++matchIterator) {
if ((*matchIterator)[0].distance < 0.7f * (*matchIterator)[1].distance) {
good_matches.push_back((*matchIterator)[0]);
}
}
// Convert keypoints into Point2f
vector<cv::Point2f> src_pts, dst_pts;
for (vector<cv::DMatch>::iterator it= good_matches.begin();
it!= good_matches.end(); ++it)
{
// Get the position of left keypoints
float x= keypoints1[it->queryIdx].pt.x;
float y= keypoints1[it->queryIdx].pt.y;
src_pts.push_back(cv::Point2f(x,y));
// Get the position of right keypoints
x= keypoints2[it->trainIdx].pt.x;
y= keypoints2[it->trainIdx].pt.y;
dst_pts.push_back(cv::Point2f(x,y));
}
// Compute F matrix using RANSAC
cv::Mat fundemental = cv::findFundamentalMat(
cv::Mat(src_pts),cv::Mat(dst_pts), // matching points
CV_FM_RANSAC, // RANSAC method
5.0); // distance
cout << fundemental << endl;
return 0;
}
及其输出:
[-4.310057787788129e-06, 0.0002459670522815174, -0.0413520716270485;
-0.0002531048911221476, -8.423657757958228e-08, 0.0974897887347238;
0.04566865455090797, -0.1062956485414729, 1]
我找不到原因。谁能告诉我为什么?
最佳答案
由于没有人回答,我将分享我的想法。您是只检查 F 中的数字,还是以某种方式应用它并观察到不正确的结果?正如@brandon-white 已经注意到的那样,浮点精度可能是原因之一。但实际上要复杂得多。
首先想到的是 C++ OpenCV 中的 AFAIK 使用它自己的矩阵和其他数学运算例程,而在 python 中尽可能使用 numpy。也许在幕后他们使用类似的算法/实现,但您仍然可能得到数值上不同的结果,尤其是在您处理歧义(特征向量分解、SVD 等)的情况下。
此外,您正在使用 RANSAC估计 F。为了处理(理论上)任何数量的异常值,RANSAC 从您的所有关键点中随机抽取一个小样本,并尝试找到满足某些约束的对。它多次执行此操作,然后选取最佳样本来计算最终模型。所以基本上你最终会得到不同的点来估计每次运行的 F,如果你适本地播种伪随机生成例程。但是,通常单应性和基本矩阵估计器使用更智能的方法,并且在找到最能满足约束的样本之后 - 满足该模型的所有点都将用于再次重新计算矩阵。这样你应该得到更一致的结果,如果 RANSAC 参数正常,理想情况下是相同的。我不确定它是否在 OpenCV 中使用,但我猜是。
最后还有degenerate cases ,其中无法完全估计 F - 平面运动的情况,当您的所有关键点都位于一个平面上(在 3D 世界中)时,以及纯粹的旋转相机运动。既然你说你的代码在 Python 中工作,这可能不是这种情况,但仍然是一个需要考虑的问题。
因此,如果您还没有这样做 - 尝试检查您在某些数据上获得的 F 矩阵,以确保您获得的结果确实不同。在那种情况下 - 某处应该有错误(诚然我还没有仔细检查你的代码)。
此外,显示您用于 F 计算的匹配项可能对调试有用,因为这会缩小您的代码可能表现不同的位置范围。
关于python - 使用 OpenCV 的 Python 和 C++ 计算基本矩阵的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293973/
如何将十进制数字转换为mixed radix表示法? 我猜想给定每个基数数组的输入和十进制数,它应该输出每列值的数组。 最佳答案 伪代码: bases = [24, 60, 60] input = 8
我有 Table-A,其中有“x”行。 (对于这个例子有 8 行) 我通过使用游标创建了列数为“x”的Table-C。 (使其动态化;如果将更多行添加到 Table-A,则会在 Table-C 中创建
我有一个关于对象的(很可能是简单而愚蠢的)问题。我创建了实例“Person”的对象“jon”。当我打电话时 console.log(jon.name) 控制台会给我输出“jon”。到目前为止,一切都很
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: javascript function vs. ( function() { … } ()); 抱歉,如果这太基础了
我正在尝试用 Java 重新创建射弹轨迹,但是,我遇到了一些问题。我看过很多解释公式之类的视频,但他们的方程中有一个目标,而我没有。我的意思是,他们有一个范围来计算子弹的下落,但我试图弄清楚子弹最终会
(希望如此)来自一个完整的 Rust 初学者的一个简单问题。我的循环有什么问题? num 计算结果为“69”的速度相当快,但是一旦 num 设置为“69”,循环就永远不会退出。我肯定遗漏了一些明显的东
我在 id="name"的元素上应用“.length”,但它计数为 29 而不是 14。我想知道我的错误在哪里?如果有人可以让我知道,那就太好了。谢谢! var name=document.getEl
我知道这很简单,但由于某种原因我无法让它工作。我正在尝试在 Java 中创建自定义颜色,但它似乎不起作用。 import java.awt.Color; Color deepGreen = new C
我有一个大文件,其中每一行都包含一个子字符串,例如 ABC123。如果我执行 grep ABC file.txt 或 grep ABC1 file.txt 我按预期返回这些行,但如果我执行 grep
我想将以下实体映射转换为 Priority 对象。在 getter 上,当我将“Short”更改为“Priority”并遵循 this.priority 时,它会提示 'basic' 属性类型不应该是
我正在开发一个相当基本的函数,我发现很难弄清楚为什么我会得到我的输出。 def mystery(n): print(n) if n < 4: my
我正在尝试对 WordPress 安装的新闻部分实现同位素过滤。我是 JavaScript/jQuery 的新手,正在尝试随时随地学习。我首先使用 Filters section of the Iso
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在另一个实体类中引用一个实体并收到此错误。下面是示例代码。我在 persistence.xml 中也有这些类。 是什么导致了这个问题?我正在使用 Spring 数据 JPA 和 Hibernate。
我正在解析 HTML 并重新格式化图像以使其更好地适应。由于某种原因,当我有多个图像需要解析时,我会超出范围,而且我一生都无法弄清楚为什么。 当 imgArray.count >1 时,我将使用带有递
我是 SQL 新手,正在尝试创建一个基本的子查询。我需要找出经理的平均年龄和实习生的平均年龄之间的差异。 标题为一栏 - 经理或实习生年龄是一列,全部在同一个表中。 我会使用两个子查询来做类似的事情:
我习惯了 csh,所以不得不使用 bash 有点烦人。这段代码有什么问题? if[$time > 0300] && [$time 和 300 && time < 900 )) then mod
我建立了这个页面:http://excelwrestling.com/poola.php即将到来的双重锦标赛。我的大部分数据都是从我的 mySQL 数据库中提取的,现在只有一些示例数据。 我希望链接选
是否有任何原因导致以下内容不起作用: for (i=0;i < someArray.length;i++) { if (someArray[i].indexOf("something") !=
我现在正在学习 Javascript,有一个问题一直困扰着我! 因此,我在这里所需要做的就是在此输入框中键入颜色,单击按钮并将标题更改为键入的颜色(仅当键入的颜色位于变量中指定的数组中时)。 我的代码
我是一名优秀的程序员,十分优秀!