YARP
Yet Another Robot Platform
os/image_process_module/image_process_module.cpp

This is the os/image_process/image_process.cpp example rewritten as a "module".It inherits better starting up, configuring, and shutting down behavior.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <yarp/os/Port.h>
#include <yarp/sig/Image.h>
/*
This example adds a moving circle to an image stream.
It is the same as image_process.cpp, except it is built
as a Yarp "Module". This makes it a bit cleaner to start/stop.
Suppose we have an image source on port /source such as:
yarpdev --device fakeFrameGrabber --name /source --mode line --framerate 10
And suppose we have an image viewer on port /view:
yarpview --name /view
Then we can hook this program up in between as follows:
./image_process --name /worker
yarp connect /source /worker
yarp connect /worker /view
You should see the normal scrolling line of fakeFrameGrabber, with a moving
circle overlaid.
*/
class ImageProcessModule : public RFModule
{
private:
// Make a port for reading and writing images
BufferedPort<ImageOf<PixelRgb>> port;
Port cmdPort;
size_t ct;
public:
bool open(Searchable& config)
{
YARP_UNUSED(config);
ct = 0;
port.open(getName());
cmdPort.open(getName("cmd")); // optional command port
attach(cmdPort); // cmdPort will work just like terminal
return true;
}
// try to interrupt any communications or resource usage
bool interruptModule() override
{
port.interrupt();
return true;
}
bool updateModule() override
{
ImageOf<PixelRgb>* img = port.read();
if (img == nullptr) {
return false;
}
// add a blue circle
PixelRgb blue{0, 0, 255};
addCircle(*img, blue, ct, 50, 10);
ct = (ct + 5) % img->width();
// output the image
port.prepare() = *img;
port.write();
return true;
}
};
int main(int argc, char* argv[])
{
// Initialize the yarp network
Network yarp;
/* prepare and configure the resource finder */
auto& rf = ResourceFinder::getResourceFinderSingleton();
rf.configure(argc, argv);
// Create and run our module
ImageProcessModule module;
module.setName("/worker");
return module.runModule(rf);
}
yarp::os::RFModule
A base-class for standard YARP modules that supports ResourceFinder.
Definition: RFModule.h:24
Network.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Port.h
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
ImageDraw.h
yarp::sig::ImageOf
Typed image class.
Definition: Image.h:647
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
module
static RFModule * module
Definition: RFModule.cpp:234
RFModule.h
BufferedPort.h
Image.h
yarp::sig::PixelRgb
Packed RGB pixel type.
Definition: Image.h:453
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::sig::draw::addCircle
void addCircle(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition: ImageDraw.h:43
ResourceFinder.h
yarp::os::ResourceFinder
Helper class for finding config files and other external resources.
Definition: ResourceFinder.h:33