How to combine images with boost gil?

依然范特西╮ 提交于 2021-02-10 06:58:18

问题


I am just getting familiar with Boost GIL (and image processing in general) and suspect that this is simple, but I haven't found the relevant documentation.

I have a set of image views that I would like to combine with an arbitrary function. For simplicity, lets say the images are aligned (same size and locator type) and I just want to add the pixel values together. One approach would be to create a combining iterator from a zip_iterator and a transform_iterator, but I'm guessing that there are image processing algorithms that are conveniently abstracted for this purpose.

The Mandelbrot example in the documentation is probably relevant, because it computes pixel values from a function, but I'm getting lost in the details and having trouble adapting it to my case.


回答1:


The only binary channel algorithm I can find is channel_multiply.

The algorithm you're probably really looking for is transform_pixels which does combine in a binary variation.

Here's the simplest example I could make.

#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/png_io.hpp>

namespace gil = boost::gil;

int main() {
    using Img = gil::rgba8_image_t;
    using Pix = Img::value_type;
    Img a, b;
    gil::png_read_image("/tmp/a.png", a);
    gil::png_read_image("/tmp/b.png", b);

    assert(a.dimensions() == b.dimensions());
    Img c(a.dimensions());

    gil::transform_pixels(view(a), view(b), view(c), [](gil::rgba8_ref_t a, gil::rgba8_ref_t b) {
            gil::red_t   R;
            gil::green_t G;
            gil::blue_t  B;
            gil::alpha_t A;
            return Pix (
                    get_color(a, R) + get_color(b, R),
                    get_color(a, G) + get_color(b, G),
                    get_color(a, B) + get_color(b, B),
                    get_color(a, A) + get_color(b, A)
                );
            });

    gil::png_write_view("/tmp/c.png", view(c));
}

When a.png is and b.png is (note transparencies too), c.png became (again, note the transparencies).

You will want to fine-tune the transformation function to do something more useful suppose.



来源:https://stackoverflow.com/questions/47622796/how-to-combine-images-with-boost-gil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!