我一直在关注 this 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(与您的操作系统几乎相同)时遇到了同样的问题
我能够让它编译的唯一方法是使用 these 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 在其文档中还有两种方法来说明如何执行此操作。
关于c++ - 带有 C++ 的 OpenCV 无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38468064/