ORControlModule.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  ORControlModule.cpp
00003  *
00004  *  (C) 2006 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *******************************************************************************/
00008 
00009 #include <sstream>
00010 
00011 #include <cv_bridge/cv_bridge.h>
00012 #include <sensor_msgs/image_encodings.h>
00013 #include <sensor_msgs/Image.h>
00014 #include <opencv2/highgui/highgui.hpp>
00015 
00016 #include <or_msgs/BoundingBox2D.h>
00017 #include <or_msgs/ExtractKeyPoints.h>
00018 #include <or_msgs/OrImage.h>
00019 
00020 #include "Architecture/Config/Config.h"
00021 
00022 #include "ORControlModule.h"
00023 #include "ORMatchingModule.h"
00024 
00025 #define THIS ORControlModule
00026 
00027 THIS::THIS(ros::NodeHandle *nh, ORMatchingModule* objRecMatchingModule)
00028 {
00029     m_ORMatchingModule = objRecMatchingModule;
00030 
00031     m_ImagesInPipeline = 0;
00032     m_MaxImagesInPipeline = Config::getInt( "ObjectRecognition.iMaxImagesInPipeline" );
00033 
00034     m_Continuous = false;
00035 
00036     // subscribe to topics
00037     m_ORCommandSubscriber = nh->subscribe<or_msgs::OrCommand>("/or/commands", 10, &ORControlModule::callbackOrCommand, this);
00038     m_ORMatchResultSubscriber = nh->subscribe<or_msgs::OrMatchResult>("or/match_result", 10, &ORControlModule::callbackOrMatchResult, this);
00039 
00040     // advertise topics
00041     m_ExtractKeyPointsPublisher = nh->advertise<or_msgs::ExtractKeyPoints>("/or/extract", 10);
00042     m_DebugImagePublisher = nh->advertise<or_msgs::OrImage>("or/debug_image", 10);
00043     m_DebugImagePublisherGray = nh->advertise<sensor_msgs::Image>("or/debug_image_gray", 10);
00044     m_DebugImagePublisherColor = nh->advertise<sensor_msgs::Image>("or/debug_image_color", 10);
00045 }
00046 
00047 
00048 THIS::~THIS()
00049 {}
00050 
00051 
00052 void THIS::callbackOrCommand( const or_msgs::OrCommand::ConstPtr& or_command_msg )
00053 {
00054     ROS_DEBUG_STREAM("or_command message received");
00055 
00056     std::vector<or_msgs::BoundingBox2D> m_BoundingBoxes = or_command_msg->bounding_boxes;
00057 
00058     switch (or_command_msg->command)
00059     {
00060     case ORControlModule::GrabSingleImage:
00061     {
00062         m_SourceId = or_command_msg->int_value; // TODO  m_SourceId = ImageSources::SourceId(message->getInt());
00063         // send message for keypoint extraction
00064         or_msgs::ExtractKeyPoints extract_msg;
00065         extract_msg.img_source = m_SourceId;
00066         extract_msg.bounding_boxes = m_BoundingBoxes;
00067         m_ExtractKeyPointsPublisher.publish(extract_msg);
00068 
00069         m_ImagesInPipeline++;
00070         m_Continuous = false;
00071         break;
00072     }
00073 
00074     case ORControlModule::LoadSingleImage:
00075     {
00076         std::string fileName = or_command_msg->string_value;
00077         ROS_INFO_STREAM("Loading image file: " << fileName);
00078 
00079         // send message for keypoint extraction
00080         or_msgs::ExtractKeyPoints extract_msg;
00081         extract_msg.img_source = 0; // TODO ImageSources::SourceId::None
00082         extract_msg.bounding_boxes = m_BoundingBoxes;
00083         m_ExtractKeyPointsPublisher.publish(extract_msg);
00084 
00085         //read and publish image
00086         cv_bridge::CvImage gray_image, color_image;
00087         gray_image.image = cv::imread(fileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
00088         gray_image.encoding = sensor_msgs::image_encodings::TYPE_8UC1;
00089         color_image.image = cv::imread(fileName.c_str(), CV_LOAD_IMAGE_COLOR);
00090         color_image.encoding = sensor_msgs::image_encodings::TYPE_8UC3;
00091 
00092 
00093 
00094         // TODO in the long term: get rid of or_msgs::OrImage or at least revise message.
00095         or_msgs::OrImage or_image_msg;
00096         or_image_msg.image_gray = *(gray_image.toImageMsg());
00097         or_image_msg.image_color = *(color_image.toImageMsg());
00098         or_image_msg.filename = fileName;
00099         m_DebugImagePublisher.publish(or_image_msg);
00100         // TODO here we have dublicate publishing
00101         sensor_msgs::Image image_msg;
00102         image_msg = *(gray_image.toImageMsg());
00103         m_DebugImagePublisherGray.publish(image_msg);
00104         image_msg = *(color_image.toImageMsg());
00105         m_DebugImagePublisherColor.publish(image_msg);
00106 
00107 
00108 
00109         m_ImagesInPipeline++;
00110         m_Continuous = false;
00111 
00112         // pass loaded image directly to the matching module
00113         m_ORMatchingModule->processImageMessage(color_image.toImageMsg());
00114 
00115         break;
00116     }
00117 
00118     case ORControlModule::StartRecognitionLoop:
00119     {
00120         m_SourceId = or_command_msg->int_value; // TODO ImageSources::SourceId(message->getInt());
00121         for ( int i=0; i<m_MaxImagesInPipeline; i++ )
00122         {
00123             // send message for keypoint extraction
00124             or_msgs::ExtractKeyPoints extract_msg;
00125             extract_msg.img_source = m_SourceId;
00126             extract_msg.bounding_boxes = m_BoundingBoxes;
00127             m_ExtractKeyPointsPublisher.publish(extract_msg);
00128             m_ImagesInPipeline++;
00129         }
00130         m_Continuous = true;
00131         break;
00132     }
00133 
00134     case ORControlModule::StopRecognitionLoop:
00135     {
00136         m_Continuous = false;
00137         break;
00138     }
00139 
00140     default:
00141         break;
00142     }
00143 }
00144 
00145 void THIS::callbackOrMatchResult( const or_msgs::OrMatchResult::ConstPtr& or_match_result_msg)
00146 {
00147     // TODO only for debug
00148     if(or_match_result_msg->match_results.size() == 0)
00149         ROS_ERROR_STREAM("no objects recognized");
00150     else
00151     {
00152            ROS_ERROR_STREAM("recognized objects: ");
00153     for(unsigned i = 0; i < or_match_result_msg->match_results.size(); i++)
00154         ROS_ERROR_STREAM(or_match_result_msg->match_results.at(i).object_name);
00155     }
00156 
00157 
00158     if ( m_Continuous )
00159     {
00160         // send message for keypoint extraction
00161         or_msgs::ExtractKeyPoints extract_msg;
00162         extract_msg.img_source = m_SourceId;
00163         extract_msg.bounding_boxes = m_BoundingBoxes;
00164         m_ExtractKeyPointsPublisher.publish(extract_msg);
00165         m_ImagesInPipeline++;
00166     }
00167     m_ImagesInPipeline--;
00168 }
00169 
00170 
00171 #undef THIS


or_nodes
Author(s): raphael
autogenerated on Mon Oct 6 2014 02:53:36