Reputation: 4333
I am trying to display live camera image to Qlabel.. When I start code it does not give any error and my camera light goes to blue which means working. However ui does not start. After I debug my code I see that in while(true)
it always looping but ui->lblProcessedVideo->setPixmap.....
command does not shows any ui.
Could you please kindly show me my mistake..
Here is my partial code:
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 )));
}
}
}
}
EDIT: Mat2QImage()
is a function which convert Mat to QImage
Upvotes: 0
Views: 1161
Reputation: 103
Like Ezee said you need to delegate capturing image from camera to separate thread, next send image to GUI thread. heres sample code:
//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);
}
Upvotes: 1