Compile and install OpenCV3 on Ubuntu

 

table of Contents

background

Install dependencies

Install ffmpeg

Compile and install opencv3

Ideal situation

Error handling

Copy header file

Modify the header file

Modify OpenCV file

Modify file method

A better solution-change the OpenCV version

3.4.1 version of OpenCV and OpenCV_contrib

Download xfeatures2d source file

Modify OpenCV file

Compile and install

Configuration

test

Conclusion


background

Record the process of installing OpenCV3 on Ubuntu 18.04 64-bit a while ago. If you are lucky, it is very cumbersome, just for reference.

Install dependencies

root@sundata:/etc/apt/sources.list.d# apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libx264-dev libx265-dev
root@sundata:/etc/apt/sources.list.d# apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev ffmpeg libavformat-dev libavcodec-dev libswresample-dev libswscale-dev libavutil-dev libsdl1.2-dev yasm

Install ffmpeg

root@sundata:/data/szc# git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
root@sundata:/data/szc# cd ffmpeg/
root@sundata:/data/szc/ffmpeg# ./configure   --enable-shared  --prefix=/usr/local/ffmpeg  --enable-gpl --enable-libx264  --enable-libx265
root@sundata:/data/szc/ffmpeg# make && make install

If an error is reported during compilation: V4L2_PIX_FMT_JPEG undeclared here, then modify the libavdevice/v4l2-common.h file and comment out the macro judgments around #include <linux/videodev2.h>

// #if HAVE_SYS_VIDEOIO_H
// #include <sys/videoio.h>
// #else
// #if HAVE_ASM_TYPES_H
// #include <asm/types.h>
// #endif
#include <linux/videodev2.h>
// #endif

have a test

root@sundata:/data/szc/ffmpeg# ldd ffmpeg
    linux-vdso.so.1 (0x00007ffd3fbb1000)
    libavdevice.so.58 => not found
    libavfilter.so.7 => not found
    libavformat.so.58 => not found
    libavcodec.so.58 => not found
    libpostproc.so.55 => not found
    libswresample.so.3 => not found
    libswscale.so.5 => not found
    libavutil.so.56 => not found
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7bf1a84000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7bf1865000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7bf1474000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7bf2068000)

root@sundata:/data/szc/ffmpeg# ffmpeg -version
ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil      55. 78.100 / 55. 78.100
libavcodec     57.107.100 / 57.107.100
libavformat    57. 83.100 / 57. 83.100
libavdevice    57. 10.100 / 57. 10.100
libavfilter     6.107.100 /  6.107.100
libavresample   3.  7.  0 /  3.  7.  0
libswscale      4.  8.100 /  4.  8.100
libswresample   2.  9.100 /  2.  9.100
libpostproc    54.  7.100 / 54.  7.100

Compile and install opencv3

Ideal situation

3.0.0 was originally installed. First download the source code package https://github.com/opencv/opencv/archive/3.0.0.zip , upload it to the server, and unzip it. Enter its directory, create a build folder and enter, execute the compilation script
(base) root@ubuntu:/home/szc/opencv/opencv-3.0.0/build# cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

If everything is normal, it will succeed and there will be a Makefile in the current directory

Then execute the installation command and everything goes well
(base) root@ubuntu:/home/szc/opencv/opencv-3.0.0/build# make -j6 && make install

The above process is normal on my virtual machine, and the screenshot after completion is shown below

 

Error handling

But I reported a bunch of errors on the server. The process I tried to solve is as follows:

Copy header file

