Go to the documentation of this file.00001
00047 #include "comthread.h"
00048
00049 #include <ros/ros.h>
00050 #include <re_vision/SearchFor.h>
00051 #include <std_msgs/String.h>
00052 #include <sensor_msgs/Image.h>
00053 #include <image_transport/image_transport.h>
00054 #include <cv_bridge/cv_bridge.h>
00055
00056 #include <QDebug>
00057
00058 #include <re_vision/SearchFor.h>
00059
00060 ComThread::ComThread(QObject *parent) :
00061 QThread(parent), image_transport(nh)
00062 {
00063 zaragoza_client = nh.serviceClient<re_vision::SearchFor>("/re_vision/search_for");
00064 zaragoza_model_dir_pub = nh.advertise<std_msgs::String>("/re_vslam/new_model", 1);
00065
00066 kinect_model_dir_pub = nh.advertise<std_msgs::String>("/re_kinect/model_path", 1);
00067
00068 image_sub = image_transport.subscribe("/camera/rgb/image_color", 1, &ComThread::imageCb, this);
00069 kinect_detected_objects_sub = nh.subscribe<re_kinect_object_detector::DetectionResult>("/re_kinect/detection_results", 10, boost::bind(&ComThread::kinect_detected_objectCb, this, _1));
00070 }
00071
00072 void ComThread::run()
00073 {
00074 ros::spin();
00075 }
00076
00077 void ComThread::publishModelDir(QString model_dir, QString model_type, QString model_name) {
00078 std_msgs::String msg;
00079 msg.data = model_dir.toStdString();
00080 if ((model_type == "3D") && zaragoza_client.exists()) {
00081 zaragoza_model_dir_pub.publish(msg);
00082 if (std::find(zaragoza_model_names.begin(), zaragoza_model_names.end(),
00083 model_name.toStdString()) == zaragoza_model_names.end()) {
00084 zaragoza_model_names.push_back(model_name.toStdString());
00085 }
00086 }
00087 else if (model_type == "kinect_pcl") {
00088 kinect_model_dir_pub.publish(msg);
00089 }
00090
00091 }
00092
00093 QImage cvMatToQImage(const cv::Mat& image)
00094 {
00095 QImage qImage(
00096 image.data,
00097 image.size().width,
00098 image.size().height,
00099 image.step,
00100 QImage::Format_RGB888
00101 );
00102
00103 return qImage.rgbSwapped();
00104 }
00105
00106 void ComThread::kinect_detected_objectCb(const re_kinect_object_detector::DetectionResultConstPtr &msg) {
00107 cv_bridge::CvImagePtr cv_ptr;
00108 try
00109 {
00110 cv_ptr = cv_bridge::toCvCopy(msg->Image,"bgr8");
00111 }
00112 catch (cv_bridge::Exception& e)
00113 {
00114 ROS_ERROR("cv_bridge exception: %s", e.what());
00115 return;
00116 }
00117
00118 for(size_t i=0; i<msg->Detections.size(); i++) {
00119 const re_msgs::DetectedObject& detectedObject = msg->Detections.at(i);
00120
00121 for (size_t j=0; j < detectedObject.points2d.size(); j++) {
00122 if (detectedObject.points2d.at(j).y < cv_ptr->image.size().height &&
00123 detectedObject.points2d.at(j).y > 0 &&
00124 detectedObject.points2d.at(j).x < cv_ptr->image.size().width &&
00125 detectedObject.points2d.at(j).x > 0)
00126 cv_ptr->image.at<cv::Vec3b>(detectedObject.points2d.at(j).y, detectedObject.points2d.at(j).x) = cv::Vec3b(0, 0, 255);
00127 }
00128 }
00129 emit updateKinectDetectionImg(cvMatToQImage(cv_ptr->image));
00130 }
00131
00132 void ComThread::imageCb(const sensor_msgs::ImageConstPtr &msg) {
00133 if (zaragoza_model_names.empty())
00134 return;
00135
00136 re_vision::SearchForRequest req;
00137 req.Image = *msg;
00138 req.Objects = zaragoza_model_names;
00139 req.MaxPointsPerObject = -1;
00140
00141 re_vision::SearchForResponse resp;
00142 zaragoza_client.call(req, resp);
00143
00144
00145
00146 if (resp.Detections.size() == 0)
00147 return;
00148
00149 cv_bridge::CvImagePtr cv_ptr;
00150 try
00151 {
00152 cv_ptr = cv_bridge::toCvCopy(msg,"bgr8");
00153 }
00154 catch (cv_bridge::Exception& e)
00155 {
00156 ROS_ERROR("cv_bridge exception: %s", e.what());
00157 return;
00158 }
00159
00160
00161 bool marked_one = false;
00162 for(int i=0; i<resp.Detections.size(); i++) {
00163 const re_msgs::DetectedObject& detectedObject = resp.Detections.at(i);
00164
00165 for (int j=0; j < detectedObject.points2d.size(); j++) {
00166 marked_one = true;
00167 cv_ptr->image.at<cv::Vec3b>(detectedObject.points2d.at(j).y, detectedObject.points2d.at(j).x) = cv::Vec3b(0, 0, 255);
00168 }
00169 }
00170
00171 if (marked_one)
00172 emit updateZaragozaDetectionImg(cvMatToQImage(cv_ptr->image));
00173 }