2

I'm having trouble passing cv::mat data between forms on a qt gui application, for the moment I simply want to pass the Image chosen by the user on the main window and display it on the results page

void MainWindow::on_pushButton_clicked() 
{
QString fileName = QFileDialog::getOpenFileName(this,
 tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)")); 
Lateral= cv::imread(fileName.toAscii().data());
}

In the header file of the main window I definded:-

public:
   cv::Mat get_Lateral(cv::Mat img);
   cv::Mat get_Posteroanterior();

In the MainWindow.cpp file I have defined the folowing (i've tried a few variations of the method):-

cv::Mat MainWindow::get_Lateral(cv::Mat img ){
Lateral.copyTo(img);
return img;
}

cv::Mat MainWindow::get_Posteroanterior(){
return Posteroanterior;
} 

Finally on the new form I have something like this:-

 MainWindow Mw ;
cv::Mat op;
Mw.get_Lateral(op);
//(Mw.get_Posteroanterior()).copyTo(op);
cv::namedWindow("Lateral Image");
cv::imshow("Lateral Image",op);

When I run this I get a runtime error, so I added an if statment to check the contents of cv::mat op like this:-

 MainWindow Mw ;
cv::Mat op;
Mw.get_Lateral(op);
//(Mw.get_Posteroanterior()).copyTo(op);
if (!op.data)
   cv::namedWindow("dud Image");
else{
cv::namedWindow("Lateral Image");
cv::imshow("Lateral Image",op);
}

And I am given the dud image window implying that op is empty.

Any advice on how to do this process properly will be apppreciated, I am fairly new to opencv and c++ so I apologize for any blatant mistakes.

Cheers

melinnde
  • 77
  • 6

2 Answers2

1

Have a look at my answer involving integrating OpenCV with larger applications. I wouldn't recommend using the highgui imshow function inside of a Qt GUI application. It has done some weird things for me in the past.

Basically, you can convert the cv::Mat to a QImage and then either use QGLWidget, or just simply draw it onto a QPixmap if you don't particularly need high speed.

As for passing a Mat object between forms, you can either convert it to a QImage and then use signals/slots like my example shows, or if you need to manipulate the Mat object further, you can create a QMetaType. The QMetaType will allow you transmit it across forms just like you would any other native Qt object. Here is a Qt example to get you started.

Hope that is helpful!

Community
  • 1
  • 1
mevatron
  • 13,911
  • 4
  • 55
  • 72
0

The return value of get_Lateral is being discarded - you either need to modify it in place via a reference:

void MainWindow::get_Lateral(cv::Mat& img ){
Lateral.copyTo(img);
//return img;
}

or assign back to your local cv::Mat object in main:

MainWindow Mw ;
cv::Mat op;
op = Mw.get_Lateral(op);
//(Mw.get_Posteroanterior()).copyTo(op);
if (!op.data)
   cv::namedWindow("dud Image");
else{
cv::namedWindow("Lateral Image");
cv::imshow("Lateral Image",op);
}
je4d
  • 7,628
  • 32
  • 46
  • Thanks for the response , I tried each of your sugesstions but they both returned the dud image – melinnde Nov 05 '11 at 12:54
  • Ok - I'm not familiar with opencv so I can only spot pure C++ errors. But assuming that Lateral.copyTo does what it says on the tin, and you're still getting null in op.data, I'd check whether lateral.data is also null – je4d Nov 05 '11 at 13:01
  • No worries,cheers. I am able to open the lateral image when the new form is opened – melinnde Nov 05 '11 at 13:12