ubuntu18编译opencv2.4.10源码

ubuntu18编译opencv2.4.10源码

  • 1. 配置
  • 2. opencv2.4.10安装
    • 2.1 下载配置
  • 3. 问题解决
  • 4. 应用

1. 配置

基本配置 版本号
CPU Intel® Core™ i5-8400 CPU @ 2.80GHz × 6
GPU GeForce RTX 2070 SUPER/PCIe/SSE2
OS Ubuntu18.04
openjdk 1.8.0_242
python 3.6.9
gcc/g++ 5.5
opencv 3.4
cmake 3.12.2
protobuf 3.0.0

hint:
我的Ubuntu环境为了满足大多demo需求,已经安装了opencv3.4.0版本,安装opencv2只是满足个别demo,所以再安装opencv2时要注意下面几点:
 1)安装的系统路径不能再是:/usr/local (opencv3已经占用,不能冲突)
 2)建议不要再安装opencv_python (opencv3已经支持)
3) !!!不建议使用安装opencv2的方法编译caffe, 折腾到最后make编译还会失败, 可以参考我的另一个博客https://blog.csdn.net/weixin_43056275/article/details/105790929 没有安装opencv2还是成功了.
 4) https://blog.csdn.net/u011681952/article/details/84661726, 这个博主说使用opencv2的cmake方式编译caffe会成功, 但我比较奇怪的是 先cmake 再 make不是连起来用的吗, 为什么还会分两种方式, 希望了解的人可以讲解下, 谢谢.

2. opencv2.4.10安装

2.1 下载配置

mkdir opencv24
cd opencv24
wget https://github.com/opencv/opencv/archive/2.4.10.zip
unzip 2.4.10.zip
cd opencv-2.4.10
mkdir build
cd build
cmake-gui

注意:
1)去掉BUIILD_opencv_python
2)去掉WITH_CUDA (有它编译出错,可根据自己情况决定)
3)更改CMAKE_INSTALL_PREFIX的Value值:/home/ying/usb/opencv2410 (默认为/usr/local,可根据自己喜好设置路径,但要记住放在那里的)
4)再次Generate,若无错,再Configure,关闭cmake-gui
5)编译

make all -j16
sudo make install 
  1. 成功
  2. 查看 (该目录就有contrib模块了)
ll /home/ying/usb/opencv2410/include/opencv2/

3. 问题解决

问题1:

CMake Error at cmake/OpenCVDetectCXXCompiler.cmake:85 (list):
  list GET given empty list
Call Stack (most recent call first):
  CMakeLists.txt:77 (include)


CMake Error at cmake/OpenCVDetectCXXCompiler.cmake:86 (list):
  list GET given empty list
Call Stack (most recent call first):
  CMakeLists.txt:77 (include)


CMake Error at cmake/OpenCVDetectCXXCompiler.cmake:89 (math):
  math cannot parse the expression: "*100 + ": syntax error, unexpected
  exp_TIMES, expecting exp_PLUS or exp_MINUS or exp_OPENPARENT or exp_NUMBER
  (1)
Call Stack (most recent call first):
  CMakeLists.txt:77 (include)

原因: Ubuntu 18.4不支持opencv-2.3.10自带的OpenCVDetectCXXCompiler.cmake
解决方法:
将OpenCVDetectCXXCompiler.cmake的内容替换为如下

# ----------------------------------------------------------------------------
# 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()

问题2:

CMake Error NOTFOUND CUDA_nppi_LIBRARY (ADVANCED)

解决方法:

文件cmake/OpenCVDetectCUDA.cmake
  46行  set(_generations "Fermi"  "kepler" "Maxwell" "Pascal")  中  "Fermi"  去掉
重新编译

问题3:
ffmpeg错误

[ 17%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/grfmt_exr.cpp.o
In file included from /home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/cap_ffmpeg_impl.hpp:60:0,
                 from /home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/cap_ffmpeg.cpp:45:
/home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/ffmpeg_codecs.hpp:104:7: error: ‘CODEC_ID_H264’ was not declared in this scope
     { CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
       ^~~~~~~~~~~~~
/home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/ffmpeg_codecs.hpp:104:7: note: suggested alternative: ‘AV_CODEC_ID_H264’
     { CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
       ^~~~~~~~~~~~~
       AV_CODEC_ID_H264
/home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/ffmpeg_codecs.hpp:105:7: error: ‘CODEC_ID_H264’ was not declared in this scope
     { CODEC_ID_H264, MKTAG('h', '2', '6', '4') },
       ^~~~~~~~~~~~~
/home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/ffmpeg_codecs.hpp:105:7: note: suggested alternative: ‘AV_CODEC_ID_H264’
     { CODEC_ID_H264, MKTAG('h', '2', '6', '4') },
       ^~~~~~~~~~~~~
       AV_CODEC_ID_H264
/home/ying/usb/opencv24/opencv-2.4.10/modules/highgui/src/ffmpeg_codecs.hpp:106:7: error: ‘CODEC_ID_H264’ was not declared in this scope
     { CODEC_ID_H264, MKTAG('X', '2', '6', '4') },
       ^~~~~~~~~~~~~

解决措施:

cmake-gui的时候将WITH_FFMPEG设置为OFF

问题4:

[ 96%] Building CXX object modules/contrib/CMakeFiles/opencv_contrib.dir/src/colortracker.cpp.o
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp: In member function ‘void cv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&, cv::Mat&, cv::Mat&, float, float, float):
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp:969:30: error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]
             if (&annotate_img!=NULL) {
                              ^
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp:1016:34: error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]
                 if (&annotate_img!=NULL) {
                                  ^
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp: In member function ‘cv::ChamferMatcher::Match* cv::ChamferMatcher::Matching::localChamferDistance(cv::Point, cv::Mat&, cv::Mat&, cv::ChamferMatcher::Template*, float):
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp:1111:25: error: the compiler can assume that the address of ‘orientation_img’ will never be NULL [-Werror=address]
     if (&orientation_img!=NULL) {
                         ^
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp:1111:5: warning: nonnull argument ‘orientation_img’ compared to NULL [-Wnonnull-compare]
     if (&orientation_img!=NULL) {
     ^~
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp: In member function ‘void cv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&, cv::Mat&, cv::Mat&, float, float, float):
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp:1016:17: warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare]
                 if (&annotate_img!=NULL) {
                 ^~
/home/ying/usb/opencv24/opencv-2.4.10/modules/contrib/src/chamfermatching.cpp:969:13: warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare]
             if (&annotate_img!=NULL) {
             ^~

解决措施:

找到opencv-2.4.10/build/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make文件,删掉-Werror=address,然后make clean后 make all -j16

问题5:
usr/include/c++/7/cstdlib:75:15: fatal error: stdlib.h: 没有那个文件或目录
解决方法:
cmake的时候加上下面的参数

cmake -DENABLE_PRECOMPILED_HEADERS=OFF ..

4. 应用

在caffe/CMakeLists.txt中,project(Caffe C CXX)之前加上:
 set(OpenCV_DIR “/home/ying/usb/opencv2410/share/OpenCV”)
 这样cmake没有问题, 但是make的时候还有问题.
 不建议使用安装opencv2的方法编译caffe, 可以参考我的另一个博客https://blog.csdn.net/weixin_43056275/article/details/105790929

你可能感兴趣的:(环境配置)