使用准备条件:
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
4.进入src目录,并将下载包中的camera解压到此目录内
5.部署完成后就可以编译了,在workspace目录下执行命令
catkin_make
6.编译成功后,执行
source devel/setup.bash
这个自动生成的脚本文件设置了若干环境变量,从而使 ROS 能够找到你创建的功能包和新生成的可执行文件
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
这里面 std_msgs rospy roscpp 是我们通常一个最基本的C++包之中需要的依赖项。后面这些依赖项均可以通过配置 package.xml 进行更改
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
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv ;
CascadeClassifier face_cascade ;
bool showResult = true ;
ros::Publisher pub;
int frame_id = 0 ;
vector detectFaces(Mat frame) {
vector 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 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("/faces", 5);
ros::Subscriber sub = nh.subscribe("/camera/color/image_raw", 1, imageCB);
ros::spin();
}
4.修改package.xml,添加所需依赖项
catkin
roscpp
rospy
std_msgs
cv_bridge
image_transport
message_generation
sensor_msgs
realsense_camera
cv_bridge
image_transport
roscpp
rospy
std_msgs
sensor_msgs
message_runtime
realsense_camera
其中的realsense_camera就是我们要依赖的realsen_camera库
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}
)
6.最后最重要的是修改realsense的camera目录下的CMakeLists.txt文件,因为原文件并没有生成别的功能包所能使用的库,所以如果不修改,由于功能包间是独立的,client功能包将找不到realsense_camera功能包,从而编译错误。红字为添加的部分
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的节点,内容如下:
8.最后别忘了source devel/setup.bash。然后就可以启动啦
roslaunch client detectface.launch