c - ‘true’ 在 opencv 中未声明(首次在此函数中使用)

标签 c opencv

<分区>

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))

关于c - ‘true’ 在 opencv 中未声明(首次在此函数中使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14376940/

相关文章:

c - c中函数的语法错误

c++ - 如何使用 Make 构建与一些 C++ 代码混合的 C 项目?

c - pthread_mutex_lock 仅适用于 sleep

c - 绘制溢出到顶级窗口之外的子窗口

c++ - cv::viz::Widget 是否可点击? (OpenCV C++)

c++ - fastNlMeansDenoising() 不滤除噪声

c - 警告 : format ‘%s’ expects type ‘char *’ , 但参数 2 的类型为 ‘int’

opencv - 模板匹配——不同尺寸的模板和图像

Python OpenCV : Detecting a general direction of movement?

android - 从线程将bmp图像保存到SD卡