Menu

How to plot a 3D graph (surfaceplot) from a Mat type of OpenCV using qwtplot3D?

General
new_user
2013-04-25
2013-04-25
  • new_user

    new_user - 2013-04-25

    I have a 2D Mat data/image using OpenCV. I would like to plot a 3D graph from it, the row as x-axis, column as y-axis & the pixel values on each coordinate(row,column) as z-axis. I would like to plot the graph similar to the surf function in Matlab. I tried to use the
    bool loadFromData (double **data, unsigned int columns, unsigned int rows, double minx, double maxx, double miny, double maxy)
    function but my program crashed when I debugged till this line. I am actually very new in using this qwtplot3d & qt4, so please help me see where i go wrong.

    What I did is copying all the pixel values into an array, then try to make it into a 2D array made of rows and columns and pass it to loadFromData like below:

    //abs_outcorr is Mat type of opencv 
    int rows=  abs_outcorr.rows;
    int columns=  abs_outcorr.cols *  abs_outcorr.channels();
    
    double fromMat[columns*rows];
    
    for (int j=0; j<rows; j++)
    {
        double* data=  abs_outcorr.ptr<double>(j); //access the pixel value from Mat
        for (int i=0; i<columns; i++)
        {
            fromMat[i] = data[i];
        }
    }
    
    double** x = new double * [rows];
    for(int i=0; i<columns; i++)
    x[i] = new double[rows];
    
    for(int i=0; i<rows; i++)
    for(int j=0; j<columns; j++)
        x[j][i] = fromMat[columns*i + j];
    
    for( int i=0; i<columns; i++)
    delete x[i];
    delete []x;
    
    
    /* My main project is based on mainwindow with some buttons. ALl these codes I implemented in a button clicked & I would like to pop out the graph, but not sure the codes below work */
    
    QWidget window;
    window.setWindowTitle("plot 3d");
    QHBoxLayout layout;
    Qwt3D::SurfacePlot qsp(&window);
    
    qsp.loadFromData(x, columns, rows, 0, columns, 0, rows);
    qsp.setRotation(30,0,15);
    qsp.setScale(1,1,1);
    qsp.setShift(0.15,0,0);
    qsp.setZoom(0.9);
    for (unsigned i=0; i!=qsp.coordinates()->axes.size(); ++i)
    {
    qsp.coordinates()->axes[i].setMajors(7);
    qsp.coordinates()->axes[i].setMinors(4);
    }
    
    qsp.coordinates()->axes[Qwt3D::X1].setLabelString("x");
    qsp.coordinates()->axes[Qwt3D::Y1].setLabelString("y");
    qsp.coordinates()->axes[Qwt3D::Z1].setLabelString("z");
    qsp.setCoordinateStyle(Qwt3D::BOX);
    qsp.updateData();
    qsp.updateGL();
    
    qsp.show();
    layout.addWidget(&qsp);
    window.setLayout(&layout);
    window.resize(1000,800);
    window.show();
    

    ////////////////////////////////////////////////////
    //These are some of my pixel value of my Mat (32x32)
    ////////////////////////////////////////////////////

    Row 0 Column 0 9.61749e-08
    Row 0 Column 1 2.16608e-09
    Row 0 Column 2 3.25873e-08
    Row 0 Column 3 2.65754e-08
    Row 0 Column 4 2.93116e-08
    Row 0 Column 5 6.55923e-08
    Row 0 Column 6 4.56592e-08
    Row 0 Column 7 4.91113e-08
    Row 0 Column 8 1.73816e-08
    Row 0 Column 9 2.27045e-10
    Row 0 Column 10 7.36088e-08
    .
    .
    .
    Row 16 Column 16 1
    .
    .
    .
    Row 31 Column 23 5.1846e-08
    Row 31 Column 24 1.01708e-07
    Row 31 Column 25 4.25331e-09
    Row 31 Column 26 2.77903e-08
    Row 31 Column 27 1.14044e-08
    Row 31 Column 28 6.03817e-08
    Row 31 Column 29 2.65248e-08
    Row 31 Column 30 1.46648e-08
    Row 31 Column 31 8.05808e-08

    Basically this Mat/image is a bright dot in the centre with value of 1, while the rest all black which are nearly 0. Thanks.

     
  • new_user

    new_user - 2013-04-25

    The expected result should look like a sharp straight peak in the middle of the plot.

     

Log in to post a comment.

MongoDB Logo MongoDB