简体   繁体   English

为什么此OpenCV代码不执行任何操作?

[英]Why doesn't this OpenCV code do anything?

I've been trying to use OpenCV with C++ but even though my code compiles (Visual Studio 2010 ), it doesn't ever do anything: 我一直在尝试将OpenCV与C ++一起使用,但是即使我的代码可以编译(Visual Studio 2010),它也不做任何事情:

#include <iostream>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cvwimage.h"
#include "cxcore.h"
#include "cxmisc.h"
#include "ml.h"

using namespace cv;
using namespace std;



int main()

{

    namedWindow("yolo", WINDOW_AUTOSIZE );
    waitKey(1);
    cout << "Why won't this show up?" << endl;

} }

It compiles OK, without errors but the program doesn't do anything - when I open it in console, it doesn't return the 'Why won't this show up?" text - there is nothing returned. 它编译正常,没有错误,但是程序什么也不做-当我在控制台中打开它时,它不会返回“为什么不会显示?”文本-没有任何返回。

Regardless of which tutorial piece of code I am trying to use, it never works and never does anything. 无论我尝试使用哪个教程代码,它都永远不会起作用,也不会做任何事情。

What is going on? 到底是怎么回事?

Best regards 最好的祝福

EDIT: When I set the wait time to 0 (forever) it still doesn't work. 编辑:当我将等待时间设置为0(永远)仍然不起作用。

The window does get created, however, because you have the waitKey function set to 1 millisecond it only exists for a very short period of time. 确实创建了该窗口,但是,因为将waitKey函数设置为1毫秒,所以它仅存在很短的时间。 Try using: 尝试使用:

waitKey(0);

waitKey(1); waitKey(1); is the culprit. 是罪魁祸首。 the minimum time is 1 millis (that#s what you got here) 最少时间是1毫秒(这就是您在这里得到的)

either make it : 要么做到:

waitKey(0); // forever

or increase the time to something sensible 或增加时间做些明智的事情

waitKey(5000); // 5 secs

It needs to be, 应该是

#include <cv.h>
#include <highgui.h>

cv::namedWindow("test_1", CV_WINDOW_AUTOSIZE );
cvWaitKey();
std::cout << "This will work because I just tested it." << endl;

I use CMake to link the libraries. 我使用CMake链接库。 The CMake code is, CMake代码是

FIND_PACKAGE( OpenCV REQUIRED )
TARGET_LINK_LIBRARIES( myProject ${OpenCV_LIBS} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM