一、下载opencv库源码

1、创建存放库的文件夹

mkdir ~/linux/mylib&&cd ~/linux/mylib

2、下载

wget https://codeload.github.com/opencv/opencv/tar.gz/3.4.1 -O opencv-3.4.1.tar.gz

3、解压&&进入解压后的文件夹

tar -xvf opencv-3.4.1.tar.gz&&cd opencv-3.4.1/



二、配置opencv参数

1、安装 cmake 和 cmake-gui 工具

sudo apt-get install cmake cmake-qt-gui cmake-curses-gui

2、创建编译和安装目录

mkdir build install&&cd build

3、打开图形化工具 cmake-gui

cmake-gui

(1)指定源码的路径和构造目录

opencv 移植到国产操作系统 opencv移植到arm板_linux

opencv 移植到国产操作系统 opencv移植到arm板_opencv 移植到国产操作系统_02

(2)配置CMake参数

opencv 移植到国产操作系统 opencv移植到arm板_开发板_03

(3)填写交叉编译器的路径,Finish

opencv 移植到国产操作系统 opencv移植到arm板_开发板_04

(4)Cmake配置完成

opencv 移植到国产操作系统 opencv移植到arm板_opencv 移植到国产操作系统_05


build 文件夹生成文件:

opencv 移植到国产操作系统 opencv移植到arm板_linux_06

(5)配置要编译的库、信息,指定依赖库的链接参数

搜索 CMAKE_EXE_LINKER_FLAGS ,添加上 -lpthread -lrt -ldl

opencv 移植到国产操作系统 opencv移植到arm板_opencv_07

(6)指定安装目录到新建的 install 目录

搜索CMAKE_INSTALL_PREFIX, /usr/local 改为 /home/pjw/linux/mylib/opencv-3.4.1/install

opencv 移植到国产操作系统 opencv移植到arm板_opencv_08

(7)改为正式版,优化速度

搜索CMAKE_BUILD_TYPE,将这个键的值改成Release

opencv 移植到国产操作系统 opencv移植到arm板_opencv_09

(8)生成 Makefile 等文件

点击 Configure,再点击 Generate 就可以生成。

opencv 移植到国产操作系统 opencv移植到arm板_linux_10



三、修改文件

因为 HAVE_PTHREAD 宏定义了 pthread 库。所以要添加宏定义才可以编译得过。

cd .. //返回opencv顶层目录
vim 3rdparty/protobuf/src/google/protobuf/stubs/common.cc
#define HAVE_PTHREAD

opencv 移植到国产操作系统 opencv移植到arm板_linux_11



四、编译和安装

//进入build文件夹编译
cd build/&&make -j16

//把库安装到创建的 install 目录&&查看安装目录 install
make install&&ls install/

opencv 移植到国产操作系统 opencv移植到arm板_opencv 移植到国产操作系统_12


opencv 移植到国产操作系统 opencv移植到arm板_linux_13



五、配置pkg-config环境变量

vim ~/.bashrc 
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:~/linux/mylib/opencv-3.4.1/install/lib/pkgconfig
source ~/.bashrc
//查看路径
pkg-config --libs --cflags opencv
//输出
-I/home/pjw/linux/mylib/opencv-3.4.1/install/include/opencv -I/home/pjw/linux/mylib/opencv-3.4.1/install/include -L/home/pjw/linux/mylib/opencv-3.4.1/install/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core



六、测试程序编写与编译

编写:cv_test.cpp

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
 
int main(){
    cv::Mat image = cv::imread("picture.png");
    cv::namedWindow("picture");
    cv::imshow("picture",image);
    cv::waitKey(5000);
    return 0;
}

编译:

arm-linux-gnueabihf-g++ -o cv_test cv_test.cpp `pkg-config --cflags --libs opencv`

查看生成的可执行文件属性,为ARM架构:

opencv 移植到国产操作系统 opencv移植到arm板_OpenCV_14



七、开发板上测试

1、参照文章在开发板根文件下挂载目录:

NFS服务开启与使用、目录挂载与卸载



2、把OpenCV的 install/lib 下的所有库,拷贝到文件系统下的 /lib 目录下。

//拷贝opencv的lib文件夹到nfs目录
cp -r ~/linux/mylib/opencv-3.4.1/install/lib ~/linux/nfs

开发板终端查看:

opencv 移植到国产操作系统 opencv移植到arm板_linux_15


//进入nfs挂载目录下的lib文件夹
cd ~/mountnfs/lib/
//拷贝所有文件到开发板 `lib` 下
cp * /lib/

可以看到,全部拷贝过来了。

opencv 移植到国产操作系统 opencv移植到arm板_OpenCV_16


3、把测试app拷贝过来:

cp -r ~/linux/project/opencvApp/cv_test ~/linux/nfs



4、执行测试app

./cv_test

报错:

OpenCV(3.4.1) Error: 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 cvNamedWindow, file /home/pjw/linux/mylib/opencv-3.4.1/modules/highgui/src/window.cpp, line 618
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.1) /home/pjw/linux/mylib/opencv-3.4.1/modules/highgui/src/window.cpp:618: error: (-2) 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
Aborted

这是因为没有GTK,跑不了UI,而代码中使用了opencv的窗口创建和显示函数,所以报错,就是以下2句代码:

cv::namedWindow("picture");  
cv::imshow("picture",image);