IRSOL
C++ code implementing socket server for interacting with Baumer camera.
opencv.cpp
Go to the documentation of this file.
1#include "irsol/opencv.hpp"
2
3#include "irsol/assert.hpp"
5
6#include <neoapi/neoapi.hpp>
7#include <opencv2/opencv.hpp>
8
9namespace irsol {
10namespace opencv {
11cv::Mat
12convertNeoImageToCvMat(const NeoAPI::Image& image)
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}
47
67cv::Mat
68createCvMatFromIrsolServerBuffer(unsigned char* data, size_t rows, size_t cols)
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}
85
86} // namespace opencv
87} // namespace irsol
Assertion macros and utilities based on the PPK_ASSERT library.
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
cv::Mat createCvMatFromIrsolServerBuffer(unsigned char *data, size_t rows, size_t cols)
Creates an OpenCV cv::Mat from a raw buffer received from the IRSOL server.
Definition opencv.cpp:68
cv::Mat convertNeoImageToCvMat(const NeoAPI::Image &image)
Converts a NeoAPI::Image (Mono12, non-packed) into an OpenCV cv::Mat.
Definition opencv.cpp:12
Provides utilities for interoperability between NeoAPI and OpenCV.
OpenCV integration utilities for NeoAPI image handling.
Utilities for managing and transforming pixel formats between each other.
Utility for swapping pixel bytes in a buffer.
Helper to convert pixel values from one bit depth to another with scaling.
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