我从模拟器中以 a 的形式获取图像流,gil::image_view
需要将它们转换为以cv::Mat
进行进一步处理。到目前为止,我或多或少地从这个答案中复制了代码,但并没有真正理解它:
auto image_view = // data received from an API
using pixel = decltype(image_view)::value_type;
static_assert(sizeof(pixel) == 4, "RGBA");
pixel raw_data[image_view.width() * image_view.height()];
boost::gil::copy_pixels(image_view, boost::gil::interleaved_view(image_view.width(),
image_view.height(),
raw_data,
image_view.width() *
sizeof(pixel)));
auto mat = cv::Mat(image_view.height(), image_view.width(), CV_8UC4, raw_data);
我确定没有使用alpha通道,所以我还可以定义另一个image_view:
auto rgb_view = boost::gil::color_converted_view<boost::gil::rgb8_pixel_t>(image_view);
using pixel = decltype(rgb_view)::value_type;
static_assert(sizeof(pixel) == 3, "RGB");
...
对于这种情况,使用复制像素boost::gil::copy_pixels(...)
是有意义的,因为无法在恒定时间内将交错的 rgba8 转换为 rgb8。
鉴于应用程序的性质,我很确定图像已经在内存中的某个地方。因此,从技术上讲,我可以只使用指向第一个元素的指针来创建我的 OpenCV 图像,而代价是使用额外的通道。