2

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.

Gregory
  • 4,147
  • 7
  • 33
  • 44
  • combine pixel values, how? Combining images had me thinking you want them tiled, offset or as a collage. – sehe Dec 03 '17 at 20:21
  • @sehe I mean for each pixel of the result to be a function of corresponding pixels from the input layers. For example, I might want img1 + img2/img3, calculated pixel-by-pixel. Ultimately, I might want to implement this with boost compute, because this arithmetic will be repeated many times and it should be parallelized, but a simple solution is what I need to start. – Gregory Dec 03 '17 at 20:38
  • Boost compute hinges on memory layout if you ask me. So be prepared to adapt your own custom pixel/locator models. Looking – sehe Dec 03 '17 at 20:46

1 Answers1

2

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.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
sehe
  • 374,641
  • 47
  • 450
  • 633