我刚刚开始使用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,我搜索了高低,尝试了很多不同的东西来编译这个测试程序(我是使用命令行 - 没有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等)文件放在哪里?
答案 0 :(得分:5)
我有完全相同的问题。我从opencv教程运行了同样的例子并得到了同样的错误
error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
我通过添加标题解决了这个问题:
#include <opencv/highgui.h>
答案 1 :(得分:3)
您忘记添加CMakeLists.txt
include_directories(${OpenCV_INCLUDE_DIRS})
在find_package(OpenCV REQUIRED)之后
答案 2 :(得分:1)
我希望我确切知道上面的问题,但我只能猜测它与我在安装OpenCV 2.4.4时尝试使用OpenCV 2.4.3的事实有关(我'猜测这会引起一些冲突。)
我通过删除OpenCV 2.4.3(仅在我放置它的目录上运行rm -rf
)在我的机器上运行OpenCV 2.4.4并卸载2.4.4(使用自制程序:{{1} })。在完成这些步骤后,我运行了以下命令(您必须拥有homebrew):
brew uninstall opencv
最后,我跟着this tutorial,瞧,它有效!我用这个方法来处理那些不直观,沮丧,并尝试273种不同方式来运行OpenCV的新软件。我想这一切都加起来有一个我不知道的安装,我认为这导致了严重的问题。我是唯一一个让OpenCV难以工作的人吗?
答案 3 :(得分:0)
还有一个包含路径的include文件夹:opencv \ build \ include。完整的标题在那里,您可以将OpenCV_INCLUDE_DIR设置为此目录