weird trouble displaying openCv video on qlabel
I am having trouble with displaying opencv Video on a Qlabel.
I'm new using opencv and qt, and these week I was trying to make a tiny exercise using a qt button event to display a video capture from opencv to a qlabel of my widget. But weirdly the program says "The program has unexpectedly finished", when I run the code attached below. Please help me cuz for me nothing seems to be wrong. Thanks for your time and greetings from Costa Rica.
PS When I simply try to run the openCv code without any gui, I mean just using the code inside the buttonClicked event and cvShowImage("Video", frame); to display the video the program run good but strip an error and several warnings like this.
HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
libpng warning: Application built with libpng-1.2.49 but running with 1.5.12
Attached code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;
IplImage* imgTracking=0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
CvCapture *cap; cap = cvCaptureFromCAM(1);
IplImage *frame; frame = cvQueryFrame(cap);
bool play = true;
while(frame && play){
cvWaitKey(10); IplImage *img = cvCloneImage(frame);
if (img->origin) {
cvFlip(img);
img->origin= 0;
}
QImage qimg;
qimg = IplImage2QImage(img);
//cvShowImage("Video", frame);
ui->labVideo->setPixmap(QPixmap::fromImage(qimg));
cvReleaseImage(&img);
frame = cvQueryFrame(cap);
}
cvReleaseCapture(&cap);
}
QImage MainWindow::IplImage2QImage(const IplImage *iplImage)
{
int height = iplImage->height;
int width = iplImage->width;
const uchar *qImageBuffer =(const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
return img.rgbSwapped();
}
It is so wrong. When you use Qt you shouldn't use cvWaitKey
and have own loop. This function is just addition in openCv for testing or when you don't have ui freamework. When you use Qt you have UI framework and openCV should be used to image processing only (this is the purpose of this library)!
Replace this loop with QTimer and let QEventLoop do its job. CvCapture *cap
must be field of class.
void MainWindow::on_timerTimeout()
{
IplImage *frame = cvQueryFrame(cap);
if (!frame) {
stopPlay();
return;
}
IplImage *img = cvCloneImage(frame);
if (img->origin) {
cvFlip(img);
img->origin= 0;
}
QImage qimg = IplImage2QImage(img);
ui->labVideo->setPixmap(QPixmap::fromImage(qimg)); // possible replace with signal emit newFrame(QPixmap::fromImage(qimg));
cvReleaseImage(&img);
}
void MainWindow::stopPlay() {
timer->stop();
cvReleaseCapture(&cap);
}
void MainWindow::on_pushButton_clicked() {
timer->start();
cap = cvCaptureFromCAM(1);
}
链接地址: http://www.djcxy.com/p/83780.html