Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 2016-08-04 04:27:24 -0600

Nbb gravatar image

reprojectImageTo3D gives weird results

I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below.

image description image description image description

reprojectImageTo3D gives weird results

I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below.below. Part of my code is also below. Disparity map must be divided by 256 as written on the dataset website.

image description image description image description

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------

    cv::Mat XYZ(disparity.size(), CV_32FC3);
    reprojectImageTo3D(disparity, XYZ, Q);

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

reprojectImageTo3D gives weird results

I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below. Part of my code is also below. Disparity map must be divided by 256 as written on the dataset website.

image description image description image description

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------

    cv::Mat XYZ(disparity.size(), CV_32FC3);
    reprojectImageTo3D(disparity, XYZ, Q);

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

The edited code based on the solution posted at http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------


Mat XYZ(disparity.size(), CV_32FC3);
Mat_<float> vec_temp(4, 1);

for (int row = 0; row < disparity.rows; row++) 
{
    for (int col = 0; col < disparity.cols; col++) 
    {
        vec_temp(0) = col; vec_temp(1) = row; vec_temp(2) = disparity.at<float>(row, col);

        // Discard points with 0 disparity    
        if (vec_temp(2) == 0) continue;
        vec_temp(3) = 1;
        vec_temp = Q*vec_temp;
        vec_temp = vec_temp / vec_temp(3);

        Vec3f &point = XYZ.at<Vec3f>(row, col);
        point[0] = vec_temp(0);
        point[1] = vec_temp(1);
        point[2] = vec_temp(2);         
    }
}

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

reprojectImageTo3D gives weird results

I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.

Please let me know if you need me to post the entire working code as well as the text file (dataset) with the calibration parameters.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below. Part of my code is also below. Disparity map must be divided by 256 as written on the dataset website.

image description image description image description

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------

    cv::Mat XYZ(disparity.size(), CV_32FC3);
    reprojectImageTo3D(disparity, XYZ, Q);

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

The edited code based on the solution posted at http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------


Mat XYZ(disparity.size(), CV_32FC3);
Mat_<float> vec_temp(4, 1);

for (int row = 0; row < disparity.rows; row++) 
{
    for (int col = 0; col < disparity.cols; col++) 
    {
        vec_temp(0) = col; vec_temp(1) = row; vec_temp(2) = disparity.at<float>(row, col);

        // Discard points with 0 disparity    
        if (vec_temp(2) == 0) continue;
        vec_temp(3) = 1;
        vec_temp = Q*vec_temp;
        vec_temp = vec_temp / vec_temp(3);

        Vec3f &point = XYZ.at<Vec3f>(row, col);
        point[0] = vec_temp(0);
        point[1] = vec_temp(1);
        point[2] = vec_temp(2);         
    }
}

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

reprojectImageTo3D gives weird results

I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.

Errors: http://stackoverflow.com/questions/26917890/opencv-pointcloud-from-depth-map?rq=1

Please let me know if you need me to post the entire working code as well as the text file (dataset) with the calibration parameters.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below. Part of my code is also below. Disparity map must be divided by 256 as written on the dataset website.

image description image description image description

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------

    cv::Mat XYZ(disparity.size(), CV_32FC3);
    reprojectImageTo3D(disparity, XYZ, Q);

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

The edited code based on the solution posted at http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------


Mat XYZ(disparity.size(), CV_32FC3);
Mat_<float> vec_temp(4, 1);

for (int row = 0; row < disparity.rows; row++) 
{
    for (int col = 0; col < disparity.cols; col++) 
    {
        vec_temp(0) = col; vec_temp(1) = row; vec_temp(2) = disparity.at<float>(row, col);

        // Discard points with 0 disparity    
        if (vec_temp(2) == 0) continue;
        vec_temp(3) = 1;
        vec_temp = Q*vec_temp;
        vec_temp = vec_temp / vec_temp(3);

        Vec3f &point = XYZ.at<Vec3f>(row, col);
        point[0] = vec_temp(0);
        point[1] = vec_temp(1);
        point[2] = vec_temp(2);         
    }
}

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

reprojectImageTo3D gives weird results

Any expert with a SOLUTION ?

I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.

Errors: http://stackoverflow.com/questions/26917890/opencv-pointcloud-from-depth-map?rq=1

Please let me know if you need me to post the entire working code as well as the text file (dataset) with the calibration parameters.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below. Part of my code is also below. Disparity map must be divided by 256 as written on the dataset website.

