Webcam Tracker  V1.0
Using openCV and an Arduino Uno
Files | Functions | Variables

This group is the main entry point for the openCV processing console application. More...

Collaboration diagram for Main Function:

Files

file  Main.cpp
 OpenCV proccesing for object tracking with a webcam and arduino uno.
 

Functions

int scale (float input, float rMax, float tMax)
 Scale funtion. More...
 
int main ()
 Main function. More...
 

Variables

cv::Mat src
 Object holding an OpenCV matrix for the capture source.
 
vector< PipeLineAction * > actions
 All OpenCV pipeline actions.
 
SerialComsc
 Object preforming the serial communication.
 
GUIg
 Object holding the grafic user interface.
 
Capture webCam
 Object holding the capture device.
 
char arduinoCommand [100]
 Command to be send to the arduino *&;/.
 

Detailed Description

This group is the main entry point for the openCV processing console application.

Function Documentation

◆ scale()

int scale ( float  input,
float  rMax,
float  tMax 
)

Scale funtion.

Scales the input to a range. Range is defined by rMax and tMax. Where rMax is the maximum of the input range and tMax is the maximum of the target(output) range

Note
function assums that for both ranges the minimum is the negative maximum
Parameters
inputinput to scale
rMaxmaximun of the input range
tMaxmaximum of the target(output) range
Returns
int: scaled value from input to target range
44 {
45  float rMin = -rMax;
46  float tMin = -tMax;
47  return static_cast<int>(((input - rMin) / (rMax - rMin))*(tMax-tMin)+tMin);
48 }
Here is the caller graph for this function:

◆ main()

main ( )

Main function.

sets up a source Matrix, creates the OpenCV pipeline, starts serial Communication with an Arduino Unoand initiates a graphic user interface.Continues until the escape - key is pressed or the console window is closed. In each loop it captures an image of the webcam.Excecutes the pipeline actions.Sends the output of the pipeline to the arduino and shows the appropriat windows on screen.

Note
setup for the serial communication
  • COM port : COM4
  • Bauderate : 9600
  • data size : 8 bits
  • parity : none
  • stop bits : 1
Returns
int exit-code

sets up a source Matrix, creates the OpenCV pipeline, starts serial Communication with an Arduino Uno and initiates a graphic user interface. Continues until the escape-key is pressed or the console window is closed. In each loop it captures an image of the webcam. Excecutes the pipeline actions. Sends the output of the pipeline to the arduino and shows the appropriat windows on screen.

Note
setup for the serial communication
  • COM port: COM4
  • Bauderate: 9600
  • data size: 8 bits
  • parity: none
  • stop bits : 1
