How to combine images with boost gil?

618 views Asked by At

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

There are 1 answers

0
sehe On BEST ANSWER

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 enter image description here and b.png is enter image description here (note transparencies too), c.png became enter image description here (again, note the transparencies).

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