Fit a cylinder given a set of points and a easy visualization for 3d axis

asked 2015-02-17 10:39:57 -0500

LivingSparks gravatar image

Hi I have set of points that approximate a cylinder (from a feature detection)

 std::vector<cv::Point3f> objectPoints;

and I would like to know how I can get the representation in space of this cylinder.

What I really like is the projection of the vector of the axes in simulated 3D like this:

I have vertices of a generic three-dimensional space:

        std::vector<cv::Point3f> verts(4);
        verts[0] = cvPoint3D32f(0, 0, 0);
        verts[1] = cvPoint3D32f(0, 1, 0);
        verts[2] = cvPoint3D32f(1, 0, 0);
        verts[3] = cvPoint3D32f(0, 0, 1);

and edges connecting the verts

        std::vector<cv::Point2d> edges(3);
        edges[0] = cvPoint2D32f(0, 1);
        edges[1] = cvPoint2D32f(0, 2);
        edges[2] = cvPoint2D32f(0, 3);

  cv::projectPoints(verts, rvec, tvec, cameraMatrix, distCoeffs, projectedVertPoints);

  for (int i = 0; i<edges.size(); i++) {

            cv::Point2d vertA,vertB;
            vertA = projectedVertPoints[edges[i].x];
            vertB = projectedVertPoints[edges[i].y];
     // Here you can play with the colors and give each axis its classic color, red green and blue
            cv::line(src, vertA, vertB,cv::Scalar(0,255,255));

        }

I took inspiration from the demo project in python plane_ar.py

edit retag flag offensive close merge delete