Since many header files cannot be found during compilation, some third-party libraries must be installed, and then some header files should be copied to the /usr/local/include directory
root@sundata:/data/szc/opencv/opencv-3.0.0/build# apt-get install libpt-dev apcalc-dev linux-oracle-headers-4.15.0-1054 
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp -r /usr/include/x86_64-linux-gnu/libavformat /usr/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/calc/cmath.h /usr/local/include/
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/c++/v1/* /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /snap/gnome-3-34-1804/36/usr/include/c++/6/cmath /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/src/linux-oracle-headers-4.15.0-1054/include/uapi/linux/videodev2.h /usr/local/include/linux/
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/ptlib/videoio.h /usr/local/include/sys/
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/ptbuildopts.h /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp -r /usr/include/ptlib /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/ptlib.h /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /snap/gnome-3-34-1804/36/usr/include/c++/6/bits/cpp_type_traits.h /usr/local/include/bits
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp -r /usr/src/linux-hwe-5.4-headers-5.4.0-48/include/linux/* /usr/local/include/linux/
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp -r /usr/src/linux-hwe-5.4-headers-5.4.0-48/include/uapi /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /snap/gnome-3-34-1804/36/usr/lib/gcc/x86_64-linux-gnu/6/include/stdbool.h /usr/local/include
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/local/include/c++/6/ext/atomicity.h /usr/local/include/ext/

Note that libpjproject-dev seems to be mutually exclusive with libpt-dev, so copy the relevant files of the latter to the /usr/local/include directory before installing the latter

root@sundata:/data/szc/opencv/opencv-3.0.0/build# apt-get install libpjproject-dev
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/pjmedia/videodev.h /usr/local/include/linux/
root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp -r /usr/include/pj* /usr/local/include

Modify the header file

Some files have to be modified:

1), /usr/include/bits/cpp_type_traits.h, add #pragma once and macro judgment above and below extern "C++" {
#pragma once
#ifdef __cplusplus
extern "C++" {
.....

} // extern "C++"
#endif

....

2), /usr/local/include/wchar.h, add the macro definition of wchar_t to a certain line at the beginning

....
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif


#define int32 wchar_t


#include_next <wchar.h>
....

3), /usr/local/include/ptlib/unix/ptlib/contain.h, plus the header file

#include <wchar.h>
4) If an error is reported for the unknown type extern "C++", namespace, class, _LIBCPP_BEGIN_NAMESPACE_STD, etc., the only solution is to modify the file reported in the /usr/local/include directory:
  • Unknown type extern "C++": modify type_traits, cmath, use #ifdef __cplusplus .... #endif wrap extern "C++" {}
  • Unknown type using namespace std: modify ptlib/unix/ptlib/contain.h, use #ifdef __cplusplus .... #endif wrap using namespace std;
  •  Unknown type_LIBCPP_BEGIN_NAMESPACE_STD: modify atomic, cctype, cstring, cstddef, cstdio, cstdint, iosfwd, ios, iterator, cstdlib, cwctype, cwchar, memory, stdexcept, tuple, typeinfo, type_traits, utility, ___tuple, ___tuples, cstdarg, __locale, ostream, iostream, iomanip, map files, use #ifdef __cplusplus.... #endif to wrap _LIBCPP_BEGIN_NAMESPACE_STD ..... _LIBCPP_END_NAMESPACE_STD, in which the atomic header file #include <stdbool.h>, and Comment out #error <atomic> is not implemented; the stdexcept file is from the first _LIBCPP_BEGIN_NAMESPACE_STD to the last _LIBCPP_END_NAMESPACE_STD, basically at the end of the file
  • Unknown type namespace std: modify __nullptr, exception, initializer_list, typeinfo files, use #ifdef __cplusplus .... #endif to wrap all namespace std {}
  •  #include_next <stddef.h> Cannot find stddef.h file, modify the cstddef file, change #include_next <stddef.h> to #include <stddef.h>
  • Unknown type _LIBCPP_PUSH_MACROS: Modify __string, __bit_reference, __mutex_base, __threading_support, __split_buffer, __tree, string_view, string, streambuf, algorithm, memory, limits, locale, bitset, mutex, chrono, ratio, istream, sstream, vector, list files: Use #ifdef __cplusplus .... #endif to wrap _LIBCPP_PUSH_MACROS and _LIBCPP_BEGIN_NAMESPACE_STD ..... _LIBCPP_POP_MACROS
5) There are some files to be modified separately:
  • new file:
  1.  Use #ifdef __cplusplus .... #endif to wrap the content between #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME) ....#endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
  2. Use #ifdef __cplusplus .... #endif to wrap _LIBCPP_BEGIN_NAMESPACE_STD ..... _LIBCPP_END_NAMESPACE_STD
  • __functional_base file: Use #ifdef __cplusplus .... #endif to wrap _LIBCPP_BEGIN_NAMESPACE_STD ..... _LIBCPP_END_NAMESPACE_STD and constexpr
  • ext/atomicity.h file: Use #ifdef __cplusplus .... #endif to wrap the namespace
  • ptlib/ directory: There are a lot of files to be modified, you can download the modified ones directly from my network disk: link: https://pan.baidu.com/s/1guHSn6iVRGyrw8enXead5Q , extraction code: hsw3
  • sys/videoio.h file: Use #ifdef __cplusplus .... #endif to wrap class PColourConverter; ...... class PVideoFont: public PObject content between the two classes
  • class: modify the __debug file, wrap the class with #ifdef __cplusplus .... #endif _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception;

Modify OpenCV file

The following files are in the directory of opencv3.0.0

1), modules/cudev/include/opencv2/cudev/common.hpp, plus the header file
#include <cuda_fp16.h>

2)、cmake/FindCUDA.cmake

first of all:
  find_cuda_helper_libs(nppi)

Change to

  find_cuda_helper_libs(nppial)
  find_cuda_helper_libs(nppicc)
  find_cuda_helper_libs(nppicom)
  find_cuda_helper_libs(nppidei)
  find_cuda_helper_libs(nppif)
  find_cuda_helper_libs(nppig)
  find_cuda_helper_libs(nppim)
  find_cuda_helper_libs(nppist)
  find_cuda_helper_libs(nppisu)
  find_cuda_helper_libs(nppitc)

then:

  set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

Change to

  set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")

At last:

  unset(CUDA_nppi_LIBRARY CACHE)

Change to

  unset(CUDA_nppial_LIBRARY CACHE)
  unset(CUDA_nppicc_LIBRARY CACHE)
  unset(CUDA_nppicom_LIBRARY CACHE)
  unset(CUDA_nppidei_LIBRARY CACHE)
  unset(CUDA_nppif_LIBRARY CACHE)
  unset(CUDA_nppig_LIBRARY CACHE)
  unset(CUDA_nppim_LIBRARY CACHE)
  unset(CUDA_nppist_LIBRARY CACHE)
  unset(CUDA_nppisu_LIBRARY CACHE)
  unset(CUDA_nppitc_LIBRARY CACHE)

3)、cmake/OpenCVDetectCUDA.cmake

  if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0 2.1(2.0)")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    if(${CUDA_VERSION} VERSION_LESS "5.0")
      set(__cuda_arch_bin "3.0")
  else()
      set(__cuda_arch_bin "3.0 3.5")
  endif()

Change to

  if(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  elseif(CUDA_GENERATION STREQUAL "Maxwell")
    set(__cuda_arch_bin "5.0 5.2")

4)、cmake/OpenCVFindLibsVideo.cmake

first of all:
    
CHECK_INCLUDE_FILE(ffmpeg/avformat.h HAVE_FFMPEG_FFMPEG)
Change to
    
CHECK_INCLUDE_FILE(libavformat/avformat.h HAVE_FFMPEG_FFMPEG)
Secondly:
      
if(EXISTS /usr/include/ffmpeg/libavformat/avformat.h OR HAVE_FFMPEG_SWSCALE)
Change to
     
if(EXISTS /usr/local/include/libavformat/avformat.h OR HAVE_FFMPEG_SWSCALE)
5) Replace the cmake/OpenCVDetectCXXCompiler.cmake file with the following content (corresponding to the error CMake match cannot parse the expression *100+)
# ----------------------------------------------------------------------------
# Detect Microsoft compiler:
# ----------------------------------------------------------------------------
if(CMAKE_CL_64)
    set(MSVC64 1)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  set(CMAKE_COMPILER_IS_GNUCXX 1)
  set(CMAKE_COMPILER_IS_CLANGCXX 1)
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
  set(CMAKE_COMPILER_IS_GNUCC 1)
  set(CMAKE_COMPILER_IS_CLANGCC 1)
endif()
if("${CMAKE_CXX_COMPILER};${CMAKE_C_COMPILER}" MATCHES "ccache")
  set(CMAKE_COMPILER_IS_CCACHE 1)
endif()

# ----------------------------------------------------------------------------
# Detect Intel ICC compiler -- for -fPIC in 3rdparty ( UNIX ONLY ):
#  see  include/opencv/cxtypes.h file for related   ICC & CV_ICC defines.
# NOTE: The system needs to determine if the '-fPIC' option needs to be added
#  for the 3rdparty static libs being compiled.  The CMakeLists.txt files
#  in 3rdparty use the CV_ICC definition being set here to determine if
#  the -fPIC flag should be used.
# ----------------------------------------------------------------------------
if(UNIX)
  if  (__ICL)
    set(CV_ICC   __ICL)
  elseif(__ICC)
    set(CV_ICC   __ICC)
  elseif(__ECL)
    set(CV_ICC   __ECL)
  elseif(__ECC)
    set(CV_ICC   __ECC)
  elseif(__INTEL_COMPILER)
    set(CV_ICC   __INTEL_COMPILER)
  elseif(CMAKE_C_COMPILER MATCHES "icc")
    set(CV_ICC   icc_matches_c_compiler)
  endif()
endif()

if(MSVC AND CMAKE_C_COMPILER MATCHES "icc|icl")
  set(CV_ICC   __INTEL_COMPILER_FOR_WINDOWS)
endif()

# ----------------------------------------------------------------------------
# Detect GNU version:
# ----------------------------------------------------------------------------
if(CMAKE_COMPILER_IS_CLANGCXX)
  set(CMAKE_GCC_REGEX_VERSION "4.2.1")
  set(CMAKE_OPENCV_GCC_VERSION_MAJOR 4)
  set(CMAKE_OPENCV_GCC_VERSION_MINOR 2)
  set(CMAKE_OPENCV_GCC_VERSION 42)
  set(CMAKE_OPENCV_GCC_VERSION_NUM 402)

  execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} -v
                  ERROR_VARIABLE CMAKE_OPENCV_CLANG_VERSION_FULL
                  ERROR_STRIP_TRAILING_WHITESPACE)

  string(REGEX MATCH "version.*$" CMAKE_OPENCV_CLANG_VERSION_FULL "${CMAKE_OPENCV_CLANG_VERSION_FULL}")
  string(REGEX MATCH "[0-9]+\\.[0-9]+" CMAKE_CLANG_REGEX_VERSION "${CMAKE_OPENCV_CLANG_VERSION_FULL}")

elseif(CMAKE_COMPILER_IS_GNUCXX)
  execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
                OUTPUT_VARIABLE CMAKE_OPENCV_GCC_VERSION_FULL
                OUTPUT_STRIP_TRAILING_WHITESPACE)

  execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} -v
                ERROR_VARIABLE CMAKE_OPENCV_GCC_INFO_FULL
                OUTPUT_STRIP_TRAILING_WHITESPACE)

  # Typical output in CMAKE_OPENCV_GCC_VERSION_FULL: "c+//0 (whatever) 4.2.3 (...)"
  # Look for the version number, major.minor.build
  string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" CMAKE_GCC_REGEX_VERSION "${CMAKE_OPENCV_GCC_VERSION_FULL}")
  if(NOT CMAKE_GCC_REGEX_VERSION)#major.minor
    string(REGEX MATCH "[0-9]+\\.[0-9]+" CMAKE_GCC_REGEX_VERSION "${CMAKE_OPENCV_GCC_VERSION_FULL}")
  endif()

  if(CMAKE_GCC_REGEX_VERSION)
    # Split the parts:
    string(REGEX MATCHALL "[0-9]+" CMAKE_OPENCV_GCC_VERSIONS "${CMAKE_GCC_REGEX_VERSION}")

    list(GET CMAKE_OPENCV_GCC_VERSIONS 0 CMAKE_OPENCV_GCC_VERSION_MAJOR)
    list(GET CMAKE_OPENCV_GCC_VERSIONS 1 CMAKE_OPENCV_GCC_VERSION_MINOR)
  else()#compiler returned just the major version number
    string(REGEX MATCH "[0-9]+" CMAKE_GCC_REGEX_VERSION "${CMAKE_OPENCV_GCC_VERSION_FULL}")
    if(NOT CMAKE_GCC_REGEX_VERSION)#compiler did not return anything reasonable
      set(CMAKE_GCC_REGEX_VERSION "0")
      message(WARNING "GCC version not detected!")
    endif()
    set(CMAKE_OPENCV_GCC_VERSION_MAJOR ${CMAKE_GCC_REGEX_VERSION})
    set(CMAKE_OPENCV_GCC_VERSION_MINOR 0)
  endif()

  set(CMAKE_OPENCV_GCC_VERSION ${CMAKE_OPENCV_GCC_VERSION_MAJOR}${CMAKE_OPENCV_GCC_VERSION_MINOR})
  math(EXPR CMAKE_OPENCV_GCC_VERSION_NUM "${CMAKE_OPENCV_GCC_VERSION_MAJOR}*100 + ${CMAKE_OPENCV_GCC_VERSION_MINOR}")
  message(STATUS "Detected version of GNU GCC: ${CMAKE_OPENCV_GCC_VERSION} (${CMAKE_OPENCV_GCC_VERSION_NUM})")

  if(WIN32)
    execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpmachine
              OUTPUT_VARIABLE OPENCV_GCC_TARGET_MACHINE
              OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(OPENCV_GCC_TARGET_MACHINE MATCHES "amd64|x86_64|AMD64")
      set(MINGW64 1)
    endif()
  endif()
endif()

if(MSVC64 OR MINGW64)
  set(X86_64 1)
elseif(MINGW OR (MSVC AND NOT CMAKE_CROSSCOMPILING))
  set(X86 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
  set(X86_64 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i686.*|i386.*|x86.*|amd64.*|AMD64.*")
  set(X86 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm.*|ARM.*)")
  set(ARM 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)")
  set(AARCH64 1)
endif()

# Workaround for 32-bit operating systems on 64-bit x86_64 processor
if(X86_64 AND CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT FORCE_X86_64)
  message(STATUS "sizeof(void) = 4 on x86 / x86_64 processor. Assume 32-bit compilation mode (X86=1)")
  unset(X86_64)
  set(X86 1)
endif()

# Similar code exists in OpenCVConfig.cmake
if(NOT DEFINED OpenCV_STATIC)
  # look for global setting
  if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
    set(OpenCV_STATIC OFF)
  else()
    set(OpenCV_STATIC ON)
  endif()
endif()

if(MSVC)
  if(CMAKE_CL_64)
    set(OpenCV_ARCH x64)
  elseif((CMAKE_GENERATOR MATCHES "ARM") OR ("${arch_hint}" STREQUAL "ARM") OR (CMAKE_VS_EFFECTIVE_PLATFORMS MATCHES "ARM|arm"))
    # see Modules/CmakeGenericSystem.cmake
    set(OpenCV_ARCH ARM)
  else()
    set(OpenCV_ARCH x86)
  endif()
  if(MSVC_VERSION EQUAL 1400)
    set(OpenCV_RUNTIME vc8)
  elseif(MSVC_VERSION EQUAL 1500)
    set(OpenCV_RUNTIME vc9)
  elseif(MSVC_VERSION EQUAL 1600)
    set(OpenCV_RUNTIME vc10)
  elseif(MSVC_VERSION EQUAL 1700)
    set(OpenCV_RUNTIME vc11)
  elseif(MSVC_VERSION EQUAL 1800)
    set(OpenCV_RUNTIME vc12)
  elseif(MSVC_VERSION EQUAL 1900)
    set(OpenCV_RUNTIME vc14)
  elseif(MSVC_VERSION EQUAL 1910)
    set(OpenCV_RUNTIME vc15)
  endif()
elseif(MINGW)
  set(OpenCV_RUNTIME mingw)

  if(MINGW64)
    set(OpenCV_ARCH x64)
  else()
    set(OpenCV_ARCH x86)
  endif()
endif()

6) If an error is reported Requested modules not available: vtkRenderingOpenGL, then modify the /cmake/OpenCVDetectVTK.cmake file, and change

find_package(VTK QUIET COMPONENTS vtkRenderingOpenGL vtkInteractionStyle vtkRenderingLOD vtkIOPLY vtkFiltersTexture vtkRenderingFreeType vtkIOExport NO_MODULE)

Change to

find_package(VTK QUIET COMPONENTS vtkRenderingOpenGL2 vtkInteractionStyle vtkRenderingLOD vtkIOPLY vtkFiltersTexture vtkRenderingFreeType vtkIOExport NO_MODULE)

7) If an error is reported as unsupported gpu architecture compute_20, because my cuda is 10.0, I must first modify the cmake/OpenCVDetectCUDA.cmake file, and add Pascal after setting _generations

set(_generations "Fermi" "Kepler" "Pascal")

Then assume the computing power (6.0 and 6.1) corresponding to Pascal in the judgment branch of CUDA_GENERATION

  if(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  elseif(CUDA_GENERATION STREQUAL "Maxwell")
    set(__cuda_arch_bin "5.0 5.2")
  elseif(CUDA_GENERATION STREQUAL "Pascal")
    set(__cuda_arch_bin "6.0 6.1")

Finally, the CUDA architecture is specified when cmake is compiled

-D CUDA_GENERATION=Pascal

Modify file method

Finally, talk about the solutions to these problems:

1), a certain header file cannot be found (such as bits/cpp_type_traits.h), first locate where the file is
root@sundata:/home/sundata# locate bits/cpp_type_traits.h
/snap/gnome-3-34-1804/36/usr/include/c++/6/bits/cpp_type_traits.h
/usr/include/c++/3.4/bits/cpp_type_traits.h
/usr/include/c++/5/bits/cpp_type_traits.h
/usr/include/c++/7/bits/cpp_type_traits.h
/usr/local/include/c++/6/bits/cpp_type_traits.h

Then find one and copy it to the /usr/local/include directory (bits/cpp_type_traits.h is not found here, so copy it to the /usr/local/include/bits directory)

root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /snap/gnome-3-34-1804/36/usr/include/c++/6/bits/cpp_type_traits.h /usr/local/include/bits

If it cannot be found locally, it means that we have to add this file by installing a third-party library (such as sys/videoio.h). First use apt-file (install with apt-get if you don't have it) to check which third-party library this header file contains and the path where it is located after installation

root@sundata:/home/sundata# apt-file search videoio.h
libopencv-videoio-dev: /usr/include/opencv2/videoio.hpp
libopencv-videoio-dev: /usr/include/opencv2/videoio/videoio.hpp
libpt-dev: /usr/include/ptlib/videoio.h
opencv-doc: /usr/share/doc/opencv-doc/html/dd/de7/group__videoio.html
opencv-doc: /usr/share/doc/opencv-doc/html/df/d2c/tutorial_table_of_content_videoio.html

We must choose the header file, so only libpt-dev can choose. After we install with apt-get, copy this file from the path after installation to /usr/local/include (because sys/videoio.h cannot be found, so we have to copy it to usr/local/include/ sys/ directory)

root@sundata:/data/szc/opencv/opencv-3.0.0/build# cp /usr/include/ptlib/videoio.h /usr/local/include/sys/

2), a field cannot be found, it has been used before definition

Search the Internet to find out which file this field is in, and then locate the position of this file in the system. After checking the content, copy a file with this field, or modify it after copying.
For example, wchar_t, I found this field in the wchar.h header file after searching the Internet for a long time. I used the above method to copy wchar.h to the /usr/loca/include directory, but it was not easy because it was also in wchar.h. There is no wchar_t. So I looked at other wchar.h in the system (path is /snap/gnome-3-34-1804/36/usr/include/x86_64-linux-gnu/bits/wchar.h) and found that there is such a section words
/* The fallback definitions, for when __WCHAR_MAX__ or __WCHAR_MIN__
   are not defined, give the right value and type as long as both int
   and wchar_t are 32-bit types.  Adding L'\0' to a constant value
   ensures that the type is correct; it is necessary to use (L'\0' +
   0) rather than just L'\0' so that the type in C++ is the promoted
   version of wchar_t rather than the distinct wchar_t type itself.
   Because wchar_t in preprocessor #if expressions is treated as
   intmax_t or uintmax_t, the expression (L'\0' - 1) would have the
   wrong value for WCHAR_MAX in such expressions and so cannot be used
   to define __WCHAR_MAX in the unsigned case.  */

