Ask Your Question
0

Issue setting up mapping3D modules from opencv_contrib and testing it with sample code.

asked 2017-09-10 10:38:07 -0600

I came across the mapping3D module in the opencv_contrib and am very interested in testing its functionality. I am having issues setting it up. I have stated below the steps I used. I hope someone can resolve my issue.

I setup OpenCV version 2.4.13 from sourceforge and followed the steps mentioned in the links: 1. http://docs.opencv.org/2.4/doc/tutori... 2. http://docs.opencv.org/2.4/doc/tutori...

The two points mentioned above works and I tested it with the source code given in the Test it! section of link 1.

For the part a of the project, I am trying to capture an image of a face or of a a land and convert it into a searchable landscape. This searchable landscape would be a 3D search space consisting of the coordinates (x, y, z). To accomplish this part, I am using OpenCV's module mapping3D which can be found on this link: 1. https://github.com/Tetragramm/opencv_....

To setup this module, I followed the steps mentioned in the link: 1. https://github.com/Tetragramm/opencv_...

I used the steps below the If you prefer using the gui version of cmake (cmake-gui), then, you can add opencv_contrib modules within opencv core by doing the following: ....

It generates an OpenCV sln file and that VS2012 solution contains all of the modules present in the opencv_contrib. When you execute step number 5. complete this OPENCV_EXTRA_MODULES_PATH by the proper pathname to the <opencv_contrib>/modules....., you might want to let OPENCV_EXTRA_MODULES_PATH have the value (path_to_your_OpenCVcontrib)/opencv_contrib/modules/mapping3d. Then I opened the OpenCV solution and build the mapping3D module and it worked.

Then I wanted to test with the sample code given by the developer of this module and have an issue with it. Sample Code Link:https://github.com/Tetragramm/op...

I created a new VS2012 project and gave it the proper include directories and the linked it with the proper .lib files. But I have a error with the following line: Ptr<orb> pORB = ORB::create( 10000, 1.2, 1 );

I am not sure where the error is but I am totally confused now. Did I miss something in the setup steps?

edit retag flag offensive close merge delete

Comments

If you need opencv_contrib you must clone opencv master 3.3-dev (or opencv at same date than original branch) :

and it is recommended to use them alongside the master branch or latest releases of OpenCV.

LBerger gravatar imageLBerger ( 2017-09-10 11:25:08 -0600 )edit

How do open a ply file which is being generated by the sample code? I am not sure which is the best application as I tried to use some tools like 3D viewer, but dont see anything. I installed the opencv using the exe for windows on sourceforge.

The contents of the ply file are like this. Not sure how to visualize it. ply format ascii 1.0 element vertex 1481 property float x property float y property float z end_header -6.16337e+011 -1.77463 -8.35503e+037 -2.35858e+011 1.42609 -0.000403444 -1.05862e-014 -1.81335 -3.56734e+028 0.394543 -1.87087 -1.2296e-020 3.62737e-016 -1.7585 -9.40945e-023 6.02611e+011 1.64101 -4.90946e+027 3.27006e-024 -1.91999 -3.39924e-022 -1.55096e-025 -1.74423 1.92807e+036 1.49853e+038 -1.75277 1.01956e+036 1.14181e-008 -1.9334 9.14855e-012 ...(more)

Androma gravatar imageAndroma ( 2017-09-10 12:05:16 -0600 )edit

I also see the sample code reads an Poses.xml file. I do not understand what does this file describes. link text

Androma gravatar imageAndroma ( 2017-09-10 12:06:22 -0600 )edit

I would be very thankful if you could please explain me the functionality very briefly so I could dive in deeper in the correct way

Androma gravatar imageAndroma ( 2017-09-10 12:10:46 -0600 )edit
LBerger gravatar imageLBerger ( 2017-09-10 13:34:06 -0600 )edit

@Tetragramm I got this module working and it generates the ouptut ply file. I changed the line: Ptr<orb> pORB = ORB::create( 10000, 1.2, 1 ); to Ptr<orb> pORB = ORB::create("ORB"). Was this the correct approach?

But I have some questions.

What does the coordinates in the output .ply file represent? What does the Poses file depict.

I am trying to get a 3D representation(x, y, z) of an 2-D landscape image. These 3D points when plotted using a 3D viewer would allow one to visualize the image in 3D.

