NDEVR
API Documentation
OpenCVImageRaster.h
1#pragma once
2#include "Design/Headers/MaterialRaster.h"
3#include "Design/Headers/ImageFactory.h"
4#include "Design/Headers/Camera.h"
5#include "Design/Headers/SelectionController.h"
6#include "Base/Headers/MatrixFunctions.h"
7#include "Base/Headers/VectorFunctions.hpp"
8#include "Base/Headers/RGBColor.h"
9#include <opencv2/stitching.hpp>
10#include <opencv2/core/hal/interface.h>
11namespace NDEVR
12{
17 {
18 public:
23 OpenCVImageRaster(const String& image_id, const Vector<2, uint04>& size)
24 : m_image_id(image_id)
25 , m_stitcher(cv::Stitcher::create())
26 , m_size(size)
27 {
28 setup();
29 }
30
35 OpenCVImageRaster(const String& image_id, const Vector<2, uint04>& size, const RGBColor& background_color)
36 : m_image_id(image_id)
37 , m_stitcher(cv::Stitcher::create())
38 , m_size(size)
39 {
40 if (background_color != RGBColor(255, 255, 255, 255))
41 setUnsetColor(background_color);
42 setup();
43 }
44
45 void setup()
46 {
47 //m_stitcher->setGP(true);
48 Buffer<uint01> pixels(4 * m_size[X] * m_size[Y], 255);
50 m_stitcher->setRegistrationResol(0.5);
51 m_stitcher->setSeamEstimationResol(0.1);
52 //m_stitcher->setCompositingResol(cv::Stitcher::ORIG_RESOL);
53 /*m_stitcher->setPanoConfidenceThresh(0.5);
54 m_stitcher->setWaveCorrection(true);
55 m_stitcher->setWaveCorrectKind(cv::detail::WAVE_CORRECT_HORIZ);*/
56 m_stitcher->setFeaturesMatcher(new cv::detail::BestOf2NearestMatcher(true));
57 m_stitcher->setBundleAdjuster(new cv::detail::BundleAdjusterRay());
58
59 }
60
63 const String& imageID() const override
64 {
65 return m_image_id;
66 }
67
69 {
70 imgs.clear();
71 }
72
76 {
77 }
78
79 void updateImage() override
80 {
81 cv::Mat pano;// (cv::Size(m_size[X], m_size[Y]), CV_8UC3);
82 cv::Stitcher::Status status = m_stitcher->stitch(imgs, pano);
83 if (status == cv::Stitcher::Status::OK)
84 {
85 Buffer<uint01> data((uint01*)pano.data, pano.dataend - pano.datastart);
86
87 Buffer<uint01> new_pixels(4 * pano.cols * pano.rows, 255U);
88 uint8_t* pixelPtr = (uint8_t*)pano.data;
89 int cn = pano.channels();
90 for (int i = 0; i < pano.rows; i++)
91 {
92 for (int j = 0; j < pano.cols; j++)
93 {
94 for (int dim = 0; dim < 3; dim++)
95 new_pixels[(i * pano.cols + j) * 4 + dim] = pixelPtr[(i * pano.cols + j) * 3 + dim];
96 }
97 }
98 ImageFactory::DefaultFactory().addImageUncompressed(m_image_id, new_pixels, Vector<2, uint04>(pano.cols, pano.rows), true);
99 }
100
101 }
102
107 void addImageBuffer(const Buffer<uint01>& pixels, const Vector<2, uint04>& size, const Matrix<fltp08>& camera_matrix) override
108 {
109
110
111 /*if (imgs.size() == 0)
112 {
113 Buffer<uint01> new_pixels(3 * m_size[X] * m_size[Y], 0);
114 for (int i = 0; i < size[X]; i++)
115 {
116 for (int j = 0; j < size[Y]; j++)
117 {
118 for (int dim = 0; dim < 3; dim++)
119 new_pixels[(i * size[X] + j) * 3 + dim] = pixels[(i * size[X] + j) * 4 + dim];
120 }
121 }
122 cv::Mat mat(cv::Size(m_size[X], m_size[Y]), CV_8UC3, (void*)new_pixels.ptr(), cv::Mat::AUTO_STEP);
123 imgs.push_back(mat.clone());
124 }
125 else*/
126 {
127 uint04 index_size = size[X] * size[Y];
128 Buffer<uint01> new_pixels(3 * index_size, 0);
129 for (uint04 i = 0; i < index_size; i++)
130 {
131 for (uint04 n = 0; n < 3; n++)
132 new_pixels[3 * i + n] = pixels[4 * i + n];
133 }
134 cv::Mat mat(cv::Size(size[X], size[Y]), CV_8UC3, (void*)new_pixels.ptr(), cv::Mat::AUTO_STEP);
135 imgs.push_back(mat.clone());
136 }
137 }
138 protected:
139 cv::Ptr<cv::Stitcher> m_stitcher;
140 std::vector<cv::Mat> imgs;
143 };
144
149 {
153 static void setDefaultMaterialRasterFactory(MaterialRasterFactory* factory)
154 {
155 s_default_raster_factory = factory;
156 }
157 protected:
163 virtual MaterialRasterBase* createRaster(const String& image_id, const RasterInfo& info)
164 {
165 return new OpenCVImageRaster(image_id, info);
166 }
168 };
169}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
void addImageUncompressed(const StringView &id, const Buffer< uint01 > &uncompressed, bool clear_other)
Adds an uncompressed (raw ARGB) image to the cache.
static ImageFactory & DefaultFactory()
Returns the singleton default ImageFactory instance.
Base class for painting pixels at a time into a 2D texture.
MaterialRasterBase(const RasterInfo &raster_info)
Constructs the base raster with the given raster configuration.
Creates rasters for converting points and directions into 2D images.
Templated logic for doing matrix multiplication.
Definition Matrix.hpp:182
A MaterialRasterBase implementation that uses OpenCV for image stitching and raster compositing.
cv::Ptr< cv::Stitcher > m_stitcher
The OpenCV stitcher instance.
void clearRaster()
Clears all accumulated images from the stitching buffer.
String m_image_id
The unique image identifier.
OpenCVImageRaster(const String &image_id, const Vector< 2, uint04 > &size, const RGBColor &background_color)
Constructs an OpenCVImageRaster with a background color.
const String & imageID() const override
Gets the unique image identifier for this raster.
OpenCVImageRaster(const String &image_id, const Vector< 2, uint04 > &size)
Constructs an OpenCVImageRaster with the given image ID and size.
void setup()
Initializes the stitcher and creates the initial blank image.
std::vector< cv::Mat > imgs
Accumulated images awaiting stitching.
void setUnsetColor(RGBColor color)
Sets the color used for unset pixels.
void updateImage() override
Stitches all accumulated images and updates the raster output.
Vector< 2, uint04 > m_size
The pixel dimensions of the output raster.
void addImageBuffer(const Buffer< uint01 > &pixels, const Vector< 2, uint04 > &size, const Matrix< fltp08 > &camera_matrix) override
Adds an image buffer to the stitching queue.
A MaterialRasterFactory implementation that creates OpenCV-based raster instances.
static MaterialRasterFactory * s_default_raster_factory
The default raster factory instance.
virtual MaterialRasterBase * createRaster(const String &image_id, const RasterInfo &info)
Creates a new OpenCVImageRaster instance.
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
The core String class for the NDEVR API.
Definition String.h:95
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Stores information for painting pixels at a time into a 2D texture used with MaterialRaster.