Halide icon indicating copy to clipboard operation
Halide copied to clipboard

Averaging two images

Open djohnkirby opened this issue 12 years ago • 7 comments

Okay, I'm just trying to write a simple program that takes two images and averages their pixels. I tried to define the function thusly:

    average(x, y, c1, c2) = Halide::cast<uint8_t>((Halide::cast<uint32_t>(input1(x,y,c1))*1.0f + Halide::cast<uint32_t>(input2(x,y,c2))*1.0f)*0.5f);

and the schedule I'm defining looks like this:

    Halide::Image<uint8_t> output = average.realize(input1.width(), input2.height(), input1.channels(), input2.channels());

The error it pops out is:

avg: /home/dkirby/Halide-current/include/Halide.h:7491: Halide::Expr Halide::Image::operator()(Halide::Expr, Halide::Expr, Halide::Expr) const [with T = unsigned char]: Assertion `args.size() == (size_t)dims' failed.

I'm not really sure how to interpret this, any hints?

djohnkirby avatar Mar 04 '14 03:03 djohnkirby

Hi Daniel,

Your average Func has 4 dimensions (x, y, c1, c2). So, the realization will also have 4 dims and the final output Func might be expecting a 3-dim one. Since you are trying to average two images channel by channel you could define your Func this way: average(x, y, c) = Halide::cast<uint8_t>((Halide::cast<uint32_t>(input1(x,y,c))_1.0f + Halide::cast<uint32_t>(input2(x,y,c))_1.0f)*0.5f);

Hope it helps.

lleraromero avatar Mar 04 '14 04:03 lleraromero

Changed it to that, changed the realization call to

    Halide::Image<uint8_t> output = average.realize(input1.width(), input2.height(), input1.channels());

And got the same error. I'm a little confused as to what the variable c is specifying. Exactly what is c? Am I passing in a variable into which to save the pixel data? Is it a variable that specifies something about the pixels?

djohnkirby avatar Mar 04 '14 14:03 djohnkirby

That original error was because you were using the three-argument accessor on an Buffer that was not 3-dimensional.

The confusion here is probably that Halide treats color images as 3D arrays. The third dimension is the color channels, so an 640x480 rgb image is represented as a 640x480x3 array in Halide. The 'c' variable then usually represents the color channel index. 0 is red, 1 is green, 2 is blue.

  • Andrew

On Tue, Mar 4, 2014 at 6:27 AM, Daniel John Kirby [email protected]:

Changed it to that, changed the realization call to

Halide::Image<uint8_t> output = average.realize(input1.width(), input2.height(), input1.channels());

And got the same error. I'm a little confused as to what the variable c is specifying. Exactly what is c? Am I passing in a variable into which to save the pixel data? Is it a variable that specifies something about the pixels?

Reply to this email directly or view it on GitHubhttps://github.com/halide/Halide/issues/253#issuecomment-36628452 .

abadams avatar Mar 04 '14 18:03 abadams

Okay, I got it to work properly. What is the proper way to make this accept command line arguments for the image? I'm looking at the code /apps/interpolate/ and I'm not entirely sure what it is doing, but that appears to be a reasonable model.

djohnkirby avatar Mar 04 '14 23:03 djohnkirby

If you're just using the JIT (not statically compiling the Halide pipeline to be run later, in a separate program), interpolate is indeed roughly the right model, though it's more complex than you actually need. The key part is at the end where you see:

Image<float> in_png = load<float>(argv[1]);

That uses the image_io.h convenience code we include in apps/support to define load<type> and save functions for PNG and PPM files to and from Halide::Images. What interpolate then does is actually more complex than you need (using an ImageParam called input to define the image in the pipeline, and then calling set(in_png) on this after the pipeline is constructed). Instead, you can just load a concrete Image<T> (for desired type T you want it to be converted to before input to your Halide pipeline) at the start of your program, then define your pipeline in terms of this. So here, the Image input1 that you're using in your code would be constructed from a loaded image file just line in_png in interpolate.

All that said, these are just convenience functions that make it easy to do this stuff in Halide, without lots of boilerplate or external dependencies. You don't need to load images in any one way or another to use Halide, you just need to package their data up in an Image object (using the JIT) or our very thin buffer_t struct if calling the pipeline directly. You can load that initial data using libJPEG, OpenImageIO, OpenCV, or your own arbitrary code, so long as you can ultimately get out the array of pixel values and know enough about its layout and dimensions.

(But yes, using our load<T> and save helpers is certainly the fastest and easiest way to get going!)

jrk avatar Mar 04 '14 23:03 jrk

Thanks jrk, I have one more question, however. I keep reading in the documentation about JIT and static compilation and the tradeoff involved. And I kno this sounds a little basic, but how do I do each one? If I do g++ a la the tutorials I get an executable but it needs libHalide.so, so that's actually JIT compiling? How would I statically compile, and if I did statically compile would that mean that input images would have to be specified at compile time rather than runtime?

djohnkirby avatar Mar 05 '14 20:03 djohnkirby

I tried to give a more thorough explanation of this here:

https://github.com/halide/Halide/wiki/Static-vs.-JIT-compilation

If you statically compile, you don't need the input images at compile time. They are represented as ImageParams at compile time and then become arguments to the pipeline at runtime. See apps/local_laplacian for an example of this model.

If you JIT compile, you don't have to use ImageParams at all, but can directly use concrete Images (assuming the images, or in particular the image sizes, don't change from one realization to the next). Apps/interpolate JIT compiles, but with ImageParams which are then explicitly set to a concrete image before running. The simples JIT examples would be anything under tests/correctness that uses an Image input.

jrk avatar Mar 05 '14 22:03 jrk