带有C++的OpenCV无法编译
OpenCV with C++ won't compile
我一直在学习如何将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在其文档中还有两个方法来说明如何做到这一点。
相关文章:
- Mingw-64 在构建和安装后不会编译 openCV 代码
- 无法在 CLion 中编译 OpenCV 头文件
- 在Linux/Ubuntu中编译openCV应用程序时,如何解决未定义的引用cv:..?
- 在构建时从源代码编译 OpenCV 3
- 编译 opencv 示例麻烦
- 尝试从 Github 编译 OpenCV QT 代码时出错
- 当 Mingw 为 C++ 编译 OpenCV 时,我遇到错误?
- 如何使用text.hpp(可选模块)重新编译OpenCV
- 使用命名空间别名编译 OpenCV
- 从源代码编译 openCV:*** 未指定目标,也未找到生成文件.停
- 如何用GTK 2.x而不是GTK3.X编译OpENCV
- 在 ubuntu 上交叉编译 openCV for ARM
- 当我尝试在启用推理引擎的情况下编译 OpenCv 时,出现错误
- 无法使用 OpenVino 推理编译 OpenCV,无法读取 cpuid.txt
- 在Debian上编译OpenCV(C++)时出错
- 如何使用Fast Downward编译OpenCV
- 使用CUDA编译OpenCV示例程序时出错
- C++ - 无法编译 OpenCV 程序
- 如何使用OpenMP编译OpenCV
- 使用CMake编译OpenCV项目时出错