image description image description image description

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------

    cv::Mat XYZ(disparity.size(), CV_32FC3);
    reprojectImageTo3D(disparity, XYZ, Q);

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

The edited code based on the solution posted at http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------


Mat XYZ(disparity.size(), CV_32FC3);
Mat_<float> vec_temp(4, 1);

for (int row = 0; row < disparity.rows; row++) 
{
    for (int col = 0; col < disparity.cols; col++) 
    {
        vec_temp(0) = col; vec_temp(1) = row; vec_temp(2) = disparity.at<float>(row, col);

        // Discard points with 0 disparity    
        if (vec_temp(2) == 0) continue;
        vec_temp(3) = 1;
        vec_temp = Q*vec_temp;
        vec_temp = vec_temp / vec_temp(3);

        Vec3f &point = XYZ.at<Vec3f>(row, col);
        point[0] = vec_temp(0);
        point[1] = vec_temp(1);
        point[2] = vec_temp(2);         
    }
}

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

reprojectImageTo3D gives weird results

Any expert with a SOLUTION ?EDIT: So it turns out that the function was working as intended. I just had to change the clouds to the 'original scene' colour to realize that I was at a very bad viewing angle.. Im an idiot.. Results below

So I guess my next question which i'll post in a different thread would be how to initialize the camera position and viewing angle such that the 3D scene fills the screen and is viewed such that it looks similar to the image on the 1st picture.

Also berak, what did you mean by x,y ? If you meant pixel col and row then I would get a 'flat' 3D scene like below. I am getting very weird results when reconstructing the scene of an image given the Q matrix and the disparity map. So I googled and apparently there are many others experiencing the same problem. I have tried the solution here http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv but am still unable to geenrate anything decent. I hope someone can assist me on this. Q matrix is given below.not able to get a nice cube like you mentioned.

Errors: http://stackoverflow.com/questions/26917890/opencv-pointcloud-from-depth-map?rq=1

Please let me know if you need me to post the entire working code as well as the text file (dataset) with the calibration parameters.

[1, 0, 0, -612.5766;
  0, 1, 0, -175.67438;
  0, 0, 0, 724.63995;
  0, 0, 1.861616, -0]

Scene, Disparity Map and the Reconstructed Image produced via viz vtk are all shown below. Part of my code is also below. Disparity map must be divided by 256 as written on the dataset website.

image description image description image descriptionimage description image description image description image description

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------

    cv::Mat XYZ(disparity.size(), CV_32FC3);
    reprojectImageTo3D(disparity, XYZ, Q);

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }

The edited code based on the solution posted at http://stackoverflow.com/questions/22418846/reprojectimageto3d-in-opencv

    Mat disparity = imread("2011_09_26_drive_0095//0000000099_left_disparity.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat image = imread("2011_09_26_drive_0095//image//0000000099.png", CV_LOAD_IMAGE_GRAYSCALE);

    namedWindow("Disparity Map", CV_WINDOW_AUTOSIZE); imshow("Disparity Map", disparity);
    namedWindow("Image", CV_WINDOW_AUTOSIZE); imshow("Image", image);

    disparity.convertTo(disparity, CV_32F);
    disparity = disparity / 256.0;

    //----------------------------------------------------------------------
    // Reconstruct
    //----------------------------------------------------------------------


Mat XYZ(disparity.size(), CV_32FC3);
Mat_<float> vec_temp(4, 1);

for (int row = 0; row < disparity.rows; row++) 
{
    for (int col = 0; col < disparity.cols; col++) 
    {
        vec_temp(0) = col; vec_temp(1) = row; vec_temp(2) = disparity.at<float>(row, col);

        // Discard points with 0 disparity    
        if (vec_temp(2) == 0) continue;
        vec_temp(3) = 1;
        vec_temp = Q*vec_temp;
        vec_temp = vec_temp / vec_temp(3);

        Vec3f &point = XYZ.at<Vec3f>(row, col);
        point[0] = vec_temp(0);
        point[1] = vec_temp(1);
        point[2] = vec_temp(2);         
    }
}

    //----------------------------------------------------------------------
    // Visualize
    //----------------------------------------------------------------------

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    while (!myWindow.wasStopped())
    {
        /// Create a cloud widget
        viz::WCloud cw(XYZ, viz::Color::red());

        /// Display it in a window
        myWindow.showWidget("CloudWidget1", cw);

        myWindow.spinOnce(1, true);
    }