Last active
April 17, 2018 20:08
-
-
Save sschnug/c8eeaf5cd81eb1ec04220fb36bb202c7 to your computer and use it in GitHub Desktop.
Halide: First steps -> block-based mean operation http://stackoverflow.com/questions/43455570/halide-how-to-process-image-in-overlapping-blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Halide.h" | |
#include "Halide/tools/halide_image_io.h" | |
#include <iostream> | |
// This code calculates a block based mean on some a-priori known image-dimensions (1 uint8_t channel) | |
// An example image to process: http://i.imgur.com/Eyo0Xvc.png | |
// No customized scheduling within this code, but the SO-answer gives some recommendation! | |
int main(int argc, char **argv) { | |
Halide::Buffer<uint8_t> input = Halide::Tools::load_image("TestImages/block_example.png"); | |
// This is a simple example assumes an input of 64x128 | |
std::cout << "dim 0: " << input.width() << std::endl; | |
std::cout << "dim 1: " << input.height() << std::endl; | |
// The "outer" (block) and "inner" (pixel) indices that describe a pixel in a tile. | |
Halide::Var xo, yo, xi, yi, x, y; | |
// The distance between the start of each tile in the input. | |
int tile_stride_x = 32; | |
int tile_stride_y = 64; | |
int tile_size_x = 32; | |
int tile_size_y = 64; | |
Halide::Func tiled_f; | |
tiled_f(xi, yi, xo, yo) = input(xo * tile_stride_x + xi, yo * tile_stride_y + yi); | |
Halide::RDom tile_dom(0, tile_size_x, 0, tile_size_y); | |
Halide::Func tile_means; | |
tile_means(xo, yo) = sum(Halide::cast<uint32_t>(tiled_f(tile_dom.x, tile_dom.y, xo, yo))) / (tile_size_x * tile_size_y); | |
Halide::Func output; | |
output(xo, yo) = Halide::cast<uint8_t>(tile_means(xo, yo)); | |
Halide::Buffer<uint8_t> output_(2, 2); | |
output.realize(output_); | |
Halide::Tools::save_image(output_, "block_based_stuff.png"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dsharletg Amazing. Indeed the buffer should be uint8 before using Halide::Tools::save_image. And the cast-before-output is now working too (i don't know what went wrong before). Everything is fine now! Thanks for all your help! I will add this tiny example to my SO-question as edit based on your answer and mark it as accepted.