[英]How to show capture image in QLabel
我正在嘗試將實時攝像機圖像顯示到Qlabel。當我啟動代碼時,它沒有給出任何錯誤,並且我的攝像機指示燈變為藍色,這意味着工作正常。 但是ui無法啟動。 調試代碼后,我發現在while(true)
它總是循環運行,但是ui->lblProcessedVideo->setPixmap.....
命令未顯示任何ui。
請您告訴我我的錯誤。
這是我的部分代碼:
void MainWindow::getImageFromVideo()
{
CvCapture* capture;
cv::Mat frame;
cv::Mat gray_frame;
capture = cvCaptureFromCAM( 0 );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
if( !frame.empty() )
{
cvtColor( frame, gray_frame, CV_BGR2GRAY);
equalizeHist( gray_frame, gray_frame );
ui->lblProcessedVideo->setPixmap( QPixmap::fromImage( Mat2QImage( frame )));
}
}
}
}
編輯: Mat2QImage()
是將Mat轉換為QImage的函數
就像Ezee所說的那樣,您需要將從攝像機捕獲的圖像委托給單獨的線程,然后將圖像發送到GUI線程。 這是示例代碼:
//timer.h
class Timer : public QThread
{
Q_OBJECT
public:
explicit Timer(QObject *parent = 0);
void run();
signals:
void updFrame(QPixmap);
public slots:
};
//timer.cpp
Timer::Timer(QObject *parent) :
QThread(parent)
{
}
void Timer::run() {
VideoCapture cap(0); // open the default camera
for(;;){
Mat frame;
cap.read(frame);
QPixmap pix = QPixmap::fromImage(IMUtils::Mat2QImage(frame));
emit updFrame(pix);
if( waitKey (30) >= 0){
break;
}
}
}
//videoviewer.h
class VideoViewer : public QLabel
{
Q_OBJECT
public:
explicit VideoViewer(QObject *parent = 0);
signals:
public slots:
void updateImage(QPixmap pix);
};
//videoviever.cpp
VideoViewer::VideoViewer(QObject *parent) :
QLabel()
{
Timer* timer = new Timer();
connect(timer,SIGNAL(updFrame(QPixmap)),this,SLOT(updateImage(QPixmap)));
timer->start();
}
void VideoViewer::updateImage(QPixmap pix){
this->setPixmap(pix);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.