5

我刚刚开始使用 OpenCV,并且我有以下示例 .cpp 文件(来自 opencv.org):

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
    Mat image;
    image = imread( argv[1], 1 );

    if( argc != 2 || !image.data )
    {
        printf( "No image data \n" );
        return -1;
    }

    namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
    imshow( "Display Image", image );

    waitKey(0);

    return 0;
}

我有以下 CMakeList.cmake 文件:

project(opencvTEST)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

find_package(OpenCV REQUIRED)

# Project Executable
add_executable (test test.cpp)
target_link_libraries(test ${OpenCV_LIBS})

我有一台 Mac(OS 10.6.8),我已经用 CMake 安装了 OpenCV 2.4.3,我搜索了高低并尝试了许多不同的方法来编译这个测试程序(我正在使用命令line--no IDE),但我收到以下编译错误(显然,由于该include语句无法正常工作):

test.cpp:3:30: error: opencv2/opencv.hpp: No such file or directory
test.cpp:5: error: ‘cv’ is not a namespace-name
test.cpp:5: error: expected namespace-name before ‘;’ token
test.cpp: In function ‘int main(int, char**)’:
test.cpp:9: error: ‘Mat’ was not declared in this scope
test.cpp:9: error: expected `;' before ‘image’
test.cpp:10: error: ‘image’ was not declared in this scope
test.cpp:10: error: ‘imread’ was not declared in this scope
test.cpp:18: error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
test.cpp:18: error: ‘namedWindow’ was not declared in this scope
test.cpp:19: error: ‘imshow’ was not declared in this scope
test.cpp:21: error: ‘waitKey’ was not declared in this scope

opencv2我有一个与 , 位于同一目录中的文件夹test.cpp,并且opencv.hpp位于 中opencv2,所以我不明白为什么找不到它。有任何想法吗?

此外,一般来说,OpenCV 期望您将源文件(.cpp 等)放在哪里?

4

4 回答 4

5

我有完全相同的问题。我从 opencv 教程中运行了相同的示例并得到了相同的错误

error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope

我通过添加标题解决了这个问题:

#include <opencv/highgui.h>
于 2013-09-10T14:20:19.140 回答
3

您忘记添加 CMakeLists.txt

include_directories(${OpenCV_INCLUDE_DIRS})

在 find_package(需要 OpenCV)之后

于 2013-04-02T04:20:26.983 回答
1

我希望我确切地知道上面的问题是什么,但我只能猜测这与我在安装 OpenCV 2.4.4 的同时尝试使用 OpenCV 2.4.3 的事实有关(我猜这个引起了一些冲突)。

rm -rf我通过删除 OpenCV 2.4.3(只是在我放置它的目录上运行)并卸载了 2.4.4(使用自制软件:),让 OpenCV 2.4.4 在我的机器上运行brew uninstall opencv。在采取这些步骤清理之后,我运行了以下命令(你必须有homebrew):

$ brew update #just in case you're missing updates
$ brew tap homebrew/science #skip this if you already have the science formulae
$ brew install opencv #this took about 15 minutes

最后,我按照本教程进行操作,瞧,它奏效了!我将此归结为使用不直观的新软件,感到沮丧,并尝试了 273 种不同的方式来运行 OpenCV。我想所有这些加起来就是我不知道的安装,这(我认为)导致了严重的问题。我是唯一一个很难让 OpenCV 工作的人吗?

于 2013-04-03T01:42:42.753 回答
0

还有一个包含路径的包含文件夹:opencv\build\include。完整的标题在那里,您可以将 OpenCV_INCLUDE_DIR 设置为该目录

于 2013-04-02T04:55:18.510 回答