如何使用qwtplot3D从Mat类型的OpenCV绘制3D图形(surfaceplot)?

时间:2013-04-25 10:04:44

标签: c++ opencv graph 3d qt4

我有一个使用OpenCV的2D Mat数据/图像。我想从它绘制一个3D图形,行为x轴,列为y轴和&每个坐标(行,列)上的像素值为z轴。我想绘制类似于Matlab中的surf函数的图形。我试着用 bool loadFromData(double ** data,unsigned int columns,unsigned int rows,double minx,double maxx,double miny,double maxy) 函数,但我的程序崩溃时,我调试到这一行。我实际上是非常新的使用这个qwtplot3d& qt4,所以请帮我看看我哪里出错。

我所做的是将所有像素值复制到一个数组中,然后尝试将其变成由行和列组成的2D数组,并将其传递给loadFromData,如下所示:

//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

基本上这个Mat /图像是中心的亮点,值为1,而其余的都是黑色,接近0。 预期结果应该看起来像图中间的尖锐直线峰值。 谢谢。

0 个答案:

没有答案