c – 使用Qt显示来自OpenCV的网络摄像头流
发布时间:2020-12-16 07:16:36 所属栏目:百科 来源:网络整理
导读:所以我可以看到我的网络摄像头流与OpenCV与imshow与这个简单的代码 int main(int,char**){ VideoCapture cap(0); Mat edges; namedWindow("webcam",1); while (true) { Mat frame; cap frame; imshow("webcam",frame); if (waitKey(30) = 0) break; } return
所以我可以看到我的网络摄像头流与OpenCV与imshow与这个简单的代码
int main(int,char**) { VideoCapture cap(0); Mat edges; namedWindow("webcam",1); while (true) { Mat frame; cap >> frame; imshow("webcam",frame); if (waitKey(30) >= 0) break; } return 0; } 现在我想要的是在QT的Widget中显示来自OpenCV的QImage中的图像 QImage Mat2QImage(cv::Mat const& src) { cv::Mat temp; cvtColor(src,temp,CV_BGR2RGB); QImage dest((const uchar *)temp.data,temp.cols,temp.rows,temp.step,QImage::Format_RGB888); dest.bits(); // of QImage::QImage ( const uchar * data,int width,int height,Format format ) return dest; } 以及在QT中用QImage显示图像的小代码 int main(int argc,char *argv[]) { QApplication a(argc,argv); QImage myImage; myImage.load("a.png"); QLabel myLabel; myLabel.setPixmap(QPixmap::fromImage(myImage)); myLabel.show(); return a.exec(); } 我试图以这种方式组合它们,但没有运气 int main(int argc,argv); VideoCapture cap(0); QImage myImage; QLabel myLabel; while (true) { Mat frame; cap >> frame; // get a new frame from camera myImage = Mat2QImage(frame); myLabel.setPixmap(QPixmap::fromImage(myImage)); } myLabel.show(); return a.exec(); 解决方法
您必须使用QTimer创建一个继承自QMainWindow的Window.在构造函数中,将计时器连接到Window方法.您将openCV代码放入此超时方法,该方法将每X毫秒调用一次:
class Window : public QMainWindow { Q_OBJECT QTimer _timer; private slots: void on_timeout() { // put your opencv code in it } public: Window() : QMainWindow(),_timer(this) { connect(&_timer,SIGNAL(timeout()),this,SLOT(on_timeout())); // populate your window with images,labels,etc. here _timer.start(10 /*call the timer every 10 ms*/); } }; 然后在主要显示你的窗口: int main(int argc,argv); Window win; win.show(); return a.exec(); } 如果您使用Qt创建者,使用Qt开发更简单:考虑一下. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |