opencv编译运行demo碰到的问题

代码下载编译安装

  1. $ git clone https://github.com/opencv/opencv.git
  2. $ cd opencv
  3. $ mkdir build
  4. $ cd build
  5. $ cmake ..
  6. $ make
  7. $ sudo make install

编译后demo运行出错

  1. > terminate called after throwing an instance of 'cv::Exception'
  2. what(): OpenCV(4.4.0-dev) /home/hui/data/study/opencv/modules/highgui/src/window.cpp:634: error: (-2:Unspecified error)
  3. The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support.
  4. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'

解决办法

cmake选项中加上:WITH_GTK=ON

sudo apt install libgtkglext1-dev

cmake后的输出

  1. -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
  2. -- Checking for module 'gtk+-3.0'
  3. -- Found gtk+-3.0, version 3.22.30
  4. -- Checking for module 'gtk+-2.0'
  5. -- Found gtk+-2.0, version 2.24.32
  6. -- Checking for module 'gthread-2.0'
  7. -- Found gthread-2.0, version 2.56.4
  8. -- Checking for module 'gtkglext-1.0'
  9. -- Found gtkglext-1.0, version 1.2.0

编译错误

  1. [ 44%] Linking CXX executable ../../bin/opencv_test_core
  2. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'
  3. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'
  4. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0'
  5. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
  6. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFWriteScanline@LIBTIFF_4.0'
  7. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFGetField@LIBTIFF_4.0'
  8. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0'
  9. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFWriteDirectory@LIBTIFF_4.0'
  10. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadEncodedTile@LIBTIFF_4.0'
  11. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadRGBATile@LIBTIFF_4.0'
  12. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFClose@LIBTIFF_4.0'
  13. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFClientOpen@LIBTIFF_4.0'
  14. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFRGBAImageOK@LIBTIFF_4.0'
  15. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFOpen@LIBTIFF_4.0'
  16. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadEncodedStrip@LIBTIFF_4.0'
  17. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFSetField@LIBTIFF_4.0'
  18. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFSetWarningHandler@LIBTIFF_4.0'
  19. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0'
  20. [ 52%] Building CXX object modules/ts/CMakeFiles/opencv_ts.dir/src/ocl_test.cpp.o
  21. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'
  22. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'
  23. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0'
  24. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
  25. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFWriteScanline@LIBTIFF_4.0'
  26. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFGetField@LIBTIFF_4.0'
  27. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0'
  28. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFWriteDirectory@LIBTIFF_4.0'
  29. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadEncodedTile@LIBTIFF_4.0'
  30. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadRGBATile@LIBTIFF_4.0'
  31. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFClose@LIBTIFF_4.0'
  32. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFClientOpen@LIBTIFF_4.0'
  33. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFRGBAImageOK@LIBTIFF_4.0'
  34. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFOpen@LIBTIFF_4.0'
  35. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFReadEncodedStrip@LIBTIFF_4.0'
  36. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFSetField@LIBTIFF_4.0'
  37. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFSetWarningHandler@LIBTIFF_4.0'
  38. ../../lib/libopencv_imgcodecs.so.5.0.0: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0'

cmake选项中加上:BUILD_TIFF=ON

最后的cmake配置项如下

  1. cmake -D CMAKE_BUILD_TYPE=RELEASE \
  2. -D CMAKE_INSTALL_PREFIX=/usr/local \
  3. -D INSTALL_PYTHON_EXAMPLES=ON \
  4. -D INSTALL_C_EXAMPLES=ON \
  5. -D WITH_GTK=ON \
  6. -D BUILD_TIFF=ON ..

build后还是报错

  1. terminate called after throwing an instance of 'cv::Exception'
  2. what(): OpenCV(4.4.0-dev) /home/hui/data/study/opencv/modules/highgui/src/window.cpp:634: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'

找到opencv的库,看了下make install没有更新,然后删除掉/usr/local/lib下的opencv相关库后重新make install

  1. $ cd /usr/local/lib
  2. $ ls -l libopencv_core.so.4.4.0
  3. -rw-r--r-- 1 root root 18073456 8 31 2020 libopencv_core.so.4.4.0
  4. $ rm libpencv*

在demo中cmake后,OpenCV version变成4.5.3,成功了!

  1. $ cmake ..
  2. -- The C compiler identification is GNU 7.5.0
  3. -- The CXX compiler identification is GNU 7.5.0
  4. -- Check for working C compiler: /usr/bin/cc
  5. -- Check for working C compiler: /usr/bin/cc -- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Detecting C compile features
  9. -- Detecting C compile features - done
  10. -- Check for working CXX compiler: /usr/bin/c++
  11. -- Check for working CXX compiler: /usr/bin/c++ -- works
  12. -- Detecting CXX compiler ABI info
  13. -- Detecting CXX compiler ABI info - done
  14. -- Detecting CXX compile features
  15. -- Detecting CXX compile features - done
  16. -- Found OpenCV: /usr/local (found suitable version "4.5.3", minimum required is "4.0.0")
  17. OpenCV version : 4.5.3
  18. -- Configuring done
  19. -- Generating done
  20. -- Build files have been written to: /home/hui/data/Learn-OpenCV-4/Poisson-fusion/build

使用demo是Learn-OpenCV-4,openCV 4计算机视觉项目实战一本书的例子:

https://github.com/PacktPublishing/Learn-OpenCV-4-By-Building-Projects-Second-Edition.git

在opencv的目录下也有sample:

opencv/samples/cpp/tutorial_code/

sample对应的图片资源需要单独下载

https://github.com/opencv/opencv\_extra.git

比如使用seamless_cloning的sample代码, copy 前面demo的一个CMakeLists.txt改改就可以用。

  1. $ cd opencv/samples/cpp/tutorial_code/photo/seamless_cloning
  2. $ mkdir build
  3. $ cmake ..
  4. $ make

生成clone-demo,然后copy opencv_extra对应的资源目录到buld下, 就可以运行sample-demo了。

  1. $ cp -rf opencv_extra/testdata/cv/cloning opencv/samples/cpp/tutorial_code/photo/seamless_cloning/build/.

附CMakeLists.txt

  1. cmake_minimum_required (VERSION 3.0)
  2. PROJECT(poisson-fusion)
  3. set (CMAKE_CXX_STANDARD 11)
  4. IF(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
  5. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
  6. conan_basic_setup()
  7. ENDIF()
  8. # Requires OpenCV
  9. FIND_PACKAGE( OpenCV 4.0.0 REQUIRED )
  10. MESSAGE("OpenCV version : ${OpenCV_VERSION}")
  11. ADD_EXECUTABLE(poisson-fusion main.cpp )
  12. TARGET_LINK_LIBRARIES( poisson-fusion ${OpenCV_LIBS} ${CONAN_LIBS} -lprotobuf )

发表评论

表情:
评论列表 (有 0 条评论,70人围观)

还没有评论,来说两句吧...

相关阅读

    相关 最近碰到问题

    最近碰到的问题,包罗万象,同时欢迎各位朋友们能提供这种迷你知识点。 问题1 MySQL报错MySQL“ERROR 1046(3D000):未选择数据库”更新查询 ![