轉載地址:http://blog.csdn.net/may0324/article/details/50984752
使用準備條件:
ROS-indigo
intel Realsense攝像頭(我使用的依舊是R200)
確保已經正常安裝驅動,安裝方法見博文
http://blog.csdn.net/may0324/article/details/50981540
1.首先到github下載ros-realsense源碼包,該包包含已經定義好的packages和nodes
https://github.com/intel-ros/realsense
2.新建工作區目錄,如
- mkdir workspace
3.進入創建的工作區目錄,病創建文件目錄,命名爲src
- cd workspace
- mkdir src
5.部署完成後就可以編譯了,在workspace目錄下執行命令
- catkin_make
- source devel/setup.bash
7.完成後就可以執行包中自帶的launch文件打開攝像頭了
- roslaunch realsense_camera realsense_r200_nodelet_standalone_preset.launch
啓動後發現攝像頭燈亮起,表明在工作,然而並沒有什麼新奇的東西發生,這是爲什麼呢?
其實是因爲該節點只是在不斷的發佈消息(就是不同形式的圖像信息,如RGB,紅外,深度,點雲等),但是並沒有節點訂閱該消息,所以爲了觀看攝像頭拍攝的圖像,我們需要再寫一個節點訂閱該消息,並將圖像顯示出來,接着再進行人臉檢測等後續功能。爲了以後功能拓展方便,我們直接寫個新的功能包(package)。
這裏我們定義的場景是,該功能節點訂閱realsense發佈的圖像消息,解碼並顯示出來,同時調用OpenCV的haar分類器進行人臉檢測,並將檢測到的人臉封裝成消息發佈出去,所以這個節點本身是個訂閱者(subscriber),同時也是個發佈者(publisher)。
1.進入workspace的src目錄內,創建包目錄
- catkin_create_pkg client std_msgs rospy roscpp
2.進入創建的包client目錄中,創建msg文件夾,並創建需要發佈的人臉消息文件facebox.msg和faces.msg:
facebox.msg
uint16 top
uint16 left
uint16 width
uint16 height
faces.msg
facebox[] face_boxes
uint16 image_width
uint16 image_height
3. 進入client/src目錄中,創建主程序文件client.cpp,並寫入如下內容
- #include <ros/ros.h>
- #include <image_transport/image_transport.h>
- #include <cv_bridge/cv_bridge.h>
- #include <sensor_msgs/image_encodings.h>
- #include <opencv2/objdetect/objdetect.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <std_msgs/String.h>
- #include <client/faces.h>
- #include <client/facebox.h>
- using namespace std;
- using namespace cv ;
- CascadeClassifier face_cascade ;
- bool showResult = true ;
- ros::Publisher pub;
- int frame_id = 0 ;
- vector<Rect> detectFaces(Mat frame) {
- vector<Rect> faces;
- Mat bufferMat;
- cvtColor(frame, bufferMat, COLOR_BGR2GRAY);
- equalizeHist(bufferMat, bufferMat);
- face_cascade.detectMultiScale(bufferMat, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
- return faces;
- }
- void imageCB(const sensor_msgs::ImageConstPtr& msg) {
- cv_bridge::CvImagePtr cvPtr;
- try {
- cvPtr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
- } catch (cv_bridge::Exception& e) {
- ROS_ERROR("cv_bridge exception: %s", e.what());
- return;
- }
- vector<Rect> faces = detectFaces(cvPtr->image);
- client::faces faces_msg;
- client::facebox _facebox;
- faces_msg.image_width = cvPtr->image.cols;
- faces_msg.image_height = cvPtr->image.rows;
- for (int i = 0; i < faces.size(); i++) {
- _facebox.top = faces[i].y;
- _facebox.left = faces[i].x;
- _facebox.width = faces[i].width;
- _facebox.height = faces[i].height;
- faces_msg.face_boxes.push_back(_facebox);
- if (showResult)
- rectangle(cvPtr->image, faces[i], CV_RGB(100, 100, 255), 1);
- }
- frame_id++ ;
- pub.publish(faces_msg);
- if (showResult) {
- imshow("Live Feed", cvPtr->image);
- waitKey(3);
- }
- }
- int main(int argc, char **argv) {
- face_cascade.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml");
- ros::init(argc, argv, "client");
- ros::NodeHandle nh;
- nh.param("/client/showResult", showResult, true);
- pub = nh.advertise<client::faces>("/faces", 5);
- ros::Subscriber sub = nh.subscribe("/camera/color/image_raw", 1, imageCB);
- ros::spin();
- }
4.修改package.xml,添加所需依賴項
- <buildtool_depend>catkin</buildtool_depend>
- <build_depend>roscpp</build_depend>
- <build_depend>rospy</build_depend>
- <build_depend>std_msgs</build_depend>
- <build_depend>cv_bridge</build_depend>
- <build_depend>image_transport</build_depend>
- <build_depend>message_generation</build_depend>
- <build_depend>sensor_msgs</build_depend>
- <build_depend>realsense_camera</build_depend>
- <run_depend>cv_bridge</run_depend>
- <run_depend>image_transport</run_depend>
- <run_depend>roscpp</run_depend>
- <run_depend>rospy</run_depend>
- <run_depend>std_msgs</run_depend>
- <run_depend>sensor_msgs</run_depend>
- <run_depend>message_runtime</run_depend>
- <run_depend>realsense_camera</run_depend>
5.修改CMakeLists.txt,文件內容如下
- cmake_minimum_required(VERSION 2.8.3)
- project(client)
- ## Find catkin macros and libraries
- ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
- ## is used, also find other catkin packages
- find_package(catkin REQUIRED COMPONENTS
- cv_bridge
- image_transport
- roscpp
- rospy
- std_msgs
- sensor_msgs
- message_generation
- realsense_camera
- )
- find_package(OpenCV REQUIRED)
- ## System dependencies are found with CMake's conventions
- # find_package(Boost REQUIRED COMPONENTS system)
- ## Uncomment this if the package has a setup.py. This macro ensures
- ## modules and global scripts declared therein get installed
- ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
- # catkin_python_setup()
- ################################################
- ## Declare ROS messages, services and actions ##
- ################################################
- ## To declare and build messages, services or actions from within this
- ## package, follow these steps:
- ## * Let MSG_DEP_SET be the set of packages whose message types you use in
- ## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
- ## * In the file package.xml:
- ## * add a build_depend tag for "message_generation"
- ## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
- ## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
- ## but can be declared for certainty nonetheless:
- ## * add a run_depend tag for "message_runtime"
- ## * In this file (CMakeLists.txt):
- ## * add "message_generation" and every package in MSG_DEP_SET to
- ## find_package(catkin REQUIRED COMPONENTS ...)
- ## * add "message_runtime" and every package in MSG_DEP_SET to
- ## catkin_package(CATKIN_DEPENDS ...)
- ## * uncomment the add_*_files sections below as needed
- ## and list every .msg/.srv/.action file to be processed
- ## * uncomment the generate_messages entry below
- ## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
- ## Generate messages in the 'msg' folder
- add_message_files(
- FILES
- # Message1.msg
- facebox.msg
- faces.msg
- )
- ## Generate services in the 'srv' folder
- # add_service_files(
- # FILES
- # Service1.srv
- # Service2.srv
- # )
- ## Generate actions in the 'action' folder
- # add_action_files(
- # FILES
- # Action1.action
- # Action2.action
- # )
- ## Generate added messages and services with any dependencies listed here
- generate_messages(
- DEPENDENCIES
- std_msgs
- geometry_msgs
- )
- ################################################
- ## Declare ROS dynamic reconfigure parameters ##
- ################################################
- ## To declare and build dynamic reconfigure parameters within this
- ## package, follow these steps:
- ## * In the file package.xml:
- ## * add a build_depend and a run_depend tag for "dynamic_reconfigure"
- ## * In this file (CMakeLists.txt):
- ## * add "dynamic_reconfigure" to
- ## find_package(catkin REQUIRED COMPONENTS ...)
- ## * uncomment the "generate_dynamic_reconfigure_options" section below
- ## and list every .cfg file to be processed
- ## Generate dynamic reconfigure parameters in the 'cfg' folder
- # generate_dynamic_reconfigure_options(
- # cfg/DynReconf1.cfg
- # cfg/DynReconf2.cfg
- # )
- ###################################
- ## catkin specific configuration ##
- ###################################
- ## The catkin_package macro generates cmake config files for your package
- ## Declare things to be passed to dependent projects
- ## INCLUDE_DIRS: uncomment this if you package contains header files
- ## LIBRARIES: libraries you create in this project that dependent projects also need
- ## CATKIN_DEPENDS: catkin_packages dependent projects also need
- ## DEPENDS: system dependencies of this project that dependent projects also need
- catkin_package(
- #INCLUDE_DIRS include
- #LIBRARIES client
- #CATKIN_DEPENDS roscpp rospy std_msgs
- #DEPENDS system_lib
- CATKIN_DEPENDS message_runtime
- )
- ###########
- ## Build ##
- ###########
- ## Specify additional locations of header files
- ## Your package locations should be listed before other locations
- # include_directories(include)
- include_directories(
- ${catkin_INCLUDE_DIRS}
- ${OpenCV_INCLUDE_DIRS}
- )
- ## Declare a C++ library
- # add_library(client
- # src/${PROJECT_NAME}/client.cpp
- # )
- ## Add cmake target dependencies of the library
- ## as an example, code may need to be generated before libraries
- ## either from message generation or dynamic reconfigure
- # add_dependencies(client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
- ## Declare a C++ executable
- # add_executable(client_node src/client_node.cpp)
- ## Add cmake target dependencies of the executable
- ## same as for the library above
- # add_dependencies(client_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
- ## Specify libraries to link a library or executable target against
- # target_link_libraries(client_node
- # ${catkin_LIBRARIES}
- # )
- #############
- ## Install ##
- #############
- # all install targets should use catkin DESTINATION variables
- # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
- ## Mark executable scripts (Python etc.) for installation
- ## in contrast to setup.py, you can choose the destination
- # install(PROGRAMS
- # scripts/my_python_script
- # DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
- # )
- ## Mark executables and/or libraries for installation
- # install(TARGETS client client_node
- # ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
- # LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
- # RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
- # )
- ## Mark cpp header files for installation
- # install(DIRECTORY include/${PROJECT_NAME}/
- # DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
- # FILES_MATCHING PATTERN "*.h"
- # PATTERN ".svn" EXCLUDE
- # )
- ## Mark other files for installation (e.g. launch and bag files, etc.)
- # install(FILES
- # # myfile1
- # # myfile2
- # DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
- # )
- #############
- ## Testing ##
- #############
- ## Add gtest based cpp test target and link libraries
- # catkin_add_gtest(${PROJECT_NAME}-test test/test_client.cpp)
- # if(TARGET ${PROJECT_NAME}-test)
- # target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
- # endif()
- ## Add folders to be run by python nosetests
- # catkin_add_nosetests(test)
- add_executable(
- client
- src/client.cpp
- )
- target_link_libraries(
- client
- ${catkin_LIBRARIES}
- ${OpenCV_LIBS}
- )
find_package(PkgConfig REQUIRED)
add_service_files(
FILES
cameraConfiguration.srv
)
#add dynamic reconfigure api
generate_dynamic_reconfigure_options(
cfg/camera_params.cfg
)
#此段很重要,表明要生成別的功能包所能使用的庫
catkin_package(
# INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
)
include_directories(
${catkin_INCLUDE_DIRS}
)
add_library(realsense_camera src/realsense_camera_nodelet.cpp)
target_link_libraries(realsense_camera
${catkin_LIBRARIES}
/usr/local/lib/librealsense.so
)
add_dependencies(realsense_camera realsense_camera_generate_messages_cpp ${PROJECT_NAME}_gencfg)
7.全部修改完成後,編譯。在client目錄中新建launch目錄,並在該目錄中新建一個.launch文件,名字隨便起,內容則是同時啓動realsense_camera的節點和client的節點,內容如下:
- <launch>
- <arg name="mode" default="preset" />
- <arg name="enable_depth" default="true" />
- <arg name="enable_color" default="true" />
- <arg name="enable_pointcloud" default="true" />
- <arg name="enable_tf" default="true" />
- <node pkg="nodelet" type="nodelet" name="standalone_nodelet" args="manager" output="screen"/>
- <node pkg="nodelet" type="nodelet" name="RealsenseNodelet"
- args="load realsense_camera/RealsenseNodelet standalone_nodelet">
- <param name="mode" type="str" value="$(arg mode)" />
- <param name="enable_depth" type="bool" value="$(arg enable_depth)" />
- <param name="enable_color" type="bool" value="$(arg enable_color)" />
- <param name="enable_pointcloud" type="bool" value="$(arg enable_pointcloud)" />
- <param name="enable_tf" type="bool" value="$(arg enable_tf)" />
- </node>
- <node pkg="client" type="client" name="clientnode" output="screen">
- <param name="showResult" value="true"/>
- </node>
- </launch>
- roslaunch client detectface.launch