linux安装C++ opencv

1. 安装依赖

opencv中的一些图像、视频相关的功能需要一些依赖,因此在安装opencv之前需要先安装这些依赖;在使用apt安装相关依赖时,会出现无法安装的情况,这时可以用aptitude来降级安装。

名称 apt package 名称 功能
编译系统 build-essential cmake pkg-config 生成 OpenCV
图像库 libpng-dev libjpeg-dev 提供各类图像格式的编解码
OpenBLAS libopenblas-dev 利用 CPU 向量运算指令为大量算法提供加速。
Eigen3 libeigen3-dev 提供线性代数相关算法支持
Intel TBB libtbb-dev 在 Intel CPU 上提供高性能并发计算支持
FFMPEG libavcodec-dev libavformat-dev libswscale-dev 提供视频编解码能力
GStreamer libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev 提供流媒体处理能力
GTK libgtk-3-dev libcanberra-gtk-module libcanberra-gtk3-module 图形化用户界面

2. 安装opencv

2.1 从源码安装

参考:在 Linux 系统中编译安装 OpenCV - 知乎

2.2 直接安装

参考: OpenCV 的最简安装方式(Linux) - 知乎

3. 验证

新建一个文件,命名为:test.cc:

#include 
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

int main(int argc, char **argv)
{
	auto img_path = argv[1];
	cv::Mat img = cv::imread(img_path);

	if(img.empty())
	{
		std::cout<<"----------image read error!--------------"<

编写CMakeLists.txt:

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Define project name
project(opencv_example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(opencv_example test_cv.cc)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})

在同级目录下新建build文件夹,并进入,编译,运行:

mkdir build
cd build
cmake ..
make -j8
./opencv_example

如果没有报错,则说明安装成功。

若有报错,大部分情况都是因为缺少相应依赖,通过第1步中的方法安装即可。

你可能感兴趣的:(C++,opencv,c++,开发语言,opencv安装)