如何正确链接ROS Indigo下的opencv3(使用CMake)?

时间:2016-06-01 06:36:52

标签: c++ cmake opencv3.0 ros catkin

无法将我自己安装的opencv3与ros分开。我收到此错误

../devel/lib/libirTest.so: undefined reference to `cv::ORB::create(int, float, int, int, int, int, int, int, int)'
../devel/lib/libirTest.so: undefined reference to `cv::calcOpticalFlowPyrLK(cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputOutputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::Size_<int>, int, cv::TermCriteria, int, double)'

但是,能够成功编译外部ROS;也就是说,使用没有ROS的类似包....这是我在ROS下使用的CMake文件(不起作用)

cmake_minimum_required(VERSION 2.8.3)
project(my_test_pkg)

SET(SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  rospy
  std_msgs
  message_generation
  genmsg
)
find_package(nodelet REQUIRED)

################################################
## Declare ROS messages, services and actions ##
################################################

 add_message_files(
   FILES
    velocity_trsl.msg 
   )

  generate_messages(
    DEPENDENCIES
    std_msgs
    )

catkin_package(
  CATKIN_DEPENDS message_generation cv_bridge image_transport roscpp rospy std_msgs
)

###########
## Build ##
###########
include_directories(
  ${catkin_INCLUDE_DIRS}
  )


#***********************
#******   GSL
#***********************
find_package( PkgConfig REQUIRED)
pkg_check_modules( gsl REQUIRED gsl )

#***********************
#******   Boost
#***********************

SET (BOOST_DIR "/home/polar/soft/lib/boost/boost_1_61_0")

FIND_PACKAGE(Boost 1.61.0 REQUIRED thread)
if (NOT Boost_FOUND)
  message(FATAL_ERROR " Fatal error: Boost (version >= 1.55) required.")
else()
  message(STATUS "Setting up BOOST")
  message(STATUS " Includes - ${Boost_INCLUDE_DIRS}")
  message(STATUS " Library  - ${Boost_LIBRARY_DIRS}")
  include_directories(${Boost_INCLUDE_DIRS})
  link_directories(${Boost_LIBRARY_DIRS})
endif (NOT Boost_FOUND)   

#***********************
#******   OpenCV
#***********************
find_package( OpenCV 3.1.0 REQUIRED )
find_package( OpenCV 3.1.0 REQUIRED PATHS /home/polar/soft/lib/opencv/opencv-3.1.0/cmake)
if (NOT OpenCV_FOUND)
  message(FATAL_ERROR "*****!!!!! opencv NOT found.")
endif (NOT OpenCV_FOUND)    

#****************************************
include_directories(${SRC}/calculus)
SET(MY_LIB
  irTest
  )

#   ALL EXEC FILES...
SET(EXE_TESTER  
   tester1
    )

  add_executable(tester1 ${CMAKE_CURRENT_SOURCE_DIR}/exe-main-files-stdl/testdir/tester1.cpp )

  SET(FILES_2_RUN
    ${EXE_TESTER}
    )


#--------------------------
foreach(file2link ${FILES_2_RUN})
  target_link_libraries(${file2link}
    ${catkin_LIBRARIES}
    ${Boost_LIBRARIES}
    ${gsl_LIBRARIES}
    ${OpenCV_LIBRARIES}
    ${OpenCV_LIBS}
    ${MY_LIB}
    )

  add_dependencies(${file2link} my_test_pkg_generate_messages_cpp)# newlly added for messages!!
endforeach(file2link)  

add_subdirectory(src)

使用的opencv版本与ROS使用的(v2.4)之间是否存在冲突?也许cv_bridge或image_transport正在创建这个问题...(???)我甚至删除了build /和devel /并尝试再次编译,但总是同样的问题.....我从来没有花这么长时间比这个问题......

2 个答案:

答案 0 :(得分:3)

我能够在ROS indigo下将ROS项目与opencv 3.1相关联。请注意,opencv 2.4仍然是indigo下的默认版本,所以这很棘手,因此YMMV。

首先,您需要安装ros-indigo-opencv3个包。通过手动指定路径,源头路径可能会起作用,但我还没有测试过。

在您的CMakeLists.txt中,请确保包含${OpenCV_INCLUDE_DIRS}并与${OpenCV_LIBRARIES}相关联:

find_package(OpenCV 3 REQUIRED)
include_directories(
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)
add_library(your_node src/your_node.cpp)
target_link_libraries(your_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} )

最后,由于ros opencv绑定仅针对opencv 2.4进行编译,因此您需要在编译之前将vision_opencv存储库(https://github.com/ros-perception/vision_opencv)克隆到工作区中。这将确保绑定链接与opencv 3。

答案 1 :(得分:1)

我同时使用Opencv2.4.8opencv3.1 ros indigoopencv3.1从主目录下的源安装/home/xxx/opencv3_installopencv2.4.8

优点是使用2.4.8的旧版软件包不需要更改任何代码,因为默认的opencv版本为opencv3.1

当需要CMAKE_PREFIX_PATH时,应设置set(CMAKE_PREFIX_PATH "/home/xxx/opencv3_install") find_package(OpenCV 3.1 REQUIRED) set(OpenCV_INCLUDE_DIRS "/home/xxx/opencv3_install/include") 以查找lib。

cv_bridge

此外,安装opencv3.1后需要重新编译#run.config #<?xml version="1.0" encoding="utf-8"?> #<runmonth>201601</runmonth> $actconfigpath = 'C:\Users\run.config' #reportingmonth.txt #201512 $rptmonth = Get-Content 'C:\Users\reportingmonth.txt' -First 1 [xml]$xml = Get-Content $actconfigpath $node = $xml.runmonth $xml.runmonth.Replace($node.ToString(),$rptmonth.toString()) $xml.Save($actconfigpath)