Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用OpenCV计算基本矩阵-我做错了什么_Python_Opencv_Camera Calibration_3d Reconstruction - Fatal编程技术网

Python 使用OpenCV计算基本矩阵-我做错了什么

Python 使用OpenCV计算基本矩阵-我做错了什么,python,opencv,camera-calibration,3d-reconstruction,Python,Opencv,Camera Calibration,3d Reconstruction,我目前正试图计算3个摄像头的基本矩阵,基于我用作“基本摄像头”的第四个摄像头。我正在使用OpenCV计算基本矩阵 尽管我的观点似乎是正确的,但当我重建相机的姿势时,它没有任何意义。例如,两个输出平移向量是(0,0,-1)。 我怀疑我在某种程度上搞砸了内部约束的实施,但不知道怎么做 我不确定我做错了什么,希望有人能帮助我:) 以下是我的数据的一些标准化采样点: 基础摄像机: 6.899999999999999467e-01 4.066666666666666347e-02 6.8999999

我目前正试图计算3个摄像头的基本矩阵,基于我用作“基本摄像头”的第四个摄像头。我正在使用OpenCV计算基本矩阵

尽管我的观点似乎是正确的,但当我重建相机的姿势时,它没有任何意义。例如,两个输出平移向量是(0,0,-1)。 我怀疑我在某种程度上搞砸了内部约束的实施,但不知道怎么做

我不确定我做错了什么,希望有人能帮助我:)

以下是我的数据的一些标准化采样点:

基础摄像机:

6.899999999999999467e-01 4.066666666666666347e-02
6.899999999999999467e-01 4.066666666666666347e-02
6.913333333333333552e-01 4.066666666666666347e-02
6.919999999999999485e-01 4.066666666666666347e-02
6.946666666666666545e-01 4.066666666666666347e-02
6.986666666666666581e-01 4.133333333333333304e-02
7.026666666666666616e-01 4.133333333333333304e-02
7.073333333333332584e-01 4.199999999999999567e-02
7.119999999999999662e-01 4.266666666666666524e-02
7.173333333333332673e-01 4.466666666666666702e-02
第一台摄像机:

3.893333333333333091e-01 6.600000000000000311e-02
3.900000000000000133e-01 6.600000000000000311e-02
3.906666666666666621e-01 6.533333333333332660e-02
3.913333333333333108e-01 6.466666666666666397e-02
3.939999999999999614e-01 6.333333333333332482e-02
3.979999999999999649e-01 6.066666666666666735e-02
4.019999999999999685e-01 5.799999999999999600e-02
4.066666666666666763e-01 5.600000000000000117e-02
4.119999999999999774e-01 5.333333333333332982e-02
4.186666666666666314e-01 5.133333333333333498e-02
摄像机矩阵:

8.830992819912107734e+02 0.000000000000000000e+00 8.279597017259351333e+02
0.000000000000000000e+00 8.678798953432041117e+02 5.529431146654350187e+02
0.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00

我正在处理你的数据。但我遇到一个问题,其中一个点在图形中丢失,然后我仔细查看您的数据

基本摄影机:点1和点2相同,可能产生矩阵秩缺陷。任何共线模式都可能导致秩缺陷。所以,选择距离足够远的点,以确保符合epi极轴约束

6.89999999467E-01 4.0666666666666347E-02

6.89999999467E-01 4.0666666666666347E-02

所以我用了你所有的10分。结果证明它有一个有效的计算输出。表示否[0,0,-1]

E[0.3020626878915512,-0.5152322345602659,-0.2301187342264469; 0.4556056566568232, 0.3185648205272573, 0.2998902206892574; 0.4240482789694776, -0.09650735405531717, 0.04705772208405343] 旋转[0.8636412841012429,0.50408818976266604,0.005017258321223739; -0.4976326944200423, 0.854097413780324, -0.1512590797870002; -0.0805321913287191, 0.1281368341222253, 0.9884814201091126] 罗德里格斯[0.14696997598983,0.04500046398453182,-0.5269188943544275] 翻译[-0.4250629252517419,-0.4494569974529025,0.7856907260602315]

这是我的代码

#include<opencv2/opencv.hpp>

using namespace std;

using namespace cv;
#define M_PI acos(-1.0)
int main(int argc, char *argv[])
{
    std::vector<cv::Point2d> Frame_A_points;
    std::vector<cv::Point2d> Frame_B_points;

    double focalx = 883.;
    double focaly = 867.;

    double focal = (focalx+focaly)/2;
       
    cv::Point2d camera_principal_point(827, 552);    
    Frame_A_points.push_back(Point2d(6.899999999999999467e-01*focalx, 4.066666666666666347e-02*focaly));
    Frame_A_points.push_back(Point2d(6.899999999999999467e-01*focalx, 4.066666666666666347e-02*focaly));
    Frame_A_points.push_back(Point2d(6.913333333333333552e-01*focalx, 4.066666666666666347e-02*focaly));    
    Frame_A_points.push_back(Point2d(6.919999999999999485e-01*focalx, 4.066666666666666347e-02*focaly));
    Frame_A_points.push_back(Point2d(6.946666666666666545e-01*focalx, 4.066666666666666347e-02*focaly)); 
    Frame_A_points.push_back(Point2d(6.986666666666666581e-01*focalx, 4.133333333333333304e-02*focaly));
    Frame_A_points.push_back(Point2d(7.026666666666666616e-01*focalx, 4.133333333333333304e-02*focaly)); 
    Frame_A_points.push_back(Point2d(7.073333333333332584e-01*focalx, 4.199999999999999567e-02*focaly));
    Frame_A_points.push_back(Point2d(7.119999999999999662e-01*focalx, 4.266666666666666524e-02*focaly)); 
    Frame_A_points.push_back(Point2d(7.173333333333332673e-01*focalx, 4.466666666666666702e-02*focaly));

    Frame_B_points.push_back(Point2d(3.893333333333333091e-01*focalx, 6.600000000000000311e-02*focaly));
    Frame_B_points.push_back(Point2d(3.900000000000000133e-01*focalx, 6.600000000000000311e-02*focaly));    
    Frame_B_points.push_back(Point2d(3.906666666666666621e-01*focalx, 6.533333333333332660e-02*focaly));
    Frame_B_points.push_back(Point2d(3.913333333333333108e-01*focalx, 6.466666666666666397e-02*focaly)); 
    Frame_B_points.push_back(Point2d(3.939999999999999614e-01*focalx, 6.333333333333332482e-02*focaly));
    Frame_B_points.push_back(Point2d(3.979999999999999649e-01*focalx, 6.066666666666666735e-02*focaly)); 
    Frame_B_points.push_back(Point2d(4.019999999999999685e-01*focalx, 5.799999999999999600e-02*focaly));
    Frame_B_points.push_back(Point2d(4.066666666666666763e-01*focalx, 5.600000000000000117e-02*focaly)); 
    Frame_B_points.push_back(Point2d(4.119999999999999774e-01*focalx, 5.333333333333332982e-02*focaly));
    Frame_B_points.push_back(Point2d(4.186666666666666314e-01*focalx, 5.133333333333333498e-02*focaly));



    std::cout<< Frame_A_points<< std::endl;


    Mat x(1000, 1000, CV_8UC3, Scalar(0, 0, 0));
    Mat y(1000, 1000, CV_8UC3, Scalar(0, 0, 0));
    double d = 0;
    for (int i = 0; i < Frame_A_points.size(); i++)
    {
        circle(x, Frame_A_points[i], 1, Scalar(0, 0, 255));
        circle(y, Frame_B_points[i], 1, Scalar(0, 255, 255));
        d += norm(Frame_A_points[i] - Frame_B_points[i]);
    }
    cout << "Mean marker distance" << d / Frame_A_points.size() << "\n";
   

 
        cv::Mat essential_matrix = cv::findEssentialMat(Frame_A_points, Frame_B_points, focal, camera_principal_point, cv::LMEDS);
        cv::Mat rotation=Mat::zeros(3,3,CV_64F), translation=Mat::zeros(3, 1, CV_64F);
        cv::recoverPose(essential_matrix, Frame_A_points, Frame_B_points, rotation, translation, focal, camera_principal_point);
        cv::Mat rot(3, 1, CV_64F);
        cv::Rodrigues(rotation, rot);
        //cout << "Essai " << i << " with " << Frame_A_points.size() << "points\n"; 
        std::cout << "E " << essential_matrix << std::endl;
        std::cout << "rotation " << rotation << std::endl;
        std::cout << "rodrigues " << rot.t() << std::endl;
        std::cout << "translation " << translation.t() << std::endl;
     imshow("ptx", x);
    imshow("pty", y);
    waitKey();
}
#包括
使用名称空间std;
使用名称空间cv;
#定义M_PI acos(-1.0)
int main(int argc,char*argv[])
{
std::向量帧A_点;
std::向量帧_B_点;
双焦点=883。;
双焦点=867。;
双焦点=(焦点x+焦点y)/2;
cv::Point2d摄像机"主要"点(827552);
帧点。向后推(点2D(6.89999999467E-01*focalx,4.0666666347E-02*focaly));
帧点。向后推(点2D(6.89999999467E-01*focalx,4.0666666347E-02*focaly));
帧点。向后推(点2D(6.913333552E-01*focalx,4.06666347E-02*focaly));
帧点。向后推(点2D(6.91999999485E-01*focalx,4.066666666347E-02*focaly));
帧点。向后推(点2D(6.94666545E-01*focalx,4.06666347E-02*focaly));
帧点。向后推(点2D(6.98666581E-01*focalx,4.13333304E-02*focaly));
帧点。向后推(点2D(7.02666616E-01*focalx,4.13333304E-02*focaly));
帧点。向后推(点2D(7.073332584E-01*focalx,4.19999999999567E-02*focaly));
帧点。向后推(点2D(7.119999999662E-01*focalx,4.26666524E-02*focaly));
帧点。向后推(点2D(7.173333332673E-01*focalx,4.4666666702E-02*focaly));
帧点。向后推(点2D(3.893333091E-01*focalx,6.60000000000000311E-02*focaly));
帧点。向后推(点2D(3.9000000000000133E-01*focalx,6.60000000000000311E-02*focaly));
帧点。向后推(点2D(3.906621E-01*focalx,6.533332660E-02*focaly));
帧点。向后推(点2D(3.913333108E-01*focalx,6.4666666397E-02*focaly));
帧点。向后推(点2D(3.93999999614E-01*focalx,6.33333333 2482E-02*focaly));
帧点。向后推(点2D(3.97999999649E-01*focalx,6.0666666735E-02*focaly));
帧点。向后推(点2D(4.019999999685E-01*focalx,5.79999999999600E-02*focaly));
帧点。向后推(点2D(4.06666763E-01*focalx,5.60000000000000117E-02*focaly));
帧点。向后推(点2D(4.11999999999774E-01*focalx,5.33333333 2982E-02*focaly));
帧点。向后推(点2D(4.18666314E-01*focalx,5.13333498E-02*focaly));

std::coutNeed a minimal repeatable sample.来自OpenCV图像校准cpp文件。我发现基于棋盘对应集找到正确姿势没有问题。您的输入数据很可能有问题。我使用了在Blender中渲染的模拟图像,因此我的点很容易与背景区分开来。我绘制了我不知道如何才能给你一个可复制的样本,因为整个程序要添加到帖子中有点长。也许我可以将项目上传到github或其他地方?显示模拟的图像对和选定的坐标。或者如果你真的有问题,你可以尝试使用opencv校准样本将向您展示其工作原理的给定样本数据。前两张图像是基座和第一个cam的校准图像。我有100张。在第三张图像中,我绘制了基座摄像头的所有提取点。这些绘制点与我的模拟点一致。选择的坐标?
#include<opencv2/opencv.hpp>

using namespace std;

using namespace cv;
#define M_PI acos(-1.0)
int main(int argc, char *argv[])
{
    std::vector<cv::Point2d> Frame_A_points;
    std::vector<cv::Point2d> Frame_B_points;

    double focalx = 883.;
    double focaly = 867.;

    double focal = (focalx+focaly)/2;
       
    cv::Point2d camera_principal_point(827, 552);    
    Frame_A_points.push_back(Point2d(6.899999999999999467e-01*focalx, 4.066666666666666347e-02*focaly));
    Frame_A_points.push_back(Point2d(6.899999999999999467e-01*focalx, 4.066666666666666347e-02*focaly));
    Frame_A_points.push_back(Point2d(6.913333333333333552e-01*focalx, 4.066666666666666347e-02*focaly));    
    Frame_A_points.push_back(Point2d(6.919999999999999485e-01*focalx, 4.066666666666666347e-02*focaly));
    Frame_A_points.push_back(Point2d(6.946666666666666545e-01*focalx, 4.066666666666666347e-02*focaly)); 
    Frame_A_points.push_back(Point2d(6.986666666666666581e-01*focalx, 4.133333333333333304e-02*focaly));
    Frame_A_points.push_back(Point2d(7.026666666666666616e-01*focalx, 4.133333333333333304e-02*focaly)); 
    Frame_A_points.push_back(Point2d(7.073333333333332584e-01*focalx, 4.199999999999999567e-02*focaly));
    Frame_A_points.push_back(Point2d(7.119999999999999662e-01*focalx, 4.266666666666666524e-02*focaly)); 
    Frame_A_points.push_back(Point2d(7.173333333333332673e-01*focalx, 4.466666666666666702e-02*focaly));

    Frame_B_points.push_back(Point2d(3.893333333333333091e-01*focalx, 6.600000000000000311e-02*focaly));
    Frame_B_points.push_back(Point2d(3.900000000000000133e-01*focalx, 6.600000000000000311e-02*focaly));    
    Frame_B_points.push_back(Point2d(3.906666666666666621e-01*focalx, 6.533333333333332660e-02*focaly));
    Frame_B_points.push_back(Point2d(3.913333333333333108e-01*focalx, 6.466666666666666397e-02*focaly)); 
    Frame_B_points.push_back(Point2d(3.939999999999999614e-01*focalx, 6.333333333333332482e-02*focaly));
    Frame_B_points.push_back(Point2d(3.979999999999999649e-01*focalx, 6.066666666666666735e-02*focaly)); 
    Frame_B_points.push_back(Point2d(4.019999999999999685e-01*focalx, 5.799999999999999600e-02*focaly));
    Frame_B_points.push_back(Point2d(4.066666666666666763e-01*focalx, 5.600000000000000117e-02*focaly)); 
    Frame_B_points.push_back(Point2d(4.119999999999999774e-01*focalx, 5.333333333333332982e-02*focaly));
    Frame_B_points.push_back(Point2d(4.186666666666666314e-01*focalx, 5.133333333333333498e-02*focaly));



    std::cout<< Frame_A_points<< std::endl;


    Mat x(1000, 1000, CV_8UC3, Scalar(0, 0, 0));
    Mat y(1000, 1000, CV_8UC3, Scalar(0, 0, 0));
    double d = 0;
    for (int i = 0; i < Frame_A_points.size(); i++)
    {
        circle(x, Frame_A_points[i], 1, Scalar(0, 0, 255));
        circle(y, Frame_B_points[i], 1, Scalar(0, 255, 255));
        d += norm(Frame_A_points[i] - Frame_B_points[i]);
    }
    cout << "Mean marker distance" << d / Frame_A_points.size() << "\n";
   

 
        cv::Mat essential_matrix = cv::findEssentialMat(Frame_A_points, Frame_B_points, focal, camera_principal_point, cv::LMEDS);
        cv::Mat rotation=Mat::zeros(3,3,CV_64F), translation=Mat::zeros(3, 1, CV_64F);
        cv::recoverPose(essential_matrix, Frame_A_points, Frame_B_points, rotation, translation, focal, camera_principal_point);
        cv::Mat rot(3, 1, CV_64F);
        cv::Rodrigues(rotation, rot);
        //cout << "Essai " << i << " with " << Frame_A_points.size() << "points\n"; 
        std::cout << "E " << essential_matrix << std::endl;
        std::cout << "rotation " << rotation << std::endl;
        std::cout << "rodrigues " << rot.t() << std::endl;
        std::cout << "translation " << translation.t() << std::endl;
     imshow("ptx", x);
    imshow("pty", y);
    waitKey();
}
    if self.id < 2:
        E, _ = cv2.findEssentialMat(self.good_new, self.good_old, self.focal, self.pp, cv2.RANSAC, 0.999, 1.0, None)
        _, self.R, self.t, _ = cv2.recoverPose(E, self.good_old, self.good_new, self.R, self.t, self.focal, self.pp, None)
    else:
        E, _ = cv2.findEssentialMat(self.good_new, self.good_old, self.focal, self.pp, cv2.RANSAC, 0.999, 1.0, None)
        _, R, t, _ = cv2.recoverPose(E, self.good_old, self.good_new, self.R.copy(), self.t.copy(), self.focal, self.pp, None)

        absolute_scale = self.get_absolute_scale()
        if (absolute_scale > 0.1 and abs(t[2][0]) > abs(t[0][0]) and abs(t[2][0]) > abs(t[1][0])):
            self.t = self.t + absolute_scale*self.R.dot(t)
            self.R = R.dot(self.R)