Repository for OpenCV's extra modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

59 lines
2.1 KiB

#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_;
};
int main() {
cv::Ptr<V4D> window = V4D::make(960, 960, "Display an Image using NanoVG");
Image_t image;
//Set the filename
#ifdef __EMSCRIPTEN__
image.filename_ = "doc/lena.png";
#else
image.filename_ = samples::findFile("lena.jpg");
#endif
//Create the run loop
window->run([&image](Ptr<V4D> win){
//Execute only once
win->once([win, &image]() {
//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.
win->nvg([](const Image_t& img, const cv::Size& sz) {
using namespace cv::v4d::nvg;
beginPath();
//Scale all further calls to window size
scale(double(sz.width)/img.w_, double(sz.height)/img.h_);
//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());
//Displays the framebuffer in the window. Returns false if the windows has been closed.
return win->display();
});
}