python还是c++处理大型矩阵_用OpenCV用Python和C++计算基本矩阵的不同结果

我用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

它的输出:

^{pr2}$

C++版本:#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

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

//Define intrinsic matrix

cv::Mat intrinsic = (cv::Mat_(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<:featuredetector> detector = new cv::SurfFeatureDetector();

// pointer to the feature descriptor extractor object

cv::Ptr<:descriptorextractor> extractor = new cv::SurfDescriptorExtractor();

// Detection of the SURF features

vector<: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<:l2> > matcher;

vector > matches;

vector<:dmatch> good_matches;

matcher.knnMatch(descriptors1, descriptors2, matches, 2);

for (vector >::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<:point2f> src_pts, dst_pts;

for (vector<: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]

我找不到原因。

谁能告诉我为什么吗?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值