Possible Duplicate:
Using boolean values in C
I am newbie to C and want to write a program that will detect face from web cam, I got one on line,I am using opencv-2.4.3 on eclipse CDT, I searched on line for the solution but did not get the appropriate solution for my problem so posting it as new question.Here is the code:
// Include header files
#include "/home/OpenCV-2.4.3/include/opencv/cv.h"
#include "/home/OpenCV-2.4.3/include/opencv/highgui.h"
#include "stdafx.h"
int main(){
//initialize to load the video stream, find the device
CvCapture *capture = cvCaptureFromCAM( 0 );
if (!capture) return 1;
//create a window
cvNamedWindow("BLINK",1);
while (true){
//grab each frame sequentially
IplImage* frame = cvQueryFrame( capture );
if (!frame) break;
//show the retrived frame in the window
cvShowImage("BLINK", frame);
//wait for 20 ms
int c = cvWaitKey(20);
//exit the loop if user press "Esc" key
if((char)c == 27 )break;
}
//destroy the opened window
cvDestroyWindow("BLINK");
//release memory
cvReleaseCapture(&capture);
return 0;
}
And I am getting error as true’ undeclared (first use in this function), It is causing problem in while loop, I read it is not good practise to use while(true) but how should I go about. Can anybody hellp me out.
Replace it with e.g.
while(1)
or
for(;;)
or you can do (defining c before the loop):
while (c != 27)
{
//grab each frame sequentially
IplImage* frame = cvQueryFrame( capture );
if (!frame)
break;
//show the retrieved frame in the window
cvShowImage("BLINK", frame);
//wait for 20 ms
c = cvWaitKey(20);
//exit the loop if user press "Esc" key
}
or without c at all, but this will start the loop with a 20ms wait:
while (cvWaitKey(20) != 27)
{
//grab each frame sequentially
IplImage* frame = cvQueryFrame( capture );
if (!frame)
break;
//show the retrieved frame in the window
cvShowImage("BLINK", frame);
}
And a third possibility:
for(;;)
{
//grab each frame sequentially
IplImage* frame = cvQueryFrame( capture );
if (!frame)
break;
//show the retrieved frame in the window
cvShowImage("BLINK", frame);
if (cvWaitKey(20) == 27)
break;
}
UPDATE: while wondering whether it would be more correct to define
#define true 1
#define false 0
or
#define true 1
#define false (!true)
or again
#define false 0
#define true (!false)
because if I, say, did:
int a = 5;
if (a == true) { // This is false. a is 5 and not 1. So a is not true }
if (a == false){ // This too is false. So a is not false }
I would come up with a really weird result, I found this link to a slightly weirder result.
I suspect that to solve this in a safe way would require some macro such as
#define IS_FALSE(a) (0 == (a))
#define IS_TRUE(a) (!IS_FALSE(a))
true is not defined in many versions of c. If you want to use "boolean" see Using boolean values in C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With