37 #define _USE_MATH_DEFINES 
   40 #include <opencv2/opencv.hpp> 
   43 #include <sensor_msgs/Image.h> 
   52     DiagnosticNodelet::onInit();
 
   57     pub_mask_image_ = advertise<sensor_msgs::Image>(*pnh_, 
"output/mask", 1);
 
   65       async_ = boost::make_shared<message_filters::Synchronizer<ApproximateSyncPolicy> >(
queue_size_);
 
   71       sync_ = boost::make_shared<message_filters::Synchronizer<ExactSyncPolicy> >(
queue_size_);
 
  103                                            const geometry_msgs::PolygonStamped::ConstPtr& poly_msg)
 
  111     cv::cvtColor(image, gray, CV_BGR2GRAY);
 
  114     cv::Point2f initial_top_left = cv::Point2f(poly_msg->polygon.points[0].x, poly_msg->polygon.points[0].y);
 
  115     cv::Point2f initial_bottom_right = cv::Point2f(poly_msg->polygon.points[1].x, poly_msg->polygon.points[1].y);
 
  117     cmt.initialise(gray, initial_top_left, initial_bottom_right);
 
  119     ROS_INFO(
"A window is initialized. top_left: (%lf, %lf), bottom_right: (%lf, %lf)",
 
  120              initial_top_left.x, initial_top_left.y, initial_bottom_right.x, initial_bottom_right.y);
 
  136     cv::cvtColor(image, gray, CV_BGR2GRAY);
 
  137     cmt.processFrame(gray);
 
  140     for (
int i=0; 
i < 
cmt.trackedKeypoints.size(); 
i++)
 
  142       cv::circle(image, 
cmt.trackedKeypoints[
i].first.pt, 3, cv::Scalar(255, 0, 0));
 
  144     cv::line(image, 
cmt.topLeft, 
cmt.topRight, cv::Scalar(0, 30, 255));
 
  145     cv::line(image, 
cmt.topRight, 
cmt.bottomRight, cv::Scalar(0, 30, 255));
 
  146     cv::line(image, 
cmt.bottomRight, 
cmt.bottomLeft, cv::Scalar(0, 30, 255));
 
  147     cv::line(image, 
cmt.bottomLeft, 
cmt.topLeft, cv::Scalar(0, 30, 255));
 
  150     cv::Point diff = 
cmt.topLeft - 
cmt.bottomLeft;
 
  151     cv::Point center2 = 
cmt.topLeft + 
cmt.bottomRight;
 
  152     cv::Point2f center = cv::Point2f(center2.x / 2, center2.y / 2);
 
  153     cv::Size2f size = cv::Size2f(cv::norm(
cmt.topLeft - 
cmt.topRight), cv::norm(
cmt.topLeft - 
cmt.bottomLeft));
 
  154     cv::RotatedRect rRect = cv::RotatedRect(center, size, asin(diff.x / cv::norm(diff)) * 180 / 
M_PI);
 
  155     cv::Point2f vertices2f[4];
 
  156     rRect.points(vertices2f);
 
  157     cv::Point vertices[4];
 
  158     for (
int i = 0; 
i < 4; ++
i)
 
  160       vertices[
i] = vertices2f[
i];
 
  162     cv::Mat mask = cv::Mat::zeros(image.rows, image.cols, CV_8UC1);
 
  163     cv::fillConvexPoly(mask, vertices, 4, cv::Scalar(255));