1.OpenCV2源码安装
安装依赖项
sudo apt-get install build-essential //build-essential是c语言的开发包,包含了gcc make gdb和libc函数库
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev //安装cmake git等依赖项
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
下载源码
https://opencv.org/releases/page/4/
解压源码
mkdir build //在opencv2.4.13新建build文件夹
cd build
cmake .. //用来生成makefile文件
make //makefile文件调用编译器
sudo make install //安装opencv在默认位置,usr/local/ bin、lib、include 存放文件
查看安装的版本
pkg-config opencv --modversion
2.OpenCV3和contrib源码安装
下载源码(两个的版本需要保证一样),解压
安装Cmake-gui // cmake图像化界面,方便操作,并不是必须的
sudo apt-get install camke-qt-gui
mkdir build
cd build
cmake-gui \不用gui,可以写修改cmakew文件 或者 指定参数,但我觉得这样都不太方便
点击configure 现实configure done 后 generate 等待 generate done
网络原因:IPPICV下载失败,可以提前下载好,地址:
打开,/home/zsm/source/opencv-4.0.0/3rdparty/ippicv下的 ippicv.cmake,将下面地址 修改为刚才保存的地址
例如:
"file:/home/lxd/source/
不打开nonfree开关,会导致:
OpenCV(3.4.3) /home/lxd/OperCV3.4/opencv-3.4.3/opencv_contrib-3.4.3/modules/xfeatures2d/src/sift.cpp:1205: error:The function/feature is not implemented
make
sudo make install
3.测试
#include <iostream>
#include <opencv2/opencv.hpp> //头文件
#include <opencv2/xfeatures2d.hpp>
using namespace cv; //包含cv命名空间
using namespace std;
int main()
{
//Create SIFT class pointer
Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
//读入图片
Mat img_1 = imread("1.png");
Mat img_2 = imread("2.png");
//Detect the keypoints
vector<KeyPoint> keypoints_1, keypoints_2;
f2d->detect(img_1, keypoints_1);
f2d->detect(img_2, keypoints_2);
//Calculate descriptors (feature vectors)
Mat descriptors_1, descriptors_2;
f2d->compute(img_1, keypoints_1, descriptors_1);
f2d->compute(img_2, keypoints_2, descriptors_2);
//Matching descriptor vector using BFMatcher
BFMatcher matcher;
vector<DMatch> matches;
matcher.match(descriptors_1, descriptors_2, matches);
//绘制匹配出的关键点
Mat img_matches;
drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_matches);
imshow("【match图】", img_matches);
//等待任意按键按下
waitKey(0);
}
重要:
OpenCV版本的切换:关键文件:OpenCVConfig.cmake。在opencv编译好后,所在目录中一般会有一个叫OpenCVConfig.cmake的文件,在安装目录里面
这个文件中指定了CMake要去哪里找OpenCV,其.h文件在哪里等,比如其中一行:
在find_package前面加上一句set,如下:
如果要用原来的opencv2,删掉这一句,只需find_package
cmake_minimum_required(VERSION 2.8) set(OpenCV_DIR "/usr/opencv3.4/share/OpenCV") find_package(OpenCV REQUIRED) include_directories(${OPenCV_INCLUDE_DIRS}) add_executable(opencvInstallTest opencv3-4Test.cpp) target_link_libraries(opencvInstallTest ${OpenCV_LIBS})
请仔细阅读!有问题评论。