ROS中opencv的使用

问题一:

ubuntu16.0中安装了ROS及其自带了Opencv3.3.1 所以没有再单独安装opencv。但是在使用opencv时提示找不到opencv库,报错如下:
CMake Error at CMakeLists.txt:6 (FIND_PACKAGE):
  By not providing "FindOpencv.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Opencv", but
  CMake did not find one.

  Could not find a package configuration file provided by "Opencv" with any
  of the following names:

    OpencvConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "Opencv" to CMAKE_PREFIX_PATH or set
  "Opencv_DIR" to a directory containing one of the above files.  If "Opencv"
  provides a separate development package or SDK, be sure it has been
  installed.

解决方案:

因为我们在所有目录中没有找到OpencvConfig.cmake文件, 只能找到OpenCVConfig.cmake文件。我们可以将这个文件名字更改为OpencvConfig.cmake。然后在CMakeLists.txt中加入Opencv_DIR的路径信息。OpenCVConfig.cmake文件出现的位置在

/opt/ros/kinetic/share/OpenCV-3.3.1-dev/下。更改完毕后在CMakeLists.txt中find_package命令前面加入下面命令:
set(Opencv_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev)
最后的CMakeLists.txt中的命令如下:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(undistort)

set(Opencv_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev)

FIND_PACKAGE(Opencv REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})

ADD_EXECUTABLE(${PROJECT_NAME} undistort_image.cpp)

TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})

参考:https://www.cnblogs.com/newneul/p/8364924.html

问题二:

解决上面问题后,编译时报错:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window.cpp, line 331 
terminate called after throwing an instance of 'cv::Exception'what():  /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow

解决方法:参数路径配置问题。

原来读取照片路径是当前目录下:string image_file = "./test.png";   需要改为绝对路径:string image_file = "/home/wei/文档/slam/homework/1-7/第四讲/源码/l4-2-undistort_image/test.png";

 

 

你可能感兴趣的:(opencv)