65 {
66  //Webcam object
67  webCam = Capture(0, cv::CAP_ANY);
68 
69  //Create pipeline
70  actions.push_back(new Blur("Blurred"));
71  actions.push_back(new ConvertColorSpace("HSV"));
72 
73  #ifdef _DEBUG //Visual studio debug mode
74  HSV_MASK_VALUES testMask{ 0,87,100,9,211,255 };
75  actions.push_back(new Mask("Mask", testMask));
76  #else //Release mode
77  actions.push_back(new Mask("Mask"));
78  #endif // DEBUG
79 
80  actions.push_back(new Contour("Contours"));
81  actions.push_back(new Moment("Moment"));
82 
83  //Start serial communication
84  sc = SerialCom::initialize("COM4",9600, 8, 'N', 1);
85  sprintf_s(arduinoCommand, "Home\n");
87 
88  //Create grafic user interface
89  g = new GUI(actions);
90 
91  //Loop until Esc-key pressed
92  while (cv::waitKey(33) != 27)
93  {
94  //Get input source
96  if (src.empty())
97  src = cv::imread("oranje_pingpongbal.jpg", cv::IMREAD_COLOR);
98 
99  //Run pipeline, every input gets its output from the previous action
100  vector<PipeLineAction*>::iterator it;
101  actions[0]->setAction(&src);
102  for (it = actions.begin()+1 ; it != actions.end(); it++)
103  {
104  (*it)->setAction((*(it - 1))->getAction());
105  }
106 
107  //Get output of the pipeline
108  cv::Point c;
109  Moment* m = dynamic_cast<Moment*>(actions[actions.size() - 1]);
110  m ? c = *m->getCenterOfMass() : c = cv::Point(-10, -10);
111 
112  if (c.x >= 0 && c.y >= 0) {
113  //Translate center of mass to reasonable x and y coordinats
114  float yInput = (src.rows / static_cast<float>(2)) - c.y;
115  float xInput = c.x - (src.cols / static_cast<float>(2));
116 
117  //Scale to range of -15° to 15° this because other wise the arduino flips (movement is to slow)
118  //X(pan) is inverted because of mirrored image
119  int yOut = scale(yInput, src.rows / static_cast<float>(2), 15);
120  int xOut = -scale(xInput, src.cols / static_cast<float>(2), 15);
121 
122  //Write to serial
123  if (g->trackingActive()) {
124  sprintf_s(arduinoCommand,"PTO%d:%d\n", xOut, yOut);
126  }
127  }
128 
129  //Read serial input
130  sc->readString();
131  if (sc->newMessageAvailable())
132  g->addMessage(sc->getLastMessage()->c_str());
133 
134  //Show images
135  g->showOutput(src, &c);
137 
138  }
139  cv::destroyAllWindows();
140  return 0;
141 }
Child-classe, implements OpenCV's GaussianBlur.
Definition: Blur.h:14
Opens the webcam and captures its feed if available.
Definition: Capture.h:14
int getFeed(cv::Mat &src)
gets the webcam feed
Definition: Capture.cpp:37
Child-classe, implements OpenCV's contour finding.
Definition: Contour.h:15
Child-classe, converts from BGR to HSV color space.
Definition: ConvertColorSpace.h:14
Contains the graphic user interface main class. Member of GUI classes Graphic group.
Definition: GUI.h:28
bool trackingActive()
gets the tracking state
Definition: GUI.cpp:113
void showOutput(cv::Mat &src, cv::Point *trackingPoint)
shows the output Matrix to the user
Definition: GUI.cpp:76
void addMessage(string message)
adds a message to the GUI console
Definition: GUI.cpp:108
void showWindows(vector< PipeLineAction * > actions)
shows pipeline action windows depending on the state of the corresponding menu button
Definition: GUI.cpp:89
Child-classe, applies a mask to the input matrix.
Definition: Mask.h:28
Child-classe, implements OpenCV's center of mass finding.
Definition: Moment.h:15
cv::Point * getCenterOfMass()
gets the output of the action
Definition: Moment.cpp:38
bool newMessageAvailable()
Shows if a new message is recieved from the Serial input.
Definition: SerialCom.cpp:89
void write(char *message)
Sends a message(command) to the Arduino Uno.
Definition: SerialCom.cpp:61
static SerialCom * initialize(string portName, long baudeRate, int dataSize, char parityType, float nStopBits)
Creates a object if there isn't one already.
Definition: SerialCom.cpp:52
string * getLastMessage()
Returns last message.
Definition: SerialCom.cpp:98
void readString()
Reads the serial port for incomming messages.
Definition: SerialCom.cpp:69
cv::Mat src
Object holding an OpenCV matrix for the capture source.
Definition: Main.cpp:26
Capture webCam
Object holding the capture device.
Definition: Main.cpp:30
vector< PipeLineAction * > actions
All OpenCV pipeline actions.
Definition: Main.cpp:27
SerialCom * sc
Object preforming the serial communication.
Definition: Main.cpp:28
GUI * g
Object holding the grafic user interface.
Definition: Main.cpp:29
int scale(float input, float rMax, float tMax)
Scale funtion.
Definition: Main.cpp:43
char arduinoCommand[100]
Command to be send to the arduino *&;/.
Definition: Main.cpp:31
Struct to easily add predifined maks values.
Definition: Mask.h:16
Here is the call graph for this function: