1. 安装库
//软件源更新
sudo apt-get update
//升级本地所有安装包,最新系统可以不升级,版本过高反而需要降级才能安装
sudo apt-get upgrade
//升级树莓派固件,固件比较新或者是Ubuntu则不用执行
sudo rpi-update
//安装build-essential、cmake、git和pkg-config
sudo apt-get install build-essential cmakegit pkg-config
//安装jpeg格式图像工具包
sudo apt-get install libjpeg8-dev
//安装tif格式图像工具包
sudo apt-get install libtiff5-dev
//安装JPEG-2000图像工具包
sudo apt-get install libjasper-dev
//安装png图像工具包
sudo apt-get install libpng12-dev
//安装视频IO包
sudo apt-get install libavcodec-devlibavformat-dev libswscale-dev libv4l-dev
//依赖包
sudo apt-get install libgtk2.0-dev
//优化函数包
sudo apt-get install libatlas-base-devgfortran
2.编译源码
mkdir ~/opencv
下载源码opencv-3.2.0.tar.gz和扩展模块opencv_contrib-3.2.0.tar.gz 将两个包解压到opencv目录下
cd ~/opencv/opencv-3.2.0/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.2.0/modules \
-D BUILD_EXAMPLES=ON ..
make -j4
sudo make install
sudo ldconfig
sudo vim /etc/ld.so.conf.d/opencv.conf 加一行/usr/local/bin
2. 测试
编写test.cpp
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
Matsrc = imread("./1.jpg");
if(!src.data)
{
cout<< "Could not load the image" << endl;
}
namedWindow("InputImage",CV_WINDOW_AUTOSIZE);
imshow("InputImage",src);
waitKey(0);
return0;
}
g++ `pkg-config --cflags --libs opencv`test.cpp -o test
SSH不支持图形界面 须在远程桌面终端下运行./test , 效果如下
同时还编了一个扩展模块例子,查看是否可用,程序如下:
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
int main()
{
Mat src = imread("./1.jpg");
if (!src.data)
{
std::cout << "Could not load the image" << std::endl;
}
//创建SURF特征检测器,生成关键点
Ptr
vector
detector->detect(src, keypoints, Mat());
//绘制关键点
Mat KeyPointImg;
drawKeypoints(src, keypoints, KeyPointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
namedWindow("SURF", CV_WINDOW_AUTOSIZE);
imshow("SURF", KeyPointImg);
waitKey(0);
return 0;
}
同时Python下也可以用,在终端输入python,然后输入import cv2,没有报错表明环境搭建成功
至此,树莓派3b+ Opencv3.2.0 和扩展模块安装和配置完毕。