-
Notifications
You must be signed in to change notification settings - Fork 15
Quick Start
Let's start with a vivalib program that access the default camera, extract the image from the camera and display them in a window.
This is a good starting code to check if your development environment is correctly set up.
All the vivalib classes and functions are placed into the viva namespace. Therefore, to access this functionality from your code, use the viva:: specifier or using namespace viva; directive:
#include "viva.h"
using namespace viva;
int main(int argc, const char * argv[])
{
Processor processor;
processor.run();
return 0;
}
The Processor class is the core of the vivalib API. We will always create an instance of this class, initialize some of its members and finally call the run() routine.
To stop the program select the main window (i.e. "Process Output") and press the ESC key.
There are several options to pass a routine to the Processor class in the vivalib framework. To showcase the possibilities let's implement an Edge detector routine that process every frame from the default webcam and display it in a window.
###Using a lambda function:
#include "viva.h"
using namespace viva;
int main(int argc, const char * argv[])
{
auto processFrame = [](const size_t frameN, const Mat &input, Mat &output){
GaussianBlur(input, output, Size(7,7), 1.5, 1.5);
Canny(output, output, 0, 30, 3);
};
Processor processor;
processor.setProcess(processFrame);
processor.run();
return 0;
}The processFrame function is a lambda function that takes three arguments
auto processFrame = [](const size_t frameN, const Mat &input, Mat &output){...}Each time a webcam frame is available, the Processor class will pass the frame to the processFrame lambda function as the input image with its frameN number (i.e. number identifying the number of the frame currently passed to the function). Your algorithm should write the results to the output image. Note that you don't need to display the image in your code. The Processor class will display it from your output in a "Process Output" window.
#include "viva.h"
using namespace viva;
void edge(const size_t frameN, const Mat &input, Mat &output)
{
GaussianBlur(input, output, Size(7,7), 1.5, 1.5);
Canny(output, output, 0, 30, 3);
}
int main(int argc, const char * argv[])
{
Processor processor;
processor.setProcess(&edge);
processor.run();
return 0;
}Similar to the lambda function, you just need to define a function with the same three parameters: frameN(const size_t), input (const Mat&), and output (Mat&).
The lambda function and function pointer are a handy and fast way to prototype your algorithms.
#include "viva.h"
using namespace viva;
class EdgeClass
{
public:
void operator()(const size_t frameN,const Mat &input, Mat &output)
{
GaussianBlur(input, output, Size(7,7), 1.5, 1.5);
Canny(output, output, 0, 30, 3);
}
};
int main(int argc, const char * argv[])
{
EdgeClass edge;
Processor processor;
processor.setProcess(edge);
processor.run();
return 0;
}For more elaborated and modular algorithms we recomend to encapsulate your code in a functor class. You just need to overload the operator() (i.e. the "function call" operator) with the same three parameters explained in previous methods. This way, your class could hold configurations parameters or other members that are only revenant to your solution and not the main.cpp file.
The ProcessFrame interface is a functor class that implements the MouseListener and KeyboardListener classes from the vivalib framework. The ProcessFrame class is available in the vivalib library once you include the "viva.h" header in your file. This method is useful when your application needs to handle mouse or keyboard events.
#include "viva.h"
using namespace viva;
class EdgeClass : public ProcessFrame
{
public:
void operator()(const size_t frameN, const Mat &input, Mat &output)
{
GaussianBlur(input, output, Size(7,7), 1.5, 1.5);
Canny(output, output, 0, 30, 3);
}
void leftButtonDown(int x, int y, int flags)
{
cout << "left click at: "<< x << ", " << y << endl;
};
void keyboardInput(int key)
{
cout << "key " << key << " pressed" << endl;
};
};
int main(int argc, const char * argv[])
{
Ptr<ProcessFrame> edge = new EdgeClass();
Processor processor;
processor.setProcess(edge);
processor.listenToMouseEvents();
processor.listenToKeyboardEvents();
processor.run();
return 0;
}More information about handling mouse and keyboard events can be found in the Handling Mouse and Keyboard Events page.
Continue learning the framework by selecting different inputs and saving your results to disk:
- Batch Processing
- Select an Input Device
- Select an Output Device
- Handling Mouse and Keyboard Events
- Channel Buffer size
- Create your own Input Class
- Create your own Output Class
- Create your own Project
- UML Class Diagram
-
API Reference
- Processor Class
- BatchProcessor Class
- Input Class
- Ouput Class
- ProcessFrame Class
- BatchProcessFrame Class
- MouseListener and KeyboardListener Classes