OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。
OpenCV用C++语言编写,它的主要接口也是C++语言,但是依然保留了大量的C语言接口。该库也有大量的Python、Java and MATLAB/OCTAVE(版本2.5)的接口。这些语言的API接口函数可以通过在线文档获得。如今也提供对于C#、Ch、Ruby,GO的支持。
——摘自百度
首先,要更新 Ubuntu 的库
sudo apt-get update
sudo apt-get upgrade
然后安装一些 OpenCV 会用的库(这一步一定要认真做,否则后面会出现各种莫名其妙的错误!!!)
sudo apt-get install build-essential cmake libgtk2.0-dev libtiff4-dev libjasper-dev libavformat-dev libswscale-dev libavcodec-dev libjpeg62-dev pkg-config ffmpeg
在这一步中可能会遇到报错:
没有可用的软件包 libtiff4-dev,但是它被其它的软件包引用了。
这可能意味着这个缺失的软件包可能已被废弃,
或者只能在其他发布源中找到
然而下列软件包会取代它:
libtiff5-dev:i386 libtiff5-dev
解决方案是(其实是随便选了第二个…):
sudo apt-get install libtiff5-dev
尤其是 libgtk2.0-dev 和 pkg-config 这两个库,否则在后面运行例程时会报错:
terminate called after throwing an instance of ‘cv::Exception’
what(): OpenCV(3.4.5) /home/cxy/文档/opencv-3.4.5/modules/highgui/src/window.cpp:617: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvNamedWindow’
如果忘记了装上面的某个库,在补装了以后要重新执行第4部分中的内容才可以正常编译工程。
直接从官网提供的链接下载就行:OpenCV source download
下载解压后放在合适的位置,进入解压目录,进行如下操作,为OpenCV的编译作准备:
mkdir release
cd release
首先,设置 cmake 编译参数:
sudo cmake …
然后开始编译 OpenCV:
sudo make
sudo make install
注:编译第一步可能耗时比较久,建议找点其他事做!
网上可以查到很多种方法,这里提供一种亲测可行的方法,仅供参考:
sudo gedit /etc/ld.so.conf.d/opencv.conf
在文件 opencv.conf 内的末尾添加 “/usr/local/lib” 并保存;
更新系统的共享链接库:
sudo ldconfig
下一步:
sudo gedit /etc/bash.bashrc
在 bash.bashrc 的末尾添加 “export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig”
source /etc/bash.bashrc
至此,OpenCV环境搭建完毕(大功告成.jpg)。
一开始以为配好环境,写一个 cpp 文件就可以运行了,其实不然…
正解:
mkdir testcv
cd testcv
工程的名字根据个人喜好决定
创建cpp文件:
sudo gedit test.cpp
编写代码如下:
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include
using namespace cv;
using namespace std;
int main()
{
cout << "Hello OpenCV " << CV_VERSION << endl;
// Load image
Mat myMat = imread("test.jpg", 1);
// Create a window
cvNamedWindow("Opencv Image", WINDOW_AUTOSIZE);
// Show the image
imshow("Opencv Image", myMat);
// Wait 5000 ms
waitKey(5000);
return 0;
}
以下内容,直接复制粘贴的(传送门)
查看 opencv-3.4.5/samples/cpp/example_cmake 路径下的 CMakeLists.txt:
这个是 cmake 的编译文件,主要用于生成 Makefile,然后用 make 编译工程
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# 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 " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})
将其中的一部分按照自己的情况修改:
# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)
# Define project name 定义工程名
project(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)
# Declare the executable target built from your sources 声明可执行目标文件及源文件
add_executable(example example.cpp) # 目标文件,源文件0,源文件1,...
# Link your application with OpenCV libraries 将目标文件与库链接
target_link_libraries(example ${OpenCV_LIBS}) # 目标文件,库路径
这里附上自己修改过的 CMakeLists.txt 文件:
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(testcv)
# 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 " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(test test.cpp)
# Link your application with OpenCV libraries
target_link_libraries(test ${OpenCV_LIBS})
生成Makefile:
cmake .
编译工程:
make
运行可执行文件:
./test
1.OpenCV 环境搭建
2.Ubuntu 创建 OpenCV 工程