gil-boost:将rgb8_image_t转换为rgba8_imaget
gil boost : convert rgb8_image_t to rgba8_image_t
我对GIL语法有点困惑。我想转换
rgb8_image_t
至
rgba8_image_t
并将alpha通道设置为1。有内置的功能吗。如果没有,如何手动完成?
您希望将boost::gil::copy_and_convert_pixels
与范围中合适的匹配color_convert
专业化一起使用。
这里有一个完整的例子:
#include <boost/gil/gil_all.hpp>
#include <cassert>
namespace boost { namespace gil {
// Define a color conversion rule NB in the boost::gil namespace
template <> void color_convert<rgb8_pixel_t,rgba8_pixel_t>(
const rgb8_pixel_t& src,
rgba8_pixel_t& dst
) {
// Well we _could_ just write...
// dst[0]=src[0];
// dst[1]=src[1];
// dst[2]=src[2];
// dst[3]=255;
// ...but that'd be too easy / not as generic as it could be
// so let's go crazy...
get_color(dst,red_t())=get_color(src,red_t());
get_color(dst,green_t())=get_color(src,green_t());
get_color(dst,blue_t())=get_color(src,blue_t());
typedef color_element_type<rgba8_pixel_t,alpha_t>::type alpha_channel_t;
get_color(dst,alpha_t())=channel_traits<alpha_channel_t>::max_value();
}
}
}
int main(int,char**) {
// Create a 1x1 RGB image and set its pixel to a color
boost::gil::rgb8_image_t a(1,1);
boost::gil::view(a)(0,0)=boost::gil::rgb8_pixel_t(1,2,3);
// Create a 1x1 RGBA
boost::gil::rgba8_image_t b(1,1);
// Copy AND CONVERT
boost::gil::copy_and_convert_pixels(
boost::gil::const_view(a),
boost::gil::view(b)
);
// Check the alpha has been set as expected
const boost::gil::rgba8_pixel_t p=boost::gil::const_view(b)(0,0);
assert(p==boost::gil::rgba8_pixel_t(1,2,3,255));
return 0;
}
或者,也有copy_and_convert_pixels
重载(见文档),它接受显式的颜色转换函子,但对于像使RGB图像在转换时具有隐式的最大alpha这样没有争议的事情,似乎没有什么理由不定义它在默认情况下的拾取位置。
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 将无符号char*转换为std::istream*C++