Loading...
Searching...
No Matches
Display an image using NanoVG
Prev Tutorial: Display an image using direct framebuffer access
Next Tutorial: Render vector graphics
Original author | Amir Hassan (kallaballa) amir@.nosp@m.viel.nosp@m.-zu.o.nosp@m.rg |
Compatibility | OpenCV >= 4.7 |
Using NanoVG to display images
Instead of feeding to the video pipeline or doing a direct framebuffer access we can use NanoVG to display an image. It is not as convinient as the other methods but it is very fast and flexible.
Downloading...
#include <opencv2/v4d/v4d.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace cv;
using namespace cv::v4d;
//A simple struct to hold our image variables
struct Image_t {
std::string filename_;
nvg::Paint paint_;
int w_;
int h_;
} image_;
public:
//Set the filename
#ifdef __EMSCRIPTEN__
image_.filename_ = "doc/lena.png";
#else
image_.filename_ = samples::findFile("lena.jpg");
#endif
//Creates a NanoVG context. The wrapped C-functions of NanoVG are available in the namespace cv::v4d::nvg;
win->nvg([](Image_t& img) {
using namespace cv::v4d::nvg;
//Create the image_ and receive a handle.
int handle = createImage(img.filename_.c_str(), NVG_IMAGE_NEAREST);
//Make sure it was created successfully
CV_Assert(handle > 0);
//Query the image_ size
imageSize(handle, &img.w_, &img.h_);
//Create a simple image_ pattern with the image dimensions
img.paint_ = imagePattern(0, 0, img.w_, img.h_, 0.0f/180.0f*NVG_PI, handle, 1.0);
}, image_);
}
//Creates a NanoVG context to draw the loaded image_ over again to the screen.
using namespace cv::v4d::nvg;
beginPath();
//Scale all further calls to window size
//Create a rounded rectangle with the images dimensions.
//Note that actually this rectangle will have the size of the window
//because of the previous scale call.
roundedRect(0,0, img.w_, img.h_, 50);
//Fill the rounded rectangle with our picture
fillPaint(img.paint_);
fill();
}, image_, win->fbSize());
}
};
int main() {
window->run<DisplayImageNVG>(0);
}
Definition: v4d.hpp:68
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition: base.hpp:342
Net::Result infer(cv::GOpaque< cv::Rect > roi, T in)
Calculates response for the specified network (template parameter) for the specified region in the so...
Definition: infer.hpp:474
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
Definition: nvg.hpp:20
void roundedRect(float x, float y, float w, float h, float r)
void beginPath()
void fillPaint(Paint paint)
void fill()
Definition: backend.hpp:15
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106
Definition: nvg.hpp:36