Simple webcam interface embedded while loop

Jun 24, 2013 at 5:23pm
Hello,

I'm quite a beginner at C++ so I apologize if this question is very elementary.

So essentially I am trying to write a C++ console application in Visual Studio 2010 that will display feeds from two stereo webcams. Then when the user decides that he likes how he the cameras are positioned he can "take a picture" by saving the current frame. The goal is to get bunch of images for calibration of the stereo cameras, so I would like to repeat this process n (defined by user) number of times.

Right now, my code is successfully saving the first image. However, it terminates immediately after that. It seems to me that instead of exiting just the embedded while loop, it is exiting the for loop as well. But I don't know why this is.

So my question to the forum is: Why is the code terminating after saving only 1 image, and how to I fix this?

Thank you very much in advance for any assistance. Below is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

# include "opencv/highgui.h"
# include "opencv/cv.h"
# include <conio.h>
# include <iostream>

int main(int argc, char** argv )
{
	//Initialize parameters
	CvCapture* capture0;
    CvCapture* capture1;
	char str0[80];
    char str1[80];
	char buffer [33];
	int numpics = 0;

	//Initialize cameras
	capture0 = cvCreateCameraCapture(0);
	capture1 = cvCreateCameraCapture(1);

	//Make sure cameras initialize
	assert( capture0 != NULL );
	assert( capture1 != NULL );

	//Initialize inidividual frame captures for each camera
	IplImage* img0 = cvQueryFrame( capture0 );
	IplImage* img1 = cvQueryFrame( capture1 );

	//Initialize frame capture for live show of video (might not be needed)
	IplImage* bgr_frame0 = cvQueryFrame( capture0);
	IplImage* bgr_frame1 = cvQueryFrame( capture1);
	
	//Determine how many pictures user would like to capture
	std::cout <<"How many calibration pictures would you like to take?";
	std::cin >> numpics;

	cvNamedWindow( "Webcam0", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Webcam1", CV_WINDOW_AUTOSIZE );

	for (int n=0; n<numpics; n++) 
	{

		//converting n into a character, 10 means it's base 10
		char *frame = itoa(n,buffer,10);

		//Creating file names for saving
		strcpy(str0,"calib_pics/camR_");
		strcat(str0,frame);
		strcat(str0,".tif");
	
		strcpy(str1,"calib_pics/camL_");
		strcat(str1,frame);
		strcat(str1,".tif");

		std::cout << "press any key to take picture";

		while( (bgr_frame0 = cvQueryFrame( capture0)) != NULL &&
			(bgr_frame1 = cvQueryFrame( capture1)) != NULL
			&& !kbhit() )
		{
			cvShowImage( "Webcam0", bgr_frame0 );
			cvShowImage( "Webcam1", bgr_frame1 );

			char c = cvWaitKey( 33 );
			if( c == 27 ) break;

			cvSaveImage(str0, bgr_frame0);
			cvSaveImage(str1, bgr_frame1);
		}

	}
		cvReleaseCapture( &capture0 );
		cvReleaseCapture( &capture1 );
		cvDestroyWindow( "Webcam0" );
		cvDestroyWindow( "Webcam1" );
		return( 0 );
}
Topic archived. No new replies allowed.