使用C ++的OpenCV不会编译

时间:2016-07-19 20:36:54

标签: c++ image opencv

我一直在关注使用OpenCV和C ++的this教程,并使用以下代码来读取图像。

#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 test test.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。 我该怎么做才能解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我使用CentOS 7时遇到了同样的问题(与你几乎一样的操作系统)

我能够编译的唯一方法是在OpenCV文档中使用these指令进行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在其文档中还有两种方法可以解决这个问题。