Skip to content
Andrés Solís Montero edited this page Nov 10, 2015 · 8 revisions

Using your code with the vivalib framework

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.

Create your algorithm

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.

Using a pointer to a function

#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.

Using a functor class

#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.

Using the ProcessFrame interface

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:

Clone this wiki locally