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.
P.S. 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();
}