Loading...
Searching...
No Matches
Custom Source and Sink

Prev Tutorial: Video editing
Next Tutorial: Form based GUI

Original author Amir Hassan (kallaballa) amir@.nosp@m.viel.nosp@m.-zu.o.nosp@m.rg
Compatibility OpenCV >= 4.7

Reading and writing to V4D using custom sources and sinks

In the previous tutorial we used a default video source and a video sink to stream a video through V4D which can be manipulated using OpenGL, NanoVG or OpenCV. In this example we are creating a custom source that generates rainbow frames. For each time the source is invoked the frame is colored a slightly different color. Additionally the custom sink saves individual images instead of a video (only in native builds).

Custom Source and Sink
Downloading...
#ifndef __EMSCRIPTEN__
#endif
using namespace cv;
using namespace cv::v4d;
int main() {
Ptr<V4D> window = V4D::make(960, 960, "Custom Source/Sink", false, false, 0);
string hr = "Hello Rainbow!";
//Make a source that generates rainbow frames.
Source src([](cv::UMat& frame){
static long cnt = 0;
//The source is responsible for initializing the frame..
if(frame.empty())
frame.create(Size(960, 960), CV_8UC3);
frame = colorConvert(Scalar(++cnt % 180, 128, 128, 255), COLOR_HLS2BGR);
return true;
}, 60.0f);
//Make a sink the saves each frame to a PNG file (does nothing in case of WebAssembly).
Sink sink([](const cv::UMat& frame){
try {
#ifndef __EMSCRIPTEN__
static long cnt = 0;
imwrite(std::to_string(cnt++) + ".png", frame);
#else
CV_UNUSED(frame);
#endif
} catch(std::exception& ex) {
cerr << "Unable to write frame: " << ex.what() << endl;
return false;
}
return true;
});
//Attach source and sink
window->setSource(src);
window->setSink(sink);
window->run([hr](cv::Ptr<V4D> win) {
if(!win->capture())
return false;
//Render "Hello Rainbow!" over the video
win->nvg([hr](const Size& sz) {
using namespace cv::v4d::nvg;
fontSize(40.0f);
fontFace("sans-bold");
fillColor(Scalar(255, 0, 0, 255));
textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
text(sz.width / 2.0, sz.height / 2.0, hr.c_str(), hr.c_str() + hr.size());
});
win->write();
return win->display();
});
}
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:335
_Tp height
the height
Definition: types.hpp:363
_Tp width
the width
Definition: types.hpp:362
Definition: mat.hpp:2432
bool empty() const
returns true if matrix data is NULL
void create(int rows, int cols, int type, UMatUsageFlags usageFlags=USAGE_DEFAULT)
allocates new matrix data unless the matrix already has specified size and type.
Definition: sink.hpp:19
Definition: source.hpp:20
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
#define CV_8UC3
Definition: interface.h:90
Definition: nvg.hpp:19
void textAlign(int align)
void fontSize(float size)
void fontFace(const char *font)
void fillColor(const cv::Scalar &color)
float text(float x, float y, const char *string, const char *end)
Definition: framebuffercontext.hpp:25
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106