vision_gui.cpp
Go to the documentation of this file.
00001 /*
00002  *  This file is part of ucl_drone 2016.
00003  *  For more information, refer
00004  *  to the corresponding header file.
00005  *
00006  *  \author Arnaud Jacques & Alexandre Leclere
00007  *  \date 2016
00008  *
00009  */
00010 
00011 #include <ucl_drone/vision_gui/vision_gui.h>
00012 
00013 VisionGui::VisionGui()
00014 {
00015   // Instantiate the viewer widow
00016   cv::namedWindow(OPENCV_WINDOW1);
00017 
00018   // Subscribe to processed_image (contains image + keypoints + ...)
00019   processed_img_channel = nh_.resolveName("processed_image");
00020   processed_img_sub = nh_.subscribe(processed_img_channel, 1, &VisionGui::processedImageCb, this);
00021 
00022   // Get parameters in launch file
00023   ros::param::get("~draw_keypoints", this->draw_keypoints);
00024   ros::param::get("~draw_target", this->draw_target);
00025 
00026   new_processed_img_available = false;
00027   target_detected = false;
00028 }
00029 
00030 VisionGui::~VisionGui()
00031 {
00032   cv::destroyWindow(OPENCV_WINDOW1);
00033 }
00034 
00035 /* This function is called every time a new processed_image is published */
00036 void VisionGui::processedImageCb(const ucl_drone::ProcessedImageMsg::ConstPtr processed_image)
00037 {
00038   ROS_DEBUG("VisionGui::processedImageCb start");
00039   this->lastProcessedImgReceived = processed_image;
00040   convertMsgToAttributes(processed_image);
00041   new_processed_img_available = true;
00042 }
00043 
00044 void VisionGui::guiDrawKeypoints()
00045 {
00046   ROS_DEBUG("signal sent VisionGui::guiDrawKeypoints");
00047 
00048   cv::Mat output = cv_ptr->image;
00049 
00050   // Show all keypoints in image
00051   if (this->draw_keypoints)
00052   {
00053     for (unsigned i = 0; i < keypoints.size(); ++i)
00054     {
00055       cv::circle(output, keypoints[i], 3, cv::Scalar(0, 0, 255), -1, 8);
00056     }
00057   }
00058 
00059   // Show the target
00060   if (this->draw_target && target_detected)
00061   // if (target_detected)
00062   {
00063     cv::line(output, this->target_cornersAndCenter[0], this->target_cornersAndCenter[1],
00064              cv::Scalar(0, 255, 0), 4);
00065     cv::line(output, this->target_cornersAndCenter[1], this->target_cornersAndCenter[2],
00066              cv::Scalar(0, 255, 0), 4);
00067     cv::line(output, this->target_cornersAndCenter[2], this->target_cornersAndCenter[3],
00068              cv::Scalar(0, 255, 0), 4);
00069     cv::line(output, this->target_cornersAndCenter[3], this->target_cornersAndCenter[0],
00070              cv::Scalar(0, 255, 0), 4);
00071   }
00072   // Update GUI Window
00073   cv::imshow(OPENCV_WINDOW1, output);
00074   cv::waitKey(3);
00075 }
00076 
00077 void VisionGui::convertMsgToAttributes(ucl_drone::ProcessedImageMsg::ConstPtr msg)
00078 {
00079   // convert points in the msg to opencv format
00080   // and store these as object attributes
00081   this->keypoints.resize(msg->keypoints.size());
00082   for (unsigned i = 0; i < msg->keypoints.size(); ++i)
00083   {
00084     this->keypoints[i].x = (double)msg->keypoints[i].point.x;
00085     this->keypoints[i].y = (double)msg->keypoints[i].point.y;
00086   }
00087   target_detected = msg->target_detected;
00088   if (target_detected)
00089   {
00090     this->target_cornersAndCenter.resize(msg->target_points.size());
00091     for (unsigned i = 0; i < msg->target_points.size(); ++i)
00092     {
00093       this->target_cornersAndCenter[i].x = msg->target_points[i].x;
00094       this->target_cornersAndCenter[i].y = msg->target_points[i].y;
00095     }
00096   }
00097 
00098   // convert ROS image to OpenCV image
00099   try
00100   {
00101     this->cv_ptr = cv_bridge::toCvCopy(msg->image, sensor_msgs::image_encodings::BGR8);
00102   }
00103   catch (cv_bridge::Exception& e)
00104   {
00105     ROS_ERROR("ucl_drone::VisionGui::cv_bridge exception: %s", e.what());
00106     return;
00107   }
00108 }
00109 
00110 int main(int argc, char** argv)
00111 {
00112   ros::init(argc, argv, "vision_gui");
00113 
00114   // if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug))
00115   // {
00116   //   ros::console::notifyLoggerLevelsChanged();
00117   // }
00118 
00119   VisionGui my_gui;
00120   ROS_DEBUG("end of computer vision viewer initialization");
00121 
00122   ros::Rate r(12);
00123 
00124   while (ros::ok())
00125   {
00126     ros::spinOnce();
00127     if (my_gui.new_processed_img_available)
00128     {
00129       TIC(gui);
00130       my_gui.guiDrawKeypoints();
00131       my_gui.new_processed_img_available = false;
00132       TOC(gui, "vision_gui")
00133     }
00134     r.sleep();
00135   }
00136 
00137   return 0;
00138 }


ucl_drone
Author(s): dronesinma
autogenerated on Sat Jun 8 2019 20:51:53