boost :: gil库的记忆功能 [英] Memory function of the boost::gil library
问题描述
我目前正在尝试使用 Boost :: GIL
(具有挑战性)进行一些TensorFlow推理(C后端).我需要一些思考,我已经能够加载我的png图片( rgb8_image_t
)并转换为 rgb32_f_image_t
.
I am currently trying to make some TensorFlow Inference (C backend) using Boost::GIL
(challenging). I need a few thinks, I have been able to load my png image (rgb8_image_t
)
and did a conversion to rgb32_f_image_t
.
我仍然需要3种想法,即数据的原始指针,分配的内存和尺寸.
I still need 3 thinks, the raw pointer of the data, memory allocated, and dimensions.
-
不幸的是,对于分配的内存,功能
total_allocated_size_in_bytes()
是私有的,所以我这样做了:
for the memory allocated unfortunately the function
total_allocated_size_in_bytes()
is private, so I did this:
boost :: gil :: view(dest).size()* boost :: gil :: view(dest).num_channels()* sizeof(value_type);
如果我没有关于对齐故事的任何额外填充,那是有效的.但这是否存在任何不错的选择?
Which is valid, if I do not have any extra padding for alignment story. But does it exist any nice alternative?
-
对于尺寸,我应该与
numpy
(来自PILLOW)匹配,我希望两个库都使用相同的内存布局模式.根据我的理解,默认情况下,数据是交错和连续的,所以应该很好.
For the dimension, I should match with
numpy
(from PILLOW), I hope both libraries are using the same memory layout pattern. From my understanding, by default, datas are interleaved and contiguous so, it should be good.
最后一个原始指针 _memory
,它是 Image
类的私有数据成员,没有专用功能. boost :: gil :: view(dest).row_begin(0)
返回第一个像素的迭代器,但我不确定如何获取数据的指针 _memory
.有什么建议吗?
Last the raw pointer _memory
, it is a private data member of the Image
class with no dedicated function. boost::gil::view(dest).row_begin(0)
returns a iterator on the first pixel but I not sure how I could get the pointer of the data _memory
. Any suggestions ?
非常感谢您,
++ t
ps:TensorFlow提出了一个C ++后端,但是,没有从任何软件包管理器中安装它,并且操纵Bazel超出了我的能力.
ps: TensorFlow proposes a C++ backend, however, it is not installed from any package managers, and manipulate Bazel is beyond my strength.
推荐答案
GIL文档非常准确地记录了各种内存布局.
GIL documentation pretty accurately documents the various memory layouts.
但是,库的要点是抽象出内存布局.如果您需要某种表示形式(平面/交错,打包或解压缩),则说明库接口的工作很困难.
The point of the library, though, is to abstract away the memory layouts. If you require some representation (planar/interleaved, packed or unpacked) you are doing things "the hard way" for the library interface.
因此,我认为您可以一次性阅读和转换,例如对于jpeg:
So, I think you can read and convert in one go, e.g. for a jpeg:
gil::rgb32f_image_t img;
gil::image_read_settings<gil::jpeg_tag> settings;
read_and_convert_image("input.jpg", img, settings);
现在可以获取原始数据了:
Now getting the raw data is possible:
auto* raw_data = gil::interleaved_view_get_raw_data(view(img));
碰巧的情况是,首选的实现存储是交错的,这很可能是您所期望的.如果您的特定图像存储是平面的,则该调用将不会编译(您可能需要
planar_view_get_raw_data(vw,plane_index)
代替).
请注意,如果需要的话,您将不得不重新解释_cast到 float [const] *
,因为没有公共接口来获取对 scoped_channel_value<> ;:的引用:value _
,但是 BaseChannelValue
类型确实是 float
,并且您可以断言包装器不会增加额外的权重:
Note that you'll have to reinterpret_cast to float [const]*
if you need that, because there is not public interface to get a reference to the scoped_channel_value<>::value_
, but the BaseChannelValue
type is indeed float
and you can assert that the wrapper doesn't add additional weight:
static_assert(sizeof(float) == sizeof(raw_data[0]));
替代方法:
相反,您可以设置自己的原始像素缓冲区,在其中安装可变视图,然后使用该视图将初始负载读取/转换为:
Alternative Approach:
Conversely, you can setup your own raw pixel buffer, mount a mutable view into it and use that to read/convert your initial load into:
// get dimension
gil::image_read_settings<gil::jpeg_tag> settings;
auto info = gil::read_image_info("input.jpg", settings).get_info();
// setup raw pixel buffer & view
using pixel = gil::rgb32f_pixel_t;
auto data = std::make_unique<pixel[]>(info._width * info._height);
auto vw = gil::interleaved_view(info._width, info._height, data.get(),
info._width * sizeof(pixel));
// load into buffer
read_and_convert_view("input.jpg", vw, settings);
我实际上通过写出结果视图来检查它是否正常工作:
I've actually checked that it works correctly by writing out the resulting view:
//// just for test - doesn't work for 32f, so choose another pixel format
//gil::write_view("output.png", vw, gil::png_tag());
这篇关于boost :: gil库的记忆功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!