Possible Duplicate:
Using boolean values in C
我是 C 的新手,想编写一个程序来检测来自网络摄像头的人脸,我在线获得了一个,我在 eclipse CDT 上使用 opencv-2.4.3,我在网上搜索了解决方案但没有为我的问题找到合适的解决方案,因此将其作为新问题发布。代码如下:
// 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;
}
我得到的错误是 true' undeclared(首次在此函数中使用),它导致 while 循环出现问题,我读到使用 while(true) 不是好的做法,但我应该怎么做。任何人都可以帮助我。
将其替换为例如
while(1)
或
for(;;)
或者你可以这样做(在循环之前定义 c
):
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
}
或者根本不使用 c
,但这将开始循环并等待 20 毫秒:
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);
}
第三种可能性:
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;
}
更新:同时想知道定义
是否更正确
#define true 1
#define false 0
或
#define true 1
#define false (!true)
或再次
#define false 0
#define true (!false)
因为如果我,比如说,做了:
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 }
我会想出一个非常奇怪的结果,我找到了 this link到一个稍微奇怪的结果。
我怀疑以安全的方式解决这个问题需要一些宏,比如
#define IS_FALSE(a) (0 == (a))
#define IS_TRUE(a) (!IS_FALSE(a))