1.11M Members

Capturing and Displaying Video with OpenCV

 
0
 

Hello,

I am trying to capture a video stream from a webcam, and simultaneously display the frames, using OpenCV. Previously, I have captured one image, and then at the end of my program displayed the image in a window, which works fine. However, when I try to capture a sequence of several images, and display them in the window, then the window just turns grey, rather than displaying the image. Only at the end of the program will it display an image - not in the middle of the program. Below is the code I am using:

int main()
{
	CvCapture* capture = cvCaptureFromCAM(0);
	IplImage* image;
	cvNamedWindow("Window");

	while (true)
	{
		cvGrabFrame(capture);
		image = cvRetrieveFrame(capture);
		cvShowImage("Window", image);
	}

	return 0;
}

The window is simply grey the whole time, rather than updating the image as I want it to. It can show an image, but only once the while loop is completed, and the program reaches its return line. But I want it to show the images simultaneously as I capture them, such that I get a video displayed in real-time.

Any ideas?

Thanks.

 
0
 

try:

while (true)
{
    image= cvQueryFrame(capture); 
    cvShowImage("Window", image);
}
 
0
 

try:

while (true)
{
    image= cvQueryFrame(capture); 
    cvShowImage("Window", image);
}

Hi,

I tried your solution, but the same thing happens. If I ask the program to print out something to the screen after cvShowImage(), then it does so, proving that the code is running, but the images are just not being displayed...

 
0
 
while (true)
	{
                 cvGrabFrame(capture); 
         /* u can use this instead of both the functions cvGrabFram()&cvRetrieveFrame()
	   image = cvQueryFrame( capture );*/
		
                  image = cvRetrieveFrame(capture);
		cvShowImage("Window", image);
		if( (cvWaitKey(10) & 255) == 27 ) break;
	}

Hope this works....!

Hello,

I am trying to capture a video stream from a webcam, and simultaneously display the frames, using OpenCV. Previously, I have captured one image, and then at the end of my program displayed the image in a window, which works fine. However, when I try to capture a sequence of several images, and display them in the window, then the window just turns grey, rather than displaying the image. Only at the end of the program will it display an image - not in the middle of the program. Below is the code I am using:

int main()
{
	CvCapture* capture = cvCaptureFromCAM(0);
	IplImage* image;
	cvNamedWindow("Window");

	while (true)
	{
		cvGrabFrame(capture);
		image = cvRetrieveFrame(capture);
		cvShowImage("Window", image);
	}

	return 0;
}

The window is simply grey the whole time, rather than updating the image as I want it to. It can show an image, but only once the while loop is completed, and the program reaches its return line. But I want it to show the images simultaneously as I capture them, such that I get a video displayed in real-time.

Any ideas?

Thanks.

 
0
 

here's my code and it works better:

#include "stdafx.h"
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "stdlib.h"
#ifdef _EiC
#define WIN32
#endif

char path[50];
int main ( int argc, const char* argv[] ){
    CvCapture* capture = cvCaptureFromCAM(0);
    IplImage* image = 0;
    cvNamedWindow("Window");

    int count = 1;
    while (true)
    {
        image = cvQueryFrame(capture); 
        cvShowImage("Window", image);
        int ch = cvWaitKey(1);

        if(ch == 115){
            sprintf(path,"d:/something/captured%d.bmp",count++);
            cvSaveImage(path,image,0);
        }
        if(ch == 27) break;
    }

    cvDestroyWindow("Window");
    cvReleaseCapture(&capture);
   return 0;
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article