事前準備
ROS Kineticでのカメラ関係の利用方法を参考にカメラの画像が取得できる環境を構築する.
ノードのひな形を作る.
下記のコマンドを実行して,画像処理用ノードのひな形を作る.
(ここでは,cv_bridge_tutorialというパッケージ名にする.また,/catkin_ws/src以下に作成するものとする.)
catkin_create_pkg cv_bridge_tutorial cv_bridge image_transport roscpp sensor_msgs std_msgs |
catkin_create_pkg cv_bridge_tutorial cv_bridge image_transport roscpp sensor_msgs std_msgs
ソースファイルを作成する.
ここでは,emacsを使っているが,自身の好きなエディタで良い.
cd /catkin_ws/src
emacs cv_bridge_tutorial_node.cpp |
cd /catkin_ws/src
emacs cv_bridge_tutorial_node.cpp
以下のソースコードを貼り付ける.
#include
#include
#include
#include
#include
#include
static const std::string OPENCV_WINDOW = "Image window";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
public:
ImageConverter()
: it_(nh_)
{
// Subscrive to input video feed and publish output video feed
image_sub_ = it_.subscribe("/image_raw", 1, &ImageConverter::imageCb, this);
image_pub_ = it_.advertise("/image_converter/output_video", 1);
cv::namedWindow(OPENCV_WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(OPENCV_WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
// Draw an example circle on the video stream
if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));
// Update GUI Window
cv::imshow(OPENCV_WINDOW, cv_ptr->image);
cv::waitKey(3);
// Output modified video stream
image_pub_.publish(cv_ptr->toImageMsg());
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_converter");
ImageConverter ic;
ros::spin();
return 0;
} |
#include
#include
#include
#include
#include
#include
static const std::string OPENCV_WINDOW = "Image window";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
public:
ImageConverter()
: it_(nh_)
{
// Subscrive to input video feed and publish output video feed
image_sub_ = it_.subscribe("/image_raw", 1, &ImageConverter::imageCb, this);
image_pub_ = it_.advertise("/image_converter/output_video", 1);
cv::namedWindow(OPENCV_WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(OPENCV_WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
// Draw an example circle on the video stream
if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));
// Update GUI Window
cv::imshow(OPENCV_WINDOW, cv_ptr->image);
cv::waitKey(3);
// Output modified video stream
image_pub_.publish(cv_ptr->toImageMsg());
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_converter");
ImageConverter ic;
ros::spin();
return 0;
}
CMakeListsの書き換え
catkin_create_packageで出力されるCmakeLists.txtは何を書いたらいいのかを丁寧に書いているが,どこを修正していいのかがわかりにくい.
そこで,CMakeLists.txtの一番下に以下のコードを追加する.
find_package(OpenCV REQUIRED)
include_directories(
${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS}
)
add_library(
${PROJECT_NAME} src/cv_bridge_tutorial.cpp
)
add_executable(
${PROJECT_NAME}_node src/cv_bridge_tutorial.cpp
)
target_link_libraries(
${PROJECT_NAME}_node
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
) |
find_package(OpenCV REQUIRED)
include_directories(
${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS}
)
add_library(
${PROJECT_NAME} src/cv_bridge_tutorial.cpp
)
add_executable(
${PROJECT_NAME}_node src/cv_bridge_tutorial.cpp
)
target_link_libraries(
${PROJECT_NAME}_node
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
)
ノードの動作確認
ここでは,4つのターミナルを用いて動作確認を行う.なお,ターミナルを起動したら,すべてのターミナルで念のため下記の様にする.
cd
cd catkin_ws/
source devel/setup.bash |
cd
cd catkin_ws/
source devel/setup.bash
ターミナル1
ここでは,roscoreを起動する.
ターミナル2
ここでは,カメラからの画像取得用ノードを起動する.
rosrun uvc_camera uvc_camera_node |
rosrun uvc_camera uvc_camera_node
ターミナル3
ここでは,画像表示用のノードを起動する.なお,ここでは,作成したノードからのトピックを受け付けられるように変更
rosrun image_view image_view image:=/image_topic |
rosrun image_view image_view image:=/image_topic
ターミナル4
rosrun cv_bridge_tutorial cv_bridge_tutorial |
rosrun cv_bridge_tutorial cv_bridge_tutorial