Webcam Tracker  V1.0
Using openCV and an Arduino Uno
Public Member Functions | Private Attributes | List of all members

Child-classe, implements OpenCV's contour finding. More...

#include <Contour.h>

Inheritance diagram for Contour:

Public Member Functions

 Contour (const cv::String name)
 Constructor. More...
 
void setAction (void *data)
 sets the output of the action More...
 
void * getAction ()
 gets the output of the action More...
 
void showWindow ()
 shows the action to the user More...
 
- Public Member Functions inherited from PipeLineAction
 PipeLineAction ()
 Default constructor.
 
 PipeLineAction (const cv::String name)
 Constructor. More...
 
void hideWindow ()
 hides the action to the user More...
 
cv::String getName ()
 gets the pipeline action name More...
 

Private Attributes

std::vector< std::vector< cv::Point > > contours
 vector containing all contour points
 
std::vector< std::vector< cv::Point > > polylines
 vector containing all polyline points
 
const cv::Scalar green { 0,255,0 }
 color green
 
const cv::Scalar red { 0,0,255 }
 color red
 
const int accuracyMax = 20
 maximum value of the accuracy trackbar
 
const int areaMax = 50
 maximum value of the area trackbar
 
int accuracy
 current value of the accuracy trackbar
 
int area
 area value of the accuracy trackbar
 

Additional Inherited Members

- Protected Attributes inherited from PipeLineAction
cv::Mat img
 Matrix which holds the image of the action.
 
bool show
 Show action flag.
 
cv::String name
 (screen)name of the pipeline action
 

Detailed Description

Child-classe, implements OpenCV's contour finding.

With area filtering and size sorting

Constructor & Destructor Documentation

◆ Contour()

Contour::Contour ( const cv::String  name)

Constructor.

Parameters
name(screen)name of mask action
14 {
15  accuracy = 3;
16  area = 10;
17 }
int accuracy
current value of the accuracy trackbar
Definition: Contour.h:27
int area
area value of the accuracy trackbar
Definition: Contour.h:28
cv::String name
(screen)name of the pipeline action
Definition: PipeLineAction.h:27
PipeLineAction()
Default constructor.
Definition: PipeLineAction.cpp:11

Member Function Documentation

◆ setAction()

void Contour::setAction ( void *  data)
virtual

sets the output of the action

Sets the action, in this case: finding all contours of the input matrix. And appying a area filter, bigger than the trackbar value multiplied by 10 contour color is green. Smaller countours are painted red. If there are no contours big enough, the vector is cleared.

Parameters
datapointer to the input data, should be a OpenCV matrix

Implements PipeLineAction.

20 {
21  Mat* d = static_cast<Mat*>(data);
22  // Find Contours
23  img = Mat(d->rows, d->cols, CV_8SC3);
24  findContours(d->clone(), contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
25 
26  if (contours.size() > 0) {
27  //Draw contours
28  polylines.resize(contours.size());
29  bool areaBigEnough = false;
30  Scalar lineColor;
31  for (size_t i = 0; i < contours.size(); i++)
32  {
33 
34  approxPolyDP(contours[i], polylines[i], accuracy, true);
35  //Check area size
36  if (contourArea(contours[i]) > area*100) {
37  areaBigEnough = true;
38  lineColor = green;
39  }
40  else {
41  lineColor = red;
42  }
43  drawContours(img, polylines, (int)i, lineColor);
44  }
45  //Sort contours by size
46  if (areaBigEnough) {
47  sort(
48  contours.begin(),
49  contours.end(),
50  [](const vector<Point>& a, const vector<Point>& b) { return a.size() < b.size(); }
51  );
52  }
53  // Clear contour area
54  else {
55  contours.clear();
56  }
57  }
58 }
const cv::Scalar green
color green
Definition: Contour.h:21
const cv::Scalar red
color red
Definition: Contour.h:22
std::vector< std::vector< cv::Point > > polylines
vector containing all polyline points
Definition: Contour.h:19
std::vector< std::vector< cv::Point > > contours
vector containing all contour points
Definition: Contour.h:18
cv::Mat img
Matrix which holds the image of the action.
Definition: PipeLineAction.h:25

◆ getAction()

void * Contour::getAction ( )
virtual

gets the output of the action

Returns
data: pointer to a vector with in a vector containing the points of all contours found

Reimplemented from PipeLineAction.

61 {
62  return &contours;
63 }

◆ showWindow()

void Contour::showWindow ( )
virtual

shows the action to the user

uses cv::imshow(). The name of the window is the name of the pipeline action. Trackbar is added to set the size of the approximation accuracy

Implements PipeLineAction.

66 {
67  if (!show) {
68  namedWindow(name);
69  createTrackbar("Accuracy", name, &accuracy, accuracyMax);
70  createTrackbar("Area factor", name, &area, areaMax);
71  show = true;
72  }
73  imshow(name, img);
74 }
const int accuracyMax
maximum value of the accuracy trackbar
Definition: Contour.h:24
const int areaMax
maximum value of the area trackbar
Definition: Contour.h:25
bool show
Show action flag.
Definition: PipeLineAction.h:26