I’m using the code from our previous conversation to build a minimal example that shows how to retrieve frames from the camera and display them inside a QLabel
.
Notice that the value passed to cvCreateCameraCapture()
can change on your system. On my Linux 0
is the magic number. On Windows and Mac I use -1
.
For simplicity purposes, I’m just sharing the body of the main()
function:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
CvCapture* capture = cvCreateCameraCapture(0); // try -1 or values > 0 if your camera doesn't work
if (!capture)
{
std::cout << "Failed to create capture";
return -1;
}
IplImage* frame = NULL;
QLabel label;
while (1)
{
frame = cvQueryFrame(capture);
if( !frame ) break;
QImage qt_img = IplImage2QImage(frame);
label.setPixmap(QPixmap::fromImage(qt_img));
label.show();
// calling cvWaitKey() is essencial to create some delay between grabbing frames
cvWaitKey(10); // wait 10ms
}
return app.exec();
}
EDIT:
The procedure to play a video file or video from a camera are the same. The only thing that really changes is cvCreateCameraCapture()
for cvCreateFileCapture()
.
If cvCreateFileCapture()
is returning NULL
, it means that it couldn’t open the video file. This could happen for 2 reasons: it doesn’t have the codec to deal with the video, or it couldn’t find the file.
A lot of people make the folowwing mistake on Windows when loading files:
capture = cvCreateFileCapture("C:\path\video.avi");
They use single slashes, when they should be using double:
capture = cvCreateFileCapture("C:\\path\\video.avi");
and if something can fail, you must ALWAYS check the return of the call:
capture = cvCreateFileCapture("C:\\path\\video.avi");
if (!capture)
{
// print error message, then exit
}