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;
string hr_ = "Hello Rainbow!";
public:
win->capture();
//Render "Hello Rainbow!" over the video
using namespace cv::v4d::nvg;
fontSize(40.0f);
fontFace("sans-bold");
fillColor(Scalar(255, 0, 0, 255));
textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
}, win->fbSize(), hr_);
win->write();
}
};
int main() {
Ptr<V4D> window = V4D::make(960, 960, "Custom Source/Sink");
//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__
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);
}
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: v4d.hpp:68
Definition: sink.hpp:20
Definition: source.hpp:21
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=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