oriented_gradient_node.cpp
Go to the documentation of this file.
00001 // @brief sample node to use oriented gradient
00002 // @author Hiroaki Yaguchi, JSK
00003 
00004 #include <vector>
00005 #include <string>
00006 
00007 #include <ros/ros.h>
00008 #include <image_transport/image_transport.h>
00009 #include <cv_bridge/cv_bridge.h>
00010 #include <sensor_msgs/image_encodings.h>
00011 #include <opencv2/imgproc/imgproc.hpp>
00012 #include <opencv2/highgui/highgui.hpp>
00013 
00014 #include "jsk_perception/oriented_gradient.hpp"
00015 
00016 namespace jsk_perception {
00017 
00018 class OrientedGradientNode {
00019  public:
00020   explicit OrientedGradientNode(const ros::NodeHandle& nh) :
00021       handle_(nh), image_transport_(nh) {
00022     image_pub_ = image_transport_.advertise("image", 1);
00023     image_sub_ = image_transport_.subscribe(
00024         "/camera/rgb/image_raw", 1,
00025         &OrientedGradientNode::imageCallback, this);
00026     cv::namedWindow("OrinetedGradient");
00027   }
00028 
00029   ~OrientedGradientNode() {
00030   }
00031 
00032  private:
00033   ros::NodeHandle handle_;
00034   image_transport::ImageTransport image_transport_;
00035   image_transport::Subscriber image_sub_;
00036   image_transport::Publisher image_pub_;
00037   void imageCallback(const sensor_msgs::ImageConstPtr& msg) {
00038     cv_bridge::CvImagePtr cv_ptr;
00039 
00040     try {
00041       cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00042     } catch (cv_bridge::Exception& e) {
00043       ROS_ERROR("cv_bridge exception: %s", e.what());
00044       return;
00045     }
00046 
00047     cv::Mat cv_img = cv_ptr->image;
00048     cv::Mat cv_og_img;
00049     cv::Mat cv_out_img;
00050 
00051     // calcOrientedGradient() will write oriented gradient to
00052     // 2nd arg image as HSV format,
00053     // H is orientation and V is intensity
00054     calcOrientedGradient(cv_img, cv_og_img);
00055     cv::cvtColor(cv_og_img, cv_out_img, CV_HSV2BGR);
00056     // cv::imshow("OrinetedGradient", cv_out_img);
00057     // cv::waitKey(1);
00058     sensor_msgs::Image::Ptr out_img = cv_bridge::CvImage(
00059       msg->header,
00060       sensor_msgs::image_encodings::BGR8,
00061       cv_out_img).toImageMsg();
00062     image_pub_.publish(out_img);
00063   }
00064 };
00065 
00066 }  // namespace
00067 
00068 
00069 int main(int argc, char** argv) {
00070   ros::init(argc, argv, "oriented_gradient");
00071   ros::NodeHandle handle("~");
00072   jsk_perception::OrientedGradientNode ognode(handle);
00073   ros::spin();
00074   return 0;
00075 }
00076 
00077 


jsk_perception
Author(s): Manabu Saito
autogenerated on Mon Oct 6 2014 01:16:59