3

阿木币

0

精华

183 小时

在线时间

技术大V

Rank: 4

发表于 2020-12-31 10:21:02 3009 浏览 0 回复

[入门教程] ros+opencv之简单入门

本帖最后由 chasing 于 2020-12-31 10:22 编辑

ROS和Opencv的融合,感觉还是各自干各自的活,只不过中间通过cv_bridge进行链接转换下。图片(视频流)的读取,既可以采用ROS的方式,也可以采用 opencv直接读取。方便了和ROS其他包的扩展,也就不用考虑程序结构的实现了,确实是“不用重复造轮子”了。
程序代码如下:


  1. #include <ros/ros.h>
  2. #include <cv_bridge/cv_bridge.h>

  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/highgui/highgui.hpp>

  5. int main(int argc,char** argv)
  6. {
  7.     ros::init(argc,argv,"image_test");
  8.     cv::Mat image;
  9.     cv::VideoCapture capture(0);
  10.     if(!capture.isOpened()){
  11.         ROS_ERROR("failed to open camera");
  12.         ros::shutdown();
  13.     }
  14.     image = cv::imread("/home/chasing/Templates/ros_tmp/src/opencvtest/src/ubuntu.png");

  15.     ROS_INFO_STREAM("opencv"<<image.cols<<"\t"<<image.rows);
  16.     cv::imshow("image",image);
  17.     cv::waitKey(0);
  18.     ros::NodeHandle nh;
  19.     ros::Publisher image_pub= nh.advertise<sensor_msgs::Image>("myopencv/image_raw",10);
  20.     cv::Mat edges;

  21.     ros::Rate rate(10);
  22.     while(ros::ok())
  23.     {

  24.         capture>>edges;
  25.         cv::imshow("image",edges);        
  26.         cv::waitKey(2);
  27.         image_pub.publish(cv_bridge::CvImage(std_msgs::Header(), "bgr8", edges).toImageMsg());
  28.         // cv::cvtColor(image,edges,CV_BGR2GRAY);
  29.         cv::namedWindow("gray window");
  30.         cv::imshow("gray window",image);
  31.         cv::waitKey(2);

  32.         ros::spinOnce();
  33.         rate.sleep();
  34.         ROS_INFO_STREAM_THROTTLE(1,"hello,opencv");
  35.     }

  36. }
复制代码

