opencv 源码编译

 

1.首先安装ffmpeg
apt-get install ffmpeg
2.安装opencv对ffmeg的依赖
#  for Compiling OpenCV with ffmpeg support
apt-get install build-essential  cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

3.下载opencv源码并编译
>> cd ~/<my_working_directory>
    >> git clone https://github.com/opencv/opencv.git
    >> cd opencv
    >> git checkout 3.4.6
    >> cd ..
    >> git clone https://github.com/opencv/opencv_contrib.git
    >> cd opencv_contrib
    >> git checkout 3.4. 6
    
    >> cd ~/<my_working_directory>/opencv
    >> mkdir build
    >> cd build
    
    >> cmake -D WITH_CUDA=ON \
            -D CMAKE_BUILD_TYPE=Release \
            -D CMAKE_INSTALL_PREFIX=/usr/local \
            -D WITH_IPP=OFF \
            -D WITH_FFMPEG=ON  \
            -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
            -D BUILD_TESTS=OFF \
            -D BUILD_PERF_TESTS=OFF \
            -D INSTALL_PYTHON_EXAMPLES=OFF \
            -D INSTALL_C_EXAMPLES=OFF \
            -D PYTHON_EXECUTABLE=$(which python) \
            -D BUILD_EXAMPLES=OFF ..
(
cmake -D WITH_CUDA=ON  -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_IPP=OFF -D WITH_FFMPEG=ON  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D PYTHON_EXECUTABLE=$(which python) -D BUILD_EXAMPLES=OFF ..
)
    >> make -j$nproc
    >> make install

 注意: issue:

Issue:
/tmp/opencv-3.2.0/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this.
Solve:
对vec_math.hpp做如下修改(把203行和205行的 ::abs 也注释掉):

CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uchar, uchar)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, char, char)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, ushort, ushort)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, short, short)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, int, int)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uint, uint)
==>
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uchar, uchar)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, char, char)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, ushort, ushort)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, short, short)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, int, int)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uint, uint)
Issue:
/home/***/software/opencv_contrib/modules/rgbd/src/odometry.cpp:41:45:  fatal error: unsupported/Eigen/MatrixFunctions: No such file or directory
Solve:
cd /usr/include/eigen3/
sudo cp -r unsupported/ ..
sudo cp -r Eigen/ ..
Issue:
/opencv-3.4.1/modules/python/src2/cv2.cpp:22:40: fatal error: pyopencv_generated_include.h: No such file or directory
 #include "pyopencv_generated_include.h"
Solve:
在opencv文件夹下面运行下面代码
python ./modules/python/src2/gen2.py ./build/modules/python_bindings_generator ./build/modules/python_bindings_generator/headers.txt

Issue:

/software/opencv/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:42: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory
Solve:

cp -rf ../opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d  modules/features2d/include/opencv2/

Issue:

/software/opencv/modules/cudacodec/src/precomp.hpp:60:37: fatal error: dynlink_nvcuvid.h: No such file or directory
         #include <dynlink_nvcuvid.h>
sovle:

1. just don't compile opencv_cudacodec
2. 修改以下文件:
  ./opencv/modules/cudacodec/src/cuvid_video_source.hpp
./opencv/modules/cudacodec/src/video_decoder.hpp
./opencv/modules/cudacodec/src/frame_queue.hpp
./opencv/modules/cudacodec/src/video_parser.hpp
./opencv/modules/cudacodec/src/precomp.hpp
  将if CUDA_VERSION >= 9000 修改为: if CUDA_VERSION >= 9000 && CUDA_VERSION < 10000
#if CUDA_VERSION >= 9000 && CUDA_VERSION < 10000 #include <dynlink_nvcuvid.h> #else #include <nvcuvid.h> #endif
posted @ 2020-07-28 16:42  瞬_冷  阅读(675)  评论(0)    收藏  举报