IRSOL
C++ code implementing socket server for interacting with Baumer camera.
OpenCV Integration

Utilities for interoperability between NeoAPI and OpenCV. More...

Namespaces

namespace  opencv
 Provides utilities for interoperability between NeoAPI and OpenCV.
 

Functions

cv::Mat irsol::opencv::convertNeoImageToCvMat (const NeoAPI::Image &image)
 Converts a NeoAPI::Image (Mono12, non-packed) into an OpenCV cv::Mat.
 
cv::Mat irsol::opencv::createCvMatFromIrsolServerBuffer (unsigned char *data, size_t rows, size_t cols)
 Creates an OpenCV cv::Mat from a raw buffer received from the IRSOL server.
 

Detailed Description

Utilities for interoperability between NeoAPI and OpenCV.

This group contains functions and types that facilitate the conversion of Baumer NeoAPI images to OpenCV cv::Mat objects. The group is only available if the OpenCV library is found on the user's computer at compilation time. If OpenCV is not detected, this header and its contents are excluded from the build.

Function Documentation

◆ convertNeoImageToCvMat()

cv::Mat irsol::opencv::convertNeoImageToCvMat ( const NeoAPI::Image &  image)

Converts a NeoAPI::Image (Mono12, non-packed) into an OpenCV cv::Mat.

Parameters
imageThe NeoAPI::Image to convert containing 12-bit grayscale pixel data. Must be in unpacked Mono12 format (2 bytes per pixel, little endian).
Returns
A cv::Mat of type CV_16UC1 with pixel values in range [0, 65535] (pixel raw values from the NeoAPI::Image 12bit depth are rescaled, so that 0 maps to 0 and 2^12-1 (=4095) maps to 2^16-1 (=65535))
Note
The function assumes the NeoAPI::Image is valid and has an appropriate pixel format compatible with OpenCV.

Definition at line 12 of file opencv.cpp.

13{
14 const size_t width = image.GetWidth();
15 const size_t height = image.GetHeight();
16 const size_t numPixels = width * height;
17
19 image.GetPixelFormat() == NeoAPI::NeoString("Mono12"),
20 "Only 'Mono12' pixel format are supported, got '%s'",
21 image.GetPixelFormat().c_str());
22
23 IRSOL_ASSERT_ERROR(image.GetImageData() != nullptr, "Image data is null");
25 image.GetSize() == numPixels * 2, "Invalid image size for Mono12 unpacked format");
26
27 // Raw image data as bytes
28 const uint8_t* rawData = reinterpret_cast<const uint8_t*>(image.GetImageData());
29
30 // Output Mat with 16-bit unsigned pixels
31 cv::Mat output(height, width, CV_16UC1);
34 for(size_t i = 0; i < numPixels; ++i) {
35 // Each pixel takes 2 bytes: LSB + MSB (little endian), we make sure to mask the top 4 bits,
36 // even if the should already be 0
38 (static_cast<irsol::camera::Pixel<16>::representation>(rawData[2 * i]) |
39 (static_cast<irsol::camera::Pixel<16>::representation>(rawData[2 * i + 1]) << 8)) &
40 0x0FFF;
41 // rescale to map [0, 4095] → [0, 65535] from the 12bit depth to the 16bit depth representation
43 }
44
45 return output;
46}
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
static Pixel< TargetBitDepth >::representation scale(typename Pixel< SourceBitDepth >::representation value)
Scales a pixel value from source bit depth to target bit depth.
typename PixelRepresentation< BitDepth >::type representation
The integral type used to represent pixel values at this bit depth.
auto value

◆ createCvMatFromIrsolServerBuffer()

cv::Mat irsol::opencv::createCvMatFromIrsolServerBuffer ( unsigned char *  data,
size_t  rows,
size_t  cols 
)

Creates an OpenCV cv::Mat from a raw buffer received from the IRSOL server.

This function takes a pointer to a buffer containing image data in unpacked Mono12 format (2 bytes per pixel, little endian, with bytes for each pixel swapped as sent by the server), and constructs a cv::Mat of type CV_16UC1. The function swaps the bytes for each pixel to match OpenCV's expected order, copies the data into the matrix, and rescales the pixel values from 12-bit depth ([0, 4095]) to 16-bit depth ([0, 65535]).

Parameters
dataPointer to the raw image buffer. The buffer must contain at least rows * cols * 2 bytes. The data is modified in-place to swap bytes for each pixel.
rowsNumber of image rows (height).
colsNumber of image columns (width).
Returns
A cv::Mat of type CV_16UC1 containing the image data with pixel values rescaled to 16 bits.
Note
The input buffer is modified in-place due to byte swapping.
The function assumes the buffer is in unpacked Mono12 format as sent by the IRSOL server.
See also
irsol::protocol::Serializer::serializeImageBinaryData
irsol::camera::PixelByteSwapper

This function takes a pointer to a buffer containing image data in unpacked Mono12 format (2 bytes per pixel, little endian, with bytes for each pixel swapped as sent by the server), and constructs a cv::Mat of type CV_16UC1. The function swaps the bytes for each pixel to match OpenCV's expected order, copies the data into the matrix, and rescales the pixel values from 12-bit depth ([0, 4095]) to 16-bit depth ([0, 65535]).

Parameters
dataPointer to the raw image buffer. The buffer must contain at least rows * cols * 2 bytes. The data is modified in-place to swap bytes for each pixel.
rowsNumber of image rows (height).
colsNumber of image columns (width).
Returns
A cv::Mat of type CV_16UC1 containing the image data with pixel values rescaled to 16 bits.
Note
The input buffer is modified in-place due to byte swapping.
The function assumes the buffer is in unpacked Mono12 format as sent by the IRSOL server.

Definition at line 68 of file opencv.cpp.

69{
70 // Create Mat from buffer
71 cv::Mat mat(rows, cols, CV_16UC1);
72 size_t expectedSize = rows * cols * 2; // 2 bytes per pixel for Mono12 unpacked
73
74 // Change the order of the bytes as the server sends the data where bytes for a single pixel are
75 // swapped and OpenCV expects them in the correct order.
76 irsol::camera::PixelByteSwapper<true>()(data, data + expectedSize);
77
78 // Copy the data into the cv::Mat
79 memcpy(mat.data, data, expectedSize);
80
81 // Scale the values from the 12-bit depth to 16-bit depth
83 return mat;
84}
Utility for swapping pixel bytes in a buffer.
Helper to convert pixel values from one bit depth to another with scaling.