我正在尝试向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的函数
答案 0 :(得分:1)
像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);
}