提问者:小点点

如何在Linux上用C++运行一个简单的OpenCV代码?


我用C++编写了一个简单的OpenCV代码,并在Linux中运行:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
int main()
{
    cv::Mat image;
    image = cv::imread("sajjad.jpg", 1);
 
    cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
    cv::imshow("Display Image", image);
    cv::waitKey(0);
    return 0;
}

出现以下错误:

opencv_test.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
 #include <opencv2/opencv.hpp>
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.

如何在Linux中安装OpenCV for C++?


共1个答案

匿名用户

1-安装依赖项:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libgtk2.0-dev 
sudo apt-get install pkg-config

2-编译OpenCV需要一个C++编译器。通常是g++/gcc或clang/llvm。OpenCV还使用了CMake构建配置工具:

sudo apt install -y g++
sudo apt-get install cmake
sudo apt-get install make

3-使用Git克隆存储库:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

4-创建生成目录:

mkdir -p build && cd build

5-配置-为首选生成系统生成生成脚本:

cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv

6-build-run实际编译过程。4是所用处理器核数。这部分可能需要几分钟。

make -j4

7-安装过程将文件复制到预定义位置,并执行次要修补:

sudo make install

8-创建一个CMake文件。源代码的名称是displayimage.cpp,例如:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

9-生成可执行文件。这一部分很简单,只需像使用cmake的任何其他项目一样进行:

cd <DisplayImage_directory>
cmake .
make

10-运行可执行文件:

./DisplayImage

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|linux|c++|运行|简单|opencv|代码)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?