我一直在学习如何将OpenCV与C++结合使用的教程,并使用以下代码读取图像。
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("map.png", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
我在vi中创建了一个简单的.cpp文件,并尝试使用g++-o测试.cpp.
我检查了一下,在/usr/bin中有OpenCV。以下是我在编译时遇到的错误:
error: opencv2/highgui/highgui.hpp: No such file or directory
error: ‘cv’ is not a namespace-name
error: expected namespace-name before ‘;’ token
In function ‘int main()’:
error: ‘Mat’ was not declared in this scope
error: expected ‘;’ before ‘img’
error: ‘img’ was not declared in this scope
error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
error: ‘namedWindow’ was not declared in this scope
error: ‘img’ was not declared in this scope
error: ‘imshow’ was not declared in this scope
error: ‘waitKey’ was not declared in this scope
error: ‘destroyWindow’ was not declared in this scope
我正在使用RHEL 6。我能做些什么来解决这个问题?非常感谢。
我在使用CentOS 7时遇到了同样的问题(与您的操作系统几乎相同)
我能够编译它的唯一方法是在OpenCV文档中使用CMake的这些指令。
但基本上。。。
1) 将程序源置于空文件夹中。
2) 用以下行在同一文件夹中创建一个CMakeLists.txt文件。。。
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
注意:如果你需要额外的标志,这部分还有几行,但这是另一个主题。。。
3) 然后运行这些bash命令。。。
$cd <DisplayImage_directory>
$cmake .
$make
如果这不起作用,那么OpenCV在其文档中还有两个方法来说明如何做到这一点。