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).
Downloading...
#include <opencv2/v4d/v4d.hpp>
#ifndef __EMSCRIPTEN__
# include <opencv2/imgcodecs.hpp>
#endif
using namespace cv;
using namespace cv::v4d;
int main() {
string hr = "Hello Rainbow!";
//Make a source that generates rainbow frames.
static long cnt = 0;
//The source is responsible for initializing the frame..
if(frame.empty())
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).
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");
textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
});
win->write();
return win->display();
});
}
Definition: mat.hpp:2432
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
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