OpenCvMat

Inherits Mat.

Public Types

enum ColorType

Public Member Functions

template<class T >
OpenCvMat(const T* matOrImg)
OpenCvMat(const kuvio_image* img, ColorType colorType)
OpenCvMat(kuvio_image* img, ColorType colorType)

Detailed Description

A class that wraps a kuvio_image or a kuvio_matrix to an OpenCV matrix (cv::Mat) so that the C types are usable in OpenCV calls. This is a non-copyable class that is intended to be used as a temporary wrapper in function calls.

Due to the lack of a standard ABI for C++, the implementation of this class cannot be used from a precompiled library but must be compiled into the calling code. To use the class you must thus add OpenCvMat.cc into your project's source files.

Note
cv::Mat does not implement the copy-on-write paradigm that is the standard convention on reference-counted objects elsewhere. Therefore, the contents of a wrapped image or a matrix may be modified if the called function modifies the data, even though the image or matrix was originally passed as a const pointer. To make things worse, OpenCV function declarations don't use const to distinguish between input and output data, and the documentation isn't always clear on this either. There is no efficient way around this deficiency, so be careful. Make a deep copy of the kuvio_image or kuvio_matrix if unsure.

Examples:

using Kuvio::OpenCvMat;
void gaussianBlur(const kuvio_image* input,
int filterSize,
kuvio_image* output)
{
filterSize |= 1; // Make odd
cv::GaussianBlur(
OpenCvMat(input),
OpenCvMat(output),
cv::Size(filterSize, filterSize),
0, 0);
}
void filter2d(const kuvio_image* input,
const kuvio_dmatrix* kernel,
kuvio_image* output)
{
cv::filter2D(
OpenCvMat(input),
OpenCvMat(output),
-1, // Default color channel depth
OpenCvMat(kernel));
}

Member Enumeration Documentation

OpenCV color types to which automatic conversions from kuvio_image are supported.

Enumerator
Gray

Any one-channel (gray level) color type.

Rgb24

24-bit RGB color type (BGR).

Rgb32

32-bit RGB color type (BGRA).

Constructor & Destructor Documentation

template<class T >
OpenCvMat::OpenCvMat (const T* matOrImg) inline

Constructs a cv::Mat that contains a reference to the data in matOrImg, which may be either a kuvio_image or any kuvio_matrix type.

OpenCvMat::OpenCvMat (const kuvio_image* img, ColorType colorType )

Creates a cv::Mat with the specified color type. If the number of color channels in img is not the same as in colorType, a deep copy of the data in img will be created.

Some (but not all) OpenCV functions assume that the input image has three channels instead of four and throw an error if a four-channel image is used. This is unfortunate as it places the responsibility of choosing the correct number of channels to the caller. This constructor can be used in such situations. It can also automatically convert a gray-level image to RGB and vice versa.

OpenCvMat::OpenCvMat (kuvio_image* img, ColorType colorType )

Creates a cv::Mat with the specified OpenCV color type. If the number of color channels in img is not the same as in the target color type, a deep copy of the data in img will be created.

When an OpenCvMat is constructed this way, the temporary copy is automatically copied back to img when the OpenCvMat object goes out of scope. Thus, any modifications made to the temporary image in a function call are propagated to img.

using Kuvio::OpenCvMat;
// floodFill accepts only 3- and 1-channel images.
// Assuming inImg is 32-bit RGB
kuvio_image* pFilled = kuvio_image_clone(inImg);
cv::floodFill(OpenCvMat(pFilled, OpenCvMat::Rgb24),
cv::Point(100, 100),
cv::Scalar(127, 0, 19));
// pFilled now contains the result in 32-bit RGB