修改CMakeLists.txt如下:


  1. cmake_minimum_required(VERSION 2.8.3)
  2. project(opencvtest)

  3. ## Compile as C++11, supported in ROS Kinetic and newer
  4. # add_compile_options(-std=c++11)

  5. ## Find catkin macros and libraries
  6. ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
  7. ## is used, also find other catkin packages
  8. find_package(catkin REQUIRED COMPONENTS
  9.   cv_bridge
  10.   roscpp
  11.   std_msgs
  12. )

  13. find_package(OpenCV REQUIRED)

  14. ## System dependencies are found with CMake's conventions
  15. # find_package(Boost REQUIRED COMPONENTS system)


  16. ## Uncomment this if the package has a setup.py. This macro ensures
  17. ## modules and global scripts declared therein get installed
  18. ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
  19. # catkin_python_setup()

  20. ################################################
  21. ## Declare ROS messages, services and actions ##
  22. ################################################

  23. ## To declare and build messages, services or actions from within this
  24. ## package, follow these steps:
  25. ## * Let MSG_DEP_SET be the set of packages whose message types you use in
  26. ##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
  27. ## * In the file package.xml:
  28. ##   * add a build_depend tag for "message_generation"
  29. ##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
  30. ##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
  31. ##     but can be declared for certainty nonetheless:
  32. ##     * add a exec_depend tag for "message_runtime"
  33. ## * In this file (CMakeLists.txt):
  34. ##   * add "message_generation" and every package in MSG_DEP_SET to
  35. ##     find_package(catkin REQUIRED COMPONENTS ...)
  36. ##   * add "message_runtime" and every package in MSG_DEP_SET to
  37. ##     catkin_package(CATKIN_DEPENDS ...)
  38. ##   * uncomment the add_*_files sections below as needed
  39. ##     and list every .msg/.srv/.action file to be processed
  40. ##   * uncomment the generate_messages entry below
  41. ##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

  42. ## Generate messages in the 'msg' folder
  43. # add_message_files(
  44. #   FILES
  45. #   Message1.msg
  46. #   Message2.msg
  47. # )

  48. ## Generate services in the 'srv' folder
  49. # add_service_files(
  50. #   FILES
  51. #   Service1.srv
  52. #   Service2.srv
  53. # )

  54. ## Generate actions in the 'action' folder
  55. # add_action_files(
  56. #   FILES
  57. #   Action1.action
  58. #   Action2.action
  59. # )

  60. ## Generate added messages and services with any dependencies listed here
  61. # generate_messages(
  62. #   DEPENDENCIES
  63. #   std_msgs
  64. # )

  65. ################################################
  66. ## Declare ROS dynamic reconfigure parameters ##
  67. ################################################

  68. ## To declare and build dynamic reconfigure parameters within this
  69. ## package, follow these steps:
  70. ## * In the file package.xml:
  71. ##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
  72. ## * In this file (CMakeLists.txt):
  73. ##   * add "dynamic_reconfigure" to
  74. ##     find_package(catkin REQUIRED COMPONENTS ...)
  75. ##   * uncomment the "generate_dynamic_reconfigure_options" section below
  76. ##     and list every .cfg file to be processed

  77. ## Generate dynamic reconfigure parameters in the 'cfg' folder
  78. # generate_dynamic_reconfigure_options(
  79. #   cfg/DynReconf1.cfg
  80. #   cfg/DynReconf2.cfg
  81. # )

  82. ###################################
  83. ## catkin specific configuration ##
  84. ###################################
  85. ## The catkin_package macro generates cmake config files for your package
  86. ## Declare things to be passed to dependent projects
  87. ## INCLUDE_DIRS: uncomment this if your package contains header files
  88. ## LIBRARIES: libraries you create in this project that dependent projects also need
  89. ## CATKIN_DEPENDS: catkin_packages dependent projects also need
  90. ## DEPENDS: system dependencies of this project that dependent projects also need
  91. catkin_package(
  92. #  INCLUDE_DIRS include
  93. #  LIBRARIES opencvtest
  94. #  CATKIN_DEPENDS cv_bridge roscpp std_msgs
  95. #  DEPENDS system_lib
  96. )

  97. ###########
  98. ## Build ##
  99. ###########

  100. ## Specify additional locations of header files
  101. ## Your package locations should be listed before other locations
  102. include_directories(
  103. # include
  104.   ${catkin_INCLUDE_DIRS}
  105. )

  106. include_directories(${OpneCV_INCLUDE_DIRS})

  107. ## Declare a C++ library
  108. # add_library(${PROJECT_NAME}
  109. #   src/${PROJECT_NAME}/opencvtest.cpp
  110. # )

  111. ## Add cmake target dependencies of the library
  112. ## as an example, code may need to be generated before libraries
  113. ## either from message generation or dynamic reconfigure
  114. # add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

  115. ## Declare a C++ executable
  116. ## With catkin_make all packages are built within a single CMake context
  117. ## The recommended prefix ensures that target names across packages don't collide
  118. add_executable(${PROJECT_NAME}_node src/opencvtest.cpp)

  119. ## Rename C++ executable without prefix
  120. ## The above recommended prefix causes long target names, the following renames the
  121. ## target back to the shorter version for ease of user use
  122. ## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
  123. # set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

  124. ## Add cmake target dependencies of the executable
  125. ## same as for the library above
  126. # add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

  127. ## Specify libraries to link a library or executable target against
  128. target_link_libraries(${PROJECT_NAME}_node
  129.    ${catkin_LIBRARIES}
  130. )
  131. target_link_libraries(${PROJECT_NAME}_node ${OpenCV_LIBRARIES})

  132. #############
  133. ## Install ##
  134. #############

  135. # all install targets should use catkin DESTINATION variables
  136. # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

  137. ## Mark executable scripts (Python etc.) for installation
  138. ## in contrast to setup.py, you can choose the destination
  139. # install(PROGRAMS
  140. #   scripts/my_python_script
  141. #   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  142. # )

  143. ## Mark executables for installation
  144. ## See http://docs.ros.org/melodic/api/ ... ng_executables.html
  145. # install(TARGETS ${PROJECT_NAME}_node
  146. #   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  147. # )

  148. ## Mark libraries for installation
  149. ## See http://docs.ros.org/melodic/api/ ... ding_libraries.html
  150. # install(TARGETS ${PROJECT_NAME}
  151. #   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  152. #   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  153. #   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
  154. # )

  155. ## Mark cpp header files for installation
  156. # install(DIRECTORY include/${PROJECT_NAME}/
  157. #   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  158. #   FILES_MATCHING PATTERN "*.h"
  159. #   PATTERN ".svn" EXCLUDE
  160. # )

  161. ## Mark other files for installation (e.g. launch and bag files, etc.)
  162. # install(FILES
  163. #   # myfile1
  164. #   # myfile2
  165. #   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
  166. # )

  167. #############
  168. ## Testing ##
  169. #############

  170. ## Add gtest based cpp test target and link libraries
  171. # catkin_add_gtest(${PROJECT_NAME}-test test/test_opencvtest.cpp)
  172. # if(TARGET ${PROJECT_NAME}-test)
  173. #   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
  174. # endif()

  175. ## Add folders to be run by python nosetests
  176. # catkin_add_nosetests(test)
复制代码

示例程序结果

                               
登录/注册后可看大图

这个图片是直接采用opencv进行读取摄像头和显示的。

                               
登录/注册后可看大图

这个图片是采用ros中的rqt工具采集ros话题进行显示的。
程序的话,熟悉ros和opencv的话,应该很快就可以看懂。



评分

参与人数 1阿木币 +2 收起 理由
amov_msq + 2

查看全部评分


扫一扫浏览分享
回复

使用道具 举报

返回列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表