树莓派 安装 OpenCV 使用CMake 编译工程 最新版2015

时间:2021-09-28 02:09:40

一、安装make,cmake

sudo apt-get install make
sudo apt-get install cmake

二、下载deb包

这里下载libopencv_2.4.10.deb.zip(我不太好下载,折腾了好久最后是用手机流量才下载下来,电脑一直下载到一大半就停止不动了)

那个deb的作者还发了这个帖子,有问题可以去问

三、安装deb包

解压.deb.zip,复制到RPi上,到那个目录里,然后执行

sudo dpkg -i ./libopencv_2.4.10.deb

四、复制头文件

下载OpenCV2.4.10 Windows版,在win下安装,得到/build,/source目录。复制/build/include/*到/usr/local/include

(假设/build/include/已经复制到RPi上的./目录了)

sudo cp -r ./* /usr/local/include

说明一下deb和头文件的关系:deb里头是一些.so(动态链接库),在链接时用到;头文件在编译时用到。缺一不可。

五、安装ffmpeg

可以先试试看不做这个步骤,反正我在安装之前一直莫名其妙地报错,说

/tmp/ccDC21Dx.o: In function `cv::Mat::~Mat()':
a.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccDC21Dx.o: In function `cv::Mat::release()':
a.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x58): undefined reference to `cv::Mat::deallocate()'

安装之后正常(不知道是不是这个原因)

sudo apt-get install ffmpeg

六、新建测试工程源码

假设我们在目录./下做这个工程

1.新建a.cpp,内容如下:(主要是看看Mat能否正常初始化和析构,可以的话基本上就没问题了)

#include "opencv2/opencv.hpp"
using namespace cv; int main(){
Mat img;
return ;
}

2.新建CMakeLists.txt,内容如下:(这个文件名不可更改,用于cmake)

PROJECT(main)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})
TARGET_LINK_LIBRARIES(main opencv_core)
TARGET_LINK_LIBRARIES(main opencv_imgproc)
TARGET_LINK_LIBRARIES(main opencv_video)
TARGET_LINK_LIBRARIES(main opencv_highgui)
TARGET_LINK_LIBRARIES(main opencv_photo)
TARGET_LINK_LIBRARIES(main opencv_flann)
TARGET_LINK_LIBRARIES(main opencv_superres)
TARGET_LINK_LIBRARIES(main opencv_stitching)
TARGET_LINK_LIBRARIES(main opencv_ml)
TARGET_LINK_LIBRARIES(main opencv_video)
TARGET_LINK_LIBRARIES(main opencv_features2d)
TARGET_LINK_LIBRARIES(main opencv_calib3d)
TARGET_LINK_LIBRARIES(main opencv_objdetect)

七、编译测试工程

1.编译

mkdir ./build
cd ./build
cmake ..
make

如果没有错误,就会产生如下输出

pi@turtle ~/swap/proj $ mkdir build
pi@turtle ~/swap/proj $ cd ./build
pi@turtle ~/swap/proj/build $ cmake ..
-- The C compiler identification is GNU 4.6.
-- The CXX compiler identification is GNU 4.6.
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/swap/proj/build
pi@turtle ~/swap/proj/build $ make
Scanning dependencies of target main
[%] Building CXX object CMakeFiles/main.dir/a.cpp.o
Linking CXX executable main
[%] Built target main

2.运行

./main

应该如下:

pi@turtle ~/swap/proj/build $ ./main
pi@turtle ~/swap/proj/build $

对,确实没有输出,但是没有报错就是好事了,接下来就可以使用了!

八、(g++命令行)

g++ ./a.cpp -lopencv_core -lopencv_imgproc -lopencv_video -lopencv_highgui -lopencv_flann -lopencv_superres -lopencv_stitching -lopencv_ml -lopencv_features2d -lopencv_calib3d -lopencv_objdetect

(版权所有,转载注明出处http://www.cnblogs.com/turtlegood/

(本文结束)