diff --git a/include/hdrplus/merge.h b/include/hdrplus/merge.h index d3aa550..bc098dc 100644 --- a/include/hdrplus/merge.h +++ b/include/hdrplus/merge.h @@ -5,12 +5,15 @@ #include #include "hdrplus/burst.h" +#define TILE_SIZE 16 + namespace hdrplus { class merge { public: + int offset = TILE_SIZE / 2; float baseline_lambda_shot = 3.24 * pow( 10, -4 ); float baseline_lambda_read = 4.3 * pow( 10, -6 ); @@ -29,7 +32,7 @@ class merge std::vector>>>& alignments); private: - cv::Mat cosineWindow1D(cv::Mat input, int window_size = 16) { + cv::Mat cosineWindow1D(cv::Mat input, int window_size = TILE_SIZE) { cv::Mat output = input.clone(); for (int i = 0; i < input.cols; ++i) { output.at(0, i) = 1. / 2. - 1. / 2. * cos(2 * M_PI * (input.at(0, i) + 1 / 2.) / window_size); @@ -55,7 +58,21 @@ class merge return window_applied; } + cv::Mat cat2Dtiles(std::vector> tiles) { + std::vector rows; + for (auto row_tiles : tiles) { + cv::Mat row; + cv::hconcat(row_tiles, row); + rows.push_back(row); + } + cv::Mat img; + cv::vconcat(rows, img); + return img; + } + std::vector getReferenceTiles(cv::Mat reference_image); + + cv::Mat mergeTiles(std::vector tiles, int rows, int cols); }; } // namespace hdrplus diff --git a/src/merge.cpp b/src/merge.cpp index ed7602b..0d20a53 100644 --- a/src/merge.cpp +++ b/src/merge.cpp @@ -13,27 +13,95 @@ void merge::process( const hdrplus::burst& burst_images, \ double lambda_shot, lambda_read; std::tie(lambda_shot, lambda_read) = burst_images.bayer_images[burst_images.reference_image_idx].get_noise_params(); - // Obtain tiles - cv::Mat reference_image = burst_images.grayscale_images_pad[0]; + // Obtain Reference tiles + cv::Mat reference_image = burst_images.grayscale_images_pad[burst_images.reference_image_idx]; std::vector reference_tiles = getReferenceTiles(reference_image); + // Temporal Denoising + + // Spatial Denoising + + // Process tiles through 2D cosine window + std::vector windowed_tiles; + for (auto tile : reference_tiles) { + windowed_tiles.push_back(cosineWindow2D(tile)); + } + + // Merge tiles + cv::Mat merged = mergeTiles(windowed_tiles, reference_image.rows, reference_image.cols); + // cv::Mat outputImg = reference_image.clone(); // cv::cvtColor(outputImg, outputImg, cv::COLOR_GRAY2RGB); // cv::imwrite("ref.jpg", outputImg); + // cv::Mat outputImg1 = reference_tiles[0].clone(); // cv::Mat outputImg1 = cosineWindow2D(reference_tiles[0].clone()); + // cv::Mat outputImg1 = cat2Dtiles(tiles_2D); // cv::cvtColor(outputImg1, outputImg1, cv::COLOR_GRAY2RGB); // cv::imwrite("tile0.jpg", outputImg1); } std::vector merge::getReferenceTiles(cv::Mat reference_image) { std::vector reference_tiles; - for (int y = 0; y < reference_image.rows - 8; y += 8) { - for (int x = 0; x < reference_image.cols - 8; x += 8) { - cv::Mat tile = reference_image(cv::Rect(x, y, 16, 16)); + for (int y = 0; y < reference_image.rows - offset; y += offset) { + for (int x = 0; x < reference_image.cols - offset; x += offset) { + cv::Mat tile = reference_image(cv::Rect(x, y, TILE_SIZE, TILE_SIZE)); reference_tiles.push_back(tile); } } return reference_tiles; } +cv::Mat merge::mergeTiles(std::vector tiles, int num_rows, int num_cols){ + // 1. get all four subsets: original (evenly split), horizontal overlapped, + // vertical overlapped, 2D overlapped + std::vector> tiles_original; + for (int y = 0; y < num_rows / offset - 1; y += 2) { + std::vector row; + for (int x = 0; x < num_cols / offset - 1; x += 2) { + row.push_back(tiles[y * (num_cols / offset - 1) + x]); + } + tiles_original.push_back(row); + } + + std::vector> tiles_horizontal; + for (int y = 0; y < num_rows / offset - 1; y += 2) { + std::vector row; + for (int x = 1; x < num_cols / offset - 1; x += 2) { + row.push_back(tiles[y * (num_cols / offset - 1) + x]); + } + tiles_horizontal.push_back(row); + } + + std::vector> tiles_vertical; + for (int y = 1; y < num_rows / offset - 1; y += 2) { + std::vector row; + for (int x = 0; x < num_cols / offset - 1; x += 2) { + row.push_back(tiles[y * (num_cols / offset - 1) + x]); + } + tiles_vertical.push_back(row); + } + + std::vector> tiles_2d; + for (int y = 1; y < num_rows / offset - 1; y += 2) { + std::vector row; + for (int x = 1; x < num_cols / offset - 1; x += 2) { + row.push_back(tiles[y * (num_cols / offset - 1) + x]); + } + tiles_2d.push_back(row); + } + + // 2. Concatenate the four subsets + cv::Mat img_original = cat2Dtiles(tiles_original); + cv::Mat img_horizontal = cat2Dtiles(tiles_horizontal); + cv::Mat img_vertical = cat2Dtiles(tiles_vertical); + cv::Mat img_2d = cat2Dtiles(tiles_2d); + + // 3. Add the four subsets together + img_original(cv::Rect(offset, 0, num_cols - TILE_SIZE, num_rows)) += img_horizontal; + img_original(cv::Rect(0, offset, num_cols, num_rows - TILE_SIZE)) += img_vertical; + img_original(cv::Rect(offset, offset, num_cols - TILE_SIZE, num_rows - TILE_SIZE)) += img_2d; + + return img_original; +} + } // namespace hdrplus