I think this is what this module achieves but may you please confirm if what I am thinking is correct.

I would be really thankful if you could let me know about this and really appreciate the help provided by OpenCV.

Thanks, Androma

Androma gravatar imageAndroma ( 2017-09-10 15:36:37 -0600 )edit

So I think that interface changed in 3.3, so the example is out of date. That change to ORB should be fine.

The poses.xml file is the location and orientation of the camera at each frame. It also contains the camera matrix and distortion matrix, which remain constant, so there's only one of those.

The values in the .ply file are the 3d coordinates of the points that were tracked through all the frames. It is basically a sparse representation of the 3d scene. They ply file does not contain any information on textures or image data, so it wouldn't look like a 3d model, just a skeleton. Like THIS.

Tetragramm gravatar imageTetragramm ( 2017-09-10 16:44:39 -0600 )edit

For instance, if I only input one image of a mountain, will Iget an outline representation of this mountain? I tried it with only one image but it doesnt seem to work. Moreover, I am using the opencv 2.4.13 and it was cumbersome to set it up. I got this module working somehow, is it necessary to use the latest version?

I have a ply file generated like the following for the sample image but can't find a good tool to view it. Which one did you use?

Ply file: ply format ascii 1.0 element vertex 1481 property float x property float y property float z end_header 0 0 0 -6.16337e+011 -1.77463 -8.35503e+037 -2.35858e+011 1.42609 -0.000403444 -1.05862e-014 -1.81335 -3.56734e+028 0.394543 -1.87087 -1.2296e-020 3.62737e-016 -1.7585 -9.40945e-023 6.02611e ...(more)

Androm gravatar imageAndrom ( 2017-09-10 17:10:19 -0600 )edit

.... Does that mean the Poses.xml has to be left alone and not modified? I just want to confirm that the sparse representation of the 3d scene is generated from the input 2-D images? I just want the representation of the image in the form of points and this is exactly what i see in the imgur link you showed me. Does it work for one image? I see that you have used Viz to visualize the results. How to use it? May you please let me know to use it in this module. So can I just test the application with one image or do I need several angles of the same image? I am really confused as I am very new to this. I would be grateful to you if you could help clear my confusion and I really want to learn and become an expert in it ...(more)

