在表单之间传递 cv::Mat img
我在 qt gui 应用程序上的表单之间传递 cv::mat 数据时遇到问题,目前我只想传递用户在主窗口上选择的图像并将其显示在结果页面上
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());
}
的头文件中我定义的主窗口:-
public:
cv::Mat get_Lateral(cv::Mat img);
cv::Mat get_Posteroanterior();
在 MainWindow.cpp 文件中,我定义了以下内容(我尝试了该方法的一些变体):-
cv::Mat MainWindow::get_Lateral(cv::Mat img ){
Lateral.copyTo(img);
return img;
}
cv::Mat MainWindow::get_Posteroanterior(){
return Posteroanterior;
}
最后在新表单上我有这样的内容:-
MainWindow Mw ;
cv::Mat op;
Mw.get_Lateral(op);
//(Mw.get_Posteroanterior()).copyTo(op);
cv::namedWindow("Lateral Image");
cv::imshow("Lateral Image",op);
当我运行这个时,我得到一个运行时错误,所以我添加了一个if语句来检查 cv::mat op 的内容,如下所示:-
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);
}
我得到了一个无用的图像窗口,暗示 op 是空的。
任何有关如何正确执行此过程的建议将不胜感激,我对 opencv 和 c++ 相当陌生,因此我对任何明显的错误表示歉意。
干杯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看我的答案,涉及将 OpenCV 与大型应用程序集成。我不建议在 Qt GUI 应用程序中使用 highgui
imshow
函数。它过去为我做过一些奇怪的事情。基本上,您可以将
cv::Mat
转换为QImage
,然后使用QGLWidget
,或者只是将其绘制到QPixmap
如果你不是特别需要高速。至于在表单之间传递
Mat
对象,您可以将其转换为QImage
然后使用信号/槽(如我的示例所示),或者如果您需要操作 < code>Mat 对象进一步,您可以创建一个 QMetaType。QMetaType
将允许您跨表单传输它,就像任何其他本机 Qt 对象一样。 这里是一个帮助您入门的 Qt 示例。希望有帮助!
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 aQImage
and then either useQGLWidget
, or just simply draw it onto aQPixmap
if you don't particularly need high speed.As for passing a
Mat
object between forms, you can either convert it to aQImage
and then use signals/slots like my example shows, or if you need to manipulate theMat
object further, you can create a QMetaType. TheQMetaType
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!
get_Lateral 的返回值被丢弃 - 您需要通过引用修改它:
或者分配回 main 中的本地 cv::Mat 对象:
The return value of get_Lateral is being discarded - you either need to modify it in place via a reference:
or assign back to your local cv::Mat object in main: