Results 1 to 3 of 3

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

  1. #1
    Join Date
    Apr 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    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, please help me */

    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.
    The expected result should look like a sharp straight peak in the middle of the plot.
    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    Please use [CODE] tags when posting source code. Otherwise, we have no way to refer to specific lines in your code.

    Qt Code:
    1. double** x = new double * [rows];
    2. for(int i=0; i<columns; i++)
    3. x[i] = new double[rows];
    4.  
    5. for(int i=0; i<rows; i++)
    6. for(int j=0; j<columns; j++)
    7. x[j][i] = fromMat[columns*i + j];
    8.  
    9. for( int i=0; i<columns; i++)
    10. delete x[i];
    11. delete []x;
    12.  
    13. // ...and later
    14.  
    15. Qwt3D::SurfacePlot qsp(&window);
    16.  
    17. qsp.loadFromData(x, columns, rows, 0, columns, 0, rows);
    To copy to clipboard, switch view to plain text mode 

    In line 11 of this code, you delete the x matrix you created and filled, but yet in line 17, you pass it into loadData(). If that is what is really happening, then you're passing in an invalid pointer.

    In line 15, you are creating your surface plot on the stack. What happens to that instance? Is it in scope the entire time your plot is supposed to be visible? If this method exits after the window.show() call, then your qsp variable gets deleted, leaving the widget with another invalid pointer.

  3. #3
    Join Date
    Apr 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    I tried to digest your comments and I found where I go wrong. I'd solved the problem! Thanks a lot...

Similar Threads

  1. Replies: 0
    Last Post: 25th April 2013, 10:52
  2. Qwtplot3D : plot 3D polyline
    By franchouze in forum Qwt
    Replies: 0
    Last Post: 16th January 2013, 16:01
  3. Plot a graph without Qwt
    By abghosh in forum Qwt
    Replies: 2
    Last Post: 17th August 2010, 10:33
  4. how to plot graph using qwt ?
    By sunitverma04 in forum Qwt
    Replies: 1
    Last Post: 2nd April 2010, 11:27
  5. How to plot a graph in Qt ?
    By vinod in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 13:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.