Home - OpenCV的open-cv安装的tutorials下的installation英文教程installation in linux
目录
1. 安装依赖库
- required:gcc,g++, cmake, git,
- 安装方式
- 在线安装sudo apt-get install gcc g++,将会当前系统安装对应的gcc版本
- 直接下载gcc release版本,自定义安装目录,下载链接如下GCC Releases- GNU Project
- 源码编译特定版本:
-
# 下载gcc wget http://ftp.gnu.org/gnu/gcc/gcc-4.9.4/gcc-4.9.4.tar.gz cd gcc-4.9.4 # 安装gcc所需三个依赖:GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+ 非常重要步骤 ./contrib/download_prerequisites mkdir build && cd build mkdir install # 创建安装目录,不影响root用户的gcc版本 # 运行configure脚本,该脚本会检查系统配置、库、头文件等,并生成Makefile ../configure --prefix=$PWD/install --enable-checking=release --enable-languages=c,c++ --disable-multilib # 开始编译并安装 make -j4 && make install # 编辑~/.bashrc添加个人用户环境变量并配置库,更新LD_LIBRARY_PATH环境变量PATH export LD_LIBRARY_PATH=/xxx/install/gcc-4.9.4/lib64:/usr/local/gcc-4.9.4/lib export PATH=$PATH:/usr/local/gcc-4.9.4/bin #生效 source ~/.bashrc
- 源码下载地址Index of /gnu/gcc,编译发布地址 gcc_download
-
- 安装cmake:
- 在线安装:sudo apt-get install cmake
- 源码安装
- https://gitlab.kitware.com/cmake/cmake/-/blob/master/README.rst
$ ./bootstrap && make && sudo make install
$ mkdir cmake-build && cd cmake-build
$ ../cmake-source/bootstrap && make
- cmake下载链接https://cmake.org/download/
yum install -y openssl openssl-devel
wget https://github.com/Kitware/CMake/releases/download/v3.17.1/cmake-3.17.1.tar.gz
tar -zxvf cmake-3.17.1.tar.gz -C cmake_3_17
cd cmake-3.17.1
./bootstrap --help
./bootstrap --prefix=~/cmake --bindir=~/cmake/bin -- -DCMAKE_BUILD_TYPE:STRING=Release -D BUILD_CMake_SERVER:BOOL=ON #安装到指定目录,home目录cmake文件夹
make -j4
make install
2. 编译opencv
- 使用git方法下载:
- cd ~/opencv
- git clone https://github.com/opencv/opencv.git
- 下载链接https://opencv.org/releases/,下载源码opencv.4.5.x.zip, 解压到工作目录,
- x86_64架构下执行以下命令:
cd ~/opencv-4.5.x
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=/usr/local \ #指定安装目录,如何没有root权限,可以选择安装在home目录
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_INSTALL_LIBDIR=lib64 \
-DOPENCV_FORCE_3RDPARTY_BUILD=ON \ # 如果系统没有libpng,libzlib,libjpeg等第三方库,可选择编译
..
make -j8
make install # 或 sudo make install
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=./install \
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_INSTALL_LIBDIR=lib64 \
-DOPENCV_FORCE_3RDPARTY_BUILD=ON \
-DBUILD_DOCS=OFF \
-DBUILD_EXAMPLES=OFF \
-DWITH_IPP=OFF \
-DBUILD_TESTS=OFF ..
- aarch64架构下执行上述命令,若存在如下错误,
-- NEON is not supported by C++ compiler
-- Performing Test HAVE_CPU_FP16_SUPPORT (check file: cmake/checks/cpu_fp16.cpp)
-- Performing Test HAVE_CPU_FP16_SUPPORT - Failed
-- FP16 is not supported by C++ compiler
CMake Error at cmake/OpenCVCompilerOptimizations.cmake:546 (message):
Required baseline optimization is not supported: NEON
(CPU_BASELINE_REQUIRE=;NEON)
Call Stack (most recent call first):
cmake/OpenCVCompilerOptions.cmake:294 (include)
CMakeLists.txt:610 (include)
- 原因是因为操作系统,不支持neon的架构,CMake detects that the system not supports NEON,因此需重新指定cmake的选项-DENABLE_NEON=OFF,考虑到opencv的依赖库如果需要编译neon的源码,则会报错,如下,则可以使用-DWITH_WEBP=OFF屏蔽改第三方库的编译
/tmp/ccjoSsg1.s:341: Error: register number out of range 0 to 15 at operand 3 -- `smlal v0.4s,v5.4h,v18.h[0]'
make[2]: *** [3rdparty/libwebp/CMakeFiles/libwebp.dir/src/dsp/yuv_neon.c.o] Error
- 设置其他编译选项,把一些无关紧要的选项选择关闭,如下:
cmake .. \
-DBUILD_DOCS=OFF \
-DBUILD_EXAMPLES=OFF \
-DINSTALL_BIN_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_opencv_apps=OFF
-DWITH_IPP=OFF \
-DBUILD_IPP_IW=OFF \
-DWITH_LAPACK=OFF \
-DWITH_EIGEN=OFF \
3. 添加opencv的库目录
#用户环境变量配置
vim ~/.bashrc
#最后一行添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${install_path}
#保存退出,使其生效
source ~/.bashrc
4. demo 测试验证
- 可以在open-cv下建一个文件夹opencv_test,该文件下新建一个cpp文件和txt文件,如下: DisplayImage.cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
cv::namedWindow( "Display Image", CV::WINDOW_AUTOSIZE );
cv::imshow( "Display Image", image );
cv::waitKey(0);
return 0;
}
- 编写CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(Demo)
set(CMAKE_CXX_STANDARD 11)
set(OpenCV_DIR "~/opencv/build" CACHE PATH "Location of opencv libraries" FORCE)
if (NOT DEFINED OpenCV_DIR OR ${OpenCV_DIR} STREQUAL "")
message(FATAL_ERROR "please set OpenCV_DIR with -DOpenCV_DIR=/path/opencv")
endif()
find_package(OpenCV 4 REQUIRED )
add_executable(Demo DisplayImage.cpp)
target_include_directories(Demo PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(Demo ${OpenCV_LIBS})
终端执行 ./Demo 3wolfmoon.jpg
参考文献:
http://blog.csdn.net/zhangxb35/article/details/46973917