1、Cmake 的编译及安装
OPENCV的编译依赖Cmake,所以首先需要安装Cmake
通过命令行的方式安装(前提是需要Ubuntu联网)
ml@ml-virtual-machine:~$ sudo apt-get install cmake
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
cmake 已经是最新版 (3.22.1-1ubuntu1.22.04.1)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 300 个软件包未被升级。
版本信息
ml@ml-virtual-machine:~$ cmake --version
cmake version 3.22.1
2、安装依赖库
2.1 安装 opencv 依赖库
执行命令
ml@ml-virtual-machine:~$ sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff5-dev libswscale-dev pkg-config
【补充】:
显示图像 imshow(),正常显示几秒后报错:
Gtk-Message: 11:37:50.847: Failed to load module “canberra-gtk-module”
缺少库,执行以下命令安装:
ml@ml-virtual-machine:~$ sudo apt-get install libcanberra-gtk-module
2.2 安装 opencv_contrib 依赖库
执行命令
ml@ml-virtual-machine:~$sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev git
安装时若是没有产生报错,其他提示均可忽略
3 、Opencv 的编译及安装
3.1、下载 opencv并解压到 opencv-4.2.0目录
下载官网:https://github.com/opencv/opencv
3.2、进入opencv-4.2.0目录,进行如下操作
ml@ml-virtual-machine:~/opencv-4.2.0$ mkdir build && cd build
进入已经创建的文件目录build
3.3、配置编译选项
ml@ml-virtual-machine:~/opencv-4.2.0/build$ cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ../
成功会产生如下信息
– Build files have been written to: /home/ml/opencv-4.2.0/build
3.4、编译
ml@ml-virtual-machine:~/opencv-4.2.0/build$ make
时间较长,需要耐心等待,直到进度条达到 100%
3.5、 安装
ml@ml-virtual-machine:~/opencv-4.2.0/build$ sudo make install
执行下列命令
ml@ml-virtual-machine:~/opencv-4.2.0/build$ ls /usr/local/opencv4/
结果为 in include lib share
3.6、 测试
直接输入下面命令进入到 opencv-4.2.0/samples/cpp/example_cmake目录
ml@ml-virtual-machine:~/opencv-4.2.0/build$ cd ..
ml@ml-virtual-machine:~/opencv-4.2.0$ cd samples/
ml@ml-virtual-machine:~/opencv-4.2.0/samples$ cd cpp/
ml@ml-virtual-machine:~/opencv-4.2.0/samples/cpp$ cd example_cmake/
ml@ml-virtual-machine:~/opencv-4.2.0/samples/cpp/example_cmake$
ml@ml-virtual-machine:~/opencv-4.2.0/samples/cpp/example_cmake$ cmake ./
ml@ml-virtual-machine:~/opencv-4.2.0/samples/cpp/example_cmake$ make
[ 50%] Building CXX object CMakeFiles/opencv_example.dir/example.cpp.o
[100%] Linking CXX executable opencv_example
[100%] Built target opencv_example
3.7、 运行示例
在刚刚的目录下:(源码目录 samples/cpp/example_cmake)
ml@ml-virtual-machine:~/opencv-4.2.0/samples/cpp/example_cmake$ ./opencv_example
Built with OpenCV 4.7.0-dev
[ WARN:0@0.002] global cap_v4l.cpp:982 open VIDEOIO(V4L2:/dev/video0): can't open camera by index
[ERROR:0@0.002] global obsensor_uvc_stream_channel.cpp:156 getStreamChannelGroup Camera index out of range
No capture
^C
即使没有摄像头打开,但是有如下界面,则 OPENCV 安装成功!
4、OPENCV_CONTRIB 的编译安装
4.1 下载 opencv
同上,在在bulid下直接进行下面操作
官网下载对应版本:opencv_contrib-4.2.0.zip
下载:https://github.com/opencv/opencv_contrib
4.2、解压移动
解压 contrib,并移动到 opencv 目录下(或者不用使用命令直接解压提取到opencv-4.2.0下)
ml@ml-virtual-machine:~/下载$ unzip opencv_contrib-4.2.0.zip
ml@ml-virtual-machine:~/下载$ mv opencv_contrib-4.2.0 ~/opencv-4.2.0
4.3、配置编译项
先将之前编译 opencv 的 build 目录清空,再配置
ml@ml-virtual-machine:~/下载$ cd ~/opencv-4.2.0
ml@ml-virtual-machine:~/opencv-4.2.0$ cd build
ml@ml-virtual-machine:~/opencv-4.2.0/build$ rm * -rf
^[-- Configuring done
– Generating done
– Build files have been written to: /home/ml/opencv-4.2.0/build
编译成功
4.4、编译
在bulid下直接进行编译
ml@ml-virtual-machine:~/opencv-4.2.0/build$ make
4.5 安装
ml@ml-virtual-machine:~/opencv-4.2.0/build$ sudo make install