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;
class CustomSourceAndSinkPlan : public Plan {
string hr_ = "Hello Rainbow!";
public:
void infer(cv::Ptr<V4D> win) override {
win->capture();
//Render "Hello Rainbow!" over the video
win->nvg([](const Size& sz, const string& str) {
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, str.c_str(), str.c_str() + str.size());
}, win->fbSize(), hr_);
win->write();
}
};
int main() {
Ptr<V4D> window = V4D::make(960, 960, "Custom Source/Sink");
//Make a source that generates rainbow frames.
cv::Ptr<Source> src = new Source([](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).
cv::Ptr<Sink> sink = new Sink([](const uint64_t& seq, const cv::UMat& frame){
try {
#ifndef __EMSCRIPTEN__
imwrite(std::to_string(seq) + ".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<CustomSourceAndSinkPlan>(0);
}
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: v4d.hpp:68
Definition: sink.hpp:20
Definition: source.hpp:21
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
#define CV_8UC3
Definition: interface.h:90
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
Definition: nvg.hpp:20
Definition: backend.hpp:15
cv::Scalar colorConvert(const cv::Scalar &src, cv::ColorConversionCodes code)
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106
::uint64_t uint64_t
Definition: cvdef.h:851