Androm gravatar imageAndrom ( 2017-09-10 17:14:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-10 18:34:03 -0600

Tetragramm gravatar image

The methods in the mapping3d module are (at the moment) just a multi-camera/multi-view triangulation method, and one to match a 3d model to observations. It does not do the image processing to find the points in multiple images. It does not have any real intelligence.

You give it a set of image points (one per image), and the position and orientation of the cameras that took the images, and it will give you the best-estimate 3d location of that point. In order to get enough information, there must be either multiple cameras, or a moving camera, or both. One image is not enough. Multiple images from a stationary camera is not enough.

It will not figure out where the cameras are based on the images. You have to provide that information. Either a tracked camera, known scene points through solvePnP, or a Structure from Motion (SfM) algorithm.

The example you have been looking at are one scene I captured with a tracked camera. The poses.xml is valid for that image set only. If you want to use your own images, you need to either modify the poses.xml, or more usefully, just read that information from however you're already storing and pass it to the mapping3d:: functions in a manner similar to the example.

As for how to use Viz, it can read .ply files, and the tutorial HERE shows how to do that and draw camera axes.

Example code including VIZ: Sorry it's not as clean as the example, I just pulled this from history.

FileStorage fs;
fs.open("D:/Data/Vive/Poses.xml", FileStorage::FORMAT_AUTO);

Mat cameraMatrix;
fs["Camera_Matrix"] >> cameraMatrix;
cameraMatrix.convertTo(cameraMatrix, CV_64F);

double fovx, fovy, fl, ar;
Point2d pp;
calibrationMatrixValues( cameraMatrix, Size( 612, 460 ), 1, 1, fovx, fovy, fl, pp, ar );
cout << fovx << "  " << fovy << "\n";
calibrationMatrixValues( cameraMatrix, Size( 612, 460 ), 100, 100, fovx, fovy, fl, pp, ar );
cout << fovx << "  " << fovy << "\n";

int numFrames;
fs["nr_of_frames"] >> numFrames;


viz::Viz3d myWindow("Coordinate Frame");
myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
Affine3f cam_pose;

vector<Mat> imgBuffer, rBuffer, tBuffer;
vector<Mat> keyFramePyr, dstFramePyr;
vector<Point2f> keyFramePts, dstFramePts;
vector<vector<Point2f>> trackingPts;
for (int i = 1, j = 0; i < numFrames; i+=10, ++j)
{
    std::stringstream str1, str2;
    str1 << "D:/Data/Vive/" << i << ".png";
    str2 << "Pose_Matrix_" << i;
    cout << "Reading Frame " << i << "\n";
    imgBuffer.push_back(imread(str1.str()));
    Mat pose, rvec;
    fs[str2.str()] >> pose;

    Mat ident;
    ident.create(3, 3, pose.type());
    setIdentity(ident, -1);
    ident.at<float>(0, 0) = 1;

    pose(Rect(0, 0, 3, 3)) = (ident * pose(Rect(0, 0, 3, 3)).t()).t();

    Rodrigues(pose(Rect(0, 0, 3, 3)), rvec);
    rBuffer.push_back(rvec.clone());
    tBuffer.push_back(pose(Rect(3, 0, 1, 3)).clone());

    Vec3f rVec3f, tVec3f;
    rVec3f = Vec3f(rBuffer[j].at<float>(0), rBuffer[j].at<float>(1), rBuffer[j].at<float>(2));
    tVec3f = Vec3f(tBuffer[j].at<float>(0), tBuffer[j].at<float>(1), tBuffer[j].at<float>(2));
    cam_pose = Affine3f(rVec3f, tVec3f);

    viz::WCameraPosition cpw ...
(more)
edit flag offensive delete link more

Comments

So you mean that I need to have various angle of the same image? How do I modify the poses.xml file lets say for different angle images of a chair on the floor? (... If you want to use your own images, you need to either modify the poses.xml, or more usefully, just read that information from however you're already storing and pass it to the mapping3d:: ..) Could you provide me with an example on how to do thiss? I think modifying the Poses.xml file will easier but I am not sure on how to do. Your help would be greatly appreciated.

Androm gravatar imageAndrom ( 2017-09-10 18:46:11 -0600 )edit

I am not even sure if the ply file generated by the code is valid. How do I send it to you to verify it? Could I see the ply file you used to display in Viz? my email is [email protected]

Androm gravatar imageAndrom ( 2017-09-10 19:20:46 -0600 )edit

The poses.xml file contains two things.

First, the information about the camera matrix and distortion coefficients, which you can get from using calibrateCamera.

Second, a set of rotation and translations of where the camera was at every frame. Or where every camera is, if you've got more than one. In this example, they are stored as a 3x4 projection matrix, but you can see they are converted to the OpenCV standard rvec and tvec in the example.

You have to measure the location of your camera in some way. Whether it's using a tracked camera, or using known points and the solvePnP function, you need external information for this. If you can't do this, then this is not the algorithm for you. You should be using a SfM algorithm instead.

Tetragramm gravatar imageTetragramm ( 2017-09-10 19:21:07 -0600 )edit

HI,

I just had a quick question. I just want to something very simple. I want to just have an application which takes in an 2D input image and will give the 3D representation in the form of x,y,z coordinates as you showed in the image. I want to use this output for my application which I am going to build upon this.

I dont want to overcomplicate this part. Can you suggest me if you know any existing application or a sample code which will do this? I need to use these output points created in the 3D space for my application which I want to build.

Androm gravatar imageAndrom ( 2017-09-10 21:52:37 -0600 )edit

@Tetragramm I want to do something very simple and I think you will be best reference to help me.

Androm gravatar imageAndrom ( 2017-09-10 21:54:58 -0600 )edit

For that you either need a special camera to capture depth information, or a neural network trained on the same kind of scene. If you want like, chairs tables and couches, you can find one trained for that, but if you want anything else, good luck.

Single Image to 3D is very very hard.

Tetragramm gravatar imageTetragramm ( 2017-09-11 07:34:45 -0600 )edit

Question Tools

Stats

Asked: 2017-09-10 10:38:07 -0600

Seen: 374 times

Last updated: Sep 10 '17