It says that if __WCHAR_MAX__ or __WCHAR_MIN__ is not defined, as long as wchar_t and int are both 32-bit types, __WCHAR_MAX__ or __WCHAR_MIN__ will be correctly assigned, and wchar_t has not been assigned in the upgraded C++ version. Clearly defined. Then I had to help it define wchar_t, 32-bit, so there is a modification to modify the second part of the header file

3)、unknown type name 'class'类:
The ones I can't find are C++ keywords such as class, namespace, and template, and the information printed out by the compiler can clearly find the g++ I configured. So I don't understand why it always uses c to compile C++ code, so it must report an error. So my solution is to use #ifdef __cplusplus .... #endif to wrap the content of the error keyword definition. The meaning of this macro judgment is to judge whether the current compiler uses the c++ compiler g++, and if it is, then execute it. The macro judges the content, otherwise it will not be executed
4), other
Just search the internet for everything else

A better solution-change the OpenCV version

If these inexplicable errors occur, the best solution is actually to upgrade OpenCV: If none of the above methods work, or an unresolvable error occurs (mine is to compile OpenCV 3.0.0 with 10.0 CUDA, which is not compatible), You can update the version of opencv. The following operations are performed without the above modifications, that is, from the beginning (after compiling and installing ffmpeg)

3.4.1 version of OpenCV and OpenCV_contrib

