Re: read webcam with opencv
First program:
The fundamental problem from a Qt viewpoint is that your code never reaches the Qt event loop, i.e. the a.exec() call, and therefore Qt is not functioning. QWidget::show() schedules the widget to be displayed when the event loop is functioning: it does not show it immediately.
Aside from that, you code leaks memory, line 6, such that it would probably not run for long before crashing out of memory or at least thrashing the machine's virtual memory.
Second program:
You create a named window "result" to display the frames, and then cvShowImage targeting a window called "Left".
Re: read webcam with opencv
thanks a lot!
well i changed the window name in the second program but it still does the same...
and regarding the first program, what do you advise then? removing the "while(1)" loop or extending it until the "return a.exec()" ?
Re: read webcam with opencv
The problem in the second program is entirely between you and OpenCV, nothing to do with Qt. I cannot offer much help.
First program: Neither. The looping behaviour needs to be replaced with event driven behaviour. It doesn't belong in the main() function. Try plugging your code into something like the framework below. Also, have your conversion function return the QImage by value rather than using the heap.
Code:
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
setCentralWidget(label);
// setup OpenCv
connect(&timer, SIGNAL(timeout()), SLOT(capture()));
timer.setInterval(200); // about 5 times per second
timer.start();
}
~MainWindow() {
// tear down OpenCv
}
public slots:
void capture() {
// capture, convert and display image on label
qDebug() << "Capturing";
}
private:
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
You can adjust the "sampling" rate or use a single shot timer to run as-fast-as-possible, but most video sources will produce a frame every 30th or 25th of a second.
Re: read webcam with opencv
Hi ,
I have made an application using opencv for webcam
I put here some of mine code , i hope it will help you
/***********************************/
//first you make a dummy image
QImage dummy(100,100,QImage::Format_RGB32);
image = dummy;
for (int x = 0; x < 100; x ++) {
for (int y =0; y < 100; y++) {
image.setPixel(x,y,qRgb(x, y, y));
}
}
//start a timer to capture frame of your webcam
pCamraTimer = new QTimer(this);
connect(pCamraTimer,SIGNAL(timeout()),this,SLOT(st artCapture()));
//In slot start capture
void WebCamViewer::startCapture()
{
if(NULL != camera)
{
IplImage *image=cvQueryFrame(camera);
putImage(image);
}
}
void WebCamViewer::putImage(IplImage *cvimage)
{
int cvIndex, cvLineStart;
// switch between bit depths
switch (cvimage->depth) {
case IPL_DEPTH_8U:
switch (cvimage->nChannels) {
case 3:
if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) ) {
QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB32);
image = temp;
}
cvIndex = 0; cvLineStart = 0;
for (int y = 0; y < cvimage->height; y++) {
unsigned char red,green,blue;
cvIndex = cvLineStart;
for (int x = 0; x < cvimage->width; x++) {
// DO it
red = cvimage->imageData[cvIndex+2];
green = cvimage->imageData[cvIndex+1];
blue = cvimage->imageData[cvIndex+0];
image.setPixel(x,y,qRgb(red, green, blue));
cvIndex += 3;
}
cvLineStart += cvimage->widthStep;
}
break;
default:
printf("This number of channels is not supported\n");
break;
}
break;
default:
printf("This type of IplImage is not implemented in QOpenCVWidget\n");
break;
}
//set this image to your label to show
ui->imgLbl->setPixmap(QPixmap::fromImage(image.scaled(ui->imgLbl->size(),Qt::KeepAspectRatio,Qt::SmoothTransformati on)));
}
/***********************************/
Re: read webcam with opencv
Thanks you all for some good guides around here!
I've tried to implement with these and found some problems:
+ the framework of ChrisW67 is good but in OpenCV, the SLOT capture() needs to hold an object Videocapture (an OpenCV class) to avoid repeating creating this class inside SLOT capture() -> by now, I still don't know how to work around this
Besides, I work with both Qt and OpenCV new versions Qt 4 and OpenCV 2.3.1 respectively and I had to do some editings to recode your ideas
** Solution:
I've just found it:
+ create a class variable VideoCapture cap
+ open it in class constructor with cap.Open(0) // default device
+ then, it can be used in SLOT without having to pass this 'cap' variable around
=> DONE already !! thanks all!