将 boost::gil::color_converted_view 与 boost::gil::for_each_pixel 结合使用

发布于 2024-10-05 15:35:43 字数 984 浏览 4 评论 0原文

我意识到写入 gil::color_converted_view 不会影响底层视图的数据。我想知道这是否正确?

例如,假设我想编写一个程序,该程序将获取红色通道的值并将蓝色通道的值设置为该值的一半。这是我失败的尝试:

template <typename SrcView>
void half_red_to_blue(SrcView & view)
{
    // Since SrcView might be RGB or BGR or some other types,
    // I decided to use a color_converted_view to ensure that I'm
    // accessing the correct channels
    typedef gil::color_converted_view_type<SrcView, gil::rgb8_pixel_t>::type MyView;
    MyView my_view = gil::color_converted_view<gil::rgb8_pixel_t>(view):
    struct my_lambda
    {
        void operator()(gil::rgb8_pixel_t & p)
        {
            p[2] = p[0] / 2;
        }
    };
    gil::for_each_pixel(my_view, my_lambda());
}

但是,它仅在 SrcView 实际上是 gil::rgb8_view_t 时才有效。如果我调用,例如 half_red_to_blue(view),视图根本不会改变!我在调试器中检查了一些,似乎写入操作正在写入某种代理位置而不是原始像素。

有什么想法吗?提前致谢!

I realized that writing to gil::color_converted_view doesn't affect the underlying view's data. I wonder if that's correct?

For example, let's say that I want to write a program that will take the value of the red channel and set the blue channel's value to half of that. Here's my failed attempt:

template <typename SrcView>
void half_red_to_blue(SrcView & view)
{
    // Since SrcView might be RGB or BGR or some other types,
    // I decided to use a color_converted_view to ensure that I'm
    // accessing the correct channels
    typedef gil::color_converted_view_type<SrcView, gil::rgb8_pixel_t>::type MyView;
    MyView my_view = gil::color_converted_view<gil::rgb8_pixel_t>(view):
    struct my_lambda
    {
        void operator()(gil::rgb8_pixel_t & p)
        {
            p[2] = p[0] / 2;
        }
    };
    gil::for_each_pixel(my_view, my_lambda());
}

However, it only works when SrcView is actually gil::rgb8_view_t. If I call, e.g. half_red_to_blue<gil::bgr8_view_t>(view), the view is not changed at all! I inspected a little in the debugger and it seems the write operation is writing to some kind of proxy location instead of the original pixels.

Any ideas? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩发小公举 2024-10-12 15:35:43

这是 Boost.GIL 中的有效行为,因为像素的颜色分量仅在访问像素时才会被触及。您可以修改 my_lambda::operator() 以使用 get_color 来触发颜色组件访问。

This is valid behaviour in Boost.GIL because the colour components of pixel are touched only upon access of pixel. You can modify my_lambda::operator() to use get_color to trigger colour component access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文