I cloned OpenCV 3.4.1 and OpenCV_contrib 3.4.1

Download xfeatures2d source file

Download some files (link: https://pan.baidu.com/s/1oT36HtuRiYPEgNbgJvrOqg  extraction code: gsg2) and copy them to the opencv_contrib-3.4.1/modules/xfeatures2d/src directory

Modify OpenCV file

1) Modify the CMakeLists.txt file in the root directory of OpenCV-3.4.1 and add it to line 576
...
include_directories("modules")

# ----------------------------------------------------------------------------
#  Path for build/platform -specific headers
# ----------------------------------------------------------------------------
....

2) Modify the CMakeLists.txt in the file opencv-3.4.1/modules/stitching directory and add a sentence:

include_directories("/home/rtlab/szc/opencv-3.4.1/modules/features2d/include/")

Compile and install

Then use the command line to compile and install

root@rtlab-computer:/home/rtlab/szc/opencv-3.4.1/build# make -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D ENABLE_PRECOMPILED_HEADERS=OFF -D CUDA_GENERATION=Pascal -D OPENCV_EXTRA_MODULES_PATH=/home/rtlab/szc/opencv_contrib-3.4.1/modules -D WITH_CUDA=ON -D WITH_OPENCL=ON ..
root@rtlab-computer:/home/rtlab/szc/opencv-3.4.1/build# make -j10 && make install

Compilation is very smooth at this time

Configuration

After installation, if you cannot run the test case, you must configure some environment variables
1), modify the /etc/ld.so.conf file, add a sentence
include /usr/local/lib

2), modify /etc/profile

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

3), enable

(base) root@ubuntu:/home/szc/opencv/opencv-3.0.0/build# ldconfig
(base) root@ubuntu:/home/szc/opencv/opencv-3.0.0/build# source /etc/profile

test

root@ubuntu:/home/szc/opencv/opencv-3.0.0/build# pkg-config opencv --modversion

If the version number can be output correctly, it will succeed

Conclusion

When choosing the OpenCV version to be compiled and installed, it is best to check whether your CUDA version is included in the cmake/OpenCVDetectCUDA.cmake file in github (for example, Pascal with a computing power of 6.X), so that you can avoid a lot of detours. In addition, some test cases in OpenCV cannot be run due to lack of data; others cannot be run for various reasons, but as long as the version number can be output correctly, it should indicate that our installation is successful.

Guess you like

Origin blog.csdn.net/qq_37475168/article/details/109894648