gui.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <image_transport/image_transport.h>
00003 #include <opencv/cv.h>
00004 #include <opencv/highgui.h>
00005 #include <cv_bridge/CvBridge.h>
00006 #include <boost/thread/mutex.hpp>
00007 
00008 #include <motld/BoundingBox.h>
00009 
00010 ros::Publisher box_pub_;
00011 sensor_msgs::CvBridge bridge_;
00012 
00013 boost::mutex state_mutex_;
00014 bool isMouseCallbackSet_ = false;
00015 bool correctBB = false;
00016 static CvFont font_;
00017 static IplImage * color_image_;
00018 static IplImage * curr_image_;
00019 static IplImage * imgc_;
00020 static CvPoint point_, point_next_;
00021 static CvRect bb_;
00022 static int drag_ = 0;
00023 
00024 static void addTextAndBB(IplImage *img) {
00025         cvPutText(img, "Draw a bounding box and press Enter", cvPoint(0, 60),
00026                   &font_, cvScalar(255, 255, 0));
00027         bool bb_set = (point_.x != -1 && point_.y != -1);
00028         if (bb_set) {
00029                 cvRectangle(img, point_, point_next_, CV_RGB(255, 0, 0), 1, 8, 0);
00030         } else {
00031                 //ROS_ERROR("cannot draw bb");
00032         }
00033 }
00034 
00035 static void mouseHandler(int event, int x, int y, int flags, void* param) {
00036         /* user press left button */
00037         if (event == CV_EVENT_LBUTTONDOWN && !drag_) {
00038                 point_ = cvPoint(x, y);
00039                 drag_ = 1;
00040         }
00041 
00042         /* user drag the mouse */
00043         if (event == CV_EVENT_MOUSEMOVE && drag_) {
00044                 /* set point_next_ */
00045                 point_next_ = cvPoint(x, y);
00046         }
00047 
00048         /* user release left button */
00049         if (event == CV_EVENT_LBUTTONUP && drag_) {
00050                 bb_ = cvRect(point_.x, point_.y, point_next_.x - point_.x, point_next_.y - point_.y);
00051                 drag_ = 0;                
00052         }
00053 }
00054 
00055 static void trackerCallback(const sensor_msgs::ImageConstPtr& msg) {
00056         // convert image
00057         boost::unique_lock<boost::mutex>(state_mutex_);
00058         color_image_ = bridge_.imgMsgToCv(msg, "bgr8");
00059         curr_image_ = (IplImage *) cvClone(color_image_);
00060         
00061         addTextAndBB(curr_image_);
00062         cvShowImage("bounding_box", curr_image_);
00063         cvReleaseImage(&curr_image_);
00064 
00065         if (isMouseCallbackSet_ == false) {
00066                 cvSetMouseCallback("bounding_box", mouseHandler, NULL);
00067                 isMouseCallbackSet_ = true;
00068         }
00069 }
00070 
00071 int main(int argc, char **argv) {
00072         ros::init(argc, argv, "motld_gui");
00073         ros::NodeHandle nh;
00074         image_transport::ImageTransport it(nh);
00075         image_transport::Subscriber sub = it.subscribe("/camera/rgb/image_color", 1, trackerCallback);
00076         motld::BoundingBox box_msg;
00077         box_pub_ = nh.advertise<motld::BoundingBox>("/motld_node/tracker_start_box", 1);
00078 
00079         cvNamedWindow("bounding_box");
00080         cvInitFont(&font_, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, 8);
00081         //cvStartWindowThread();
00082         
00083         bool user_exit = false;
00084         point_.x = -1;
00085         point_.y = -1;
00086         while (ros::ok() && !user_exit) {
00087                 ros::spinOnce();
00088                 char key = cvWaitKey(5);
00089                 if(tolower(key) == 'q') {
00090                         ROS_INFO("motld gui user exit!");
00091                         user_exit = true;
00092                 }
00093                 if((key == '\n') && (bb_.x != -1) && (bb_.y != -1)) {
00094                         correctBB = true;
00095                         ROS_INFO("motld sending bounding box");
00096                         box_msg.x = bb_.x;
00097                         box_msg.y = bb_.y;
00098                         box_msg.width = bb_.width;
00099                         box_msg.height = bb_.height;
00100                         box_pub_.publish(box_msg);
00101                 }
00102         }
00103         
00104         cvSetMouseCallback("bounding_box", NULL, NULL);
00105         cvDestroyWindow("bounding_box");
00106         cvReleaseImage(&curr_image_);
00107 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


motld
Author(s): Jost Tobias Springenberg, Jan Wuelfing
autogenerated on Wed Dec 26 2012 16:24:49