NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ContourTester.hpp
1 //LineSegmentTester.hpp
2 // Created on: Jun 1, 2015
3 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
4 #pragma once
5 #include <opencv2/opencv.hpp>
6 #include <vector>
7 #include <vision_module/Tools/LineSegment.hpp>
8 #include <vision_module/Tools/General.hpp>
9 
10 using namespace std;
11 using namespace cv;
12 
13 void CallBackFuncCT(int event, int x, int y, int flags, void* userdata);
20 {
21 public:
22  Mat Image;
23  vector<vector<Point> > Contours;
24  string name;
25  size_t Max;
26  vector<Scalar> colors;
27 public:
28  inline ContourTester(Size box, size_t _max = 2, string _name =
29  "ContourTester") :
30  name(_name), Max(_max)
31  {
32  Image = Mat::zeros(box, CV_8UC3);
33  namedWindow(name, 1);
34  //set the callback function for any mouse event
35  setMouseCallback(name, CallBackFuncCT, this);
36  Contours.push_back(vector<Point>());
37  colors.push_back(randColor());
38  }
39  inline bool IsReady(size_t count)
40  {
41  return Contours.size() > count
42  && Contours[Contours.size() - 1].size() > 2;
43  }
44  inline bool IsReady()
45  {
46  return Contours.size() > Max && Contours[Contours.size() - 1].size() > 2;
47  }
48  inline void Show(int wait = 1)
49  {
50  for (size_t i = 0; i < Contours.size(); i++)
51  {
52  if (Contours.size() - 1 == i)
53  {
54  drawContours(Image, Contours, i, redColor(), 2);
55  }
56  else
57  {
58  drawContours(Image, Contours, i, colors[i], 1);
59  }
60  }
61  if(Contours[Contours.size() - 1].size() == 1)
62  {
63  circle(Image,Contours[Contours.size() - 1][0],2,redColor(),1);
64  }
65  imshow(name, Image);
66  waitKey(wait);
67  Image = Mat::zeros(Image.size(), CV_8UC3);
68  }
69 };
70 
71 void CallBackFuncCT(int event, int x, int y, int flags, void* userdata)
72 {
73  ContourTester* data = reinterpret_cast<ContourTester*>(userdata);
74  if (event == EVENT_RBUTTONUP)
75  {
76  if (data->Contours[data->Contours.size() - 1].size() > 0)
77  {
78  data->Contours[data->Contours.size() - 1].pop_back();
79  }
80  else
81  {
82  if (data->Contours.size() > 1)
83  {
84  data->Contours.pop_back();
85  data->colors.pop_back();
86  }
87  }
88  }
89  else if (event == EVENT_MBUTTONDOWN)
90  {
91  if (data->Contours[data->Contours.size() - 1].size() < 3)
92  return;
93  data->Contours.push_back(vector<Point>());
94  data->colors.push_back(randColor());
95  }
96  else if (event == EVENT_LBUTTONUP)
97  {
98  data->Contours[data->Contours.size() - 1].push_back(Point(x, y));
99  }
100 }
A container class for testing countours.
Definition: ContourTester.hpp:19