From 01881ca08959e21f5b62359d12a484b1605fdb8b Mon Sep 17 00:00:00 2001 From: Xiao Song Date: Tue, 12 Apr 2022 21:59:25 -0700 Subject: [PATCH] align : image pyramid --- include/hdrplus/align.h | 1 + include/hdrplus/utility.h | 2 - src/align.cpp | 77 ++++++++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/include/hdrplus/align.h b/include/hdrplus/align.h index 587baa1..18a4784 100644 --- a/include/hdrplus/align.h +++ b/include/hdrplus/align.h @@ -25,6 +25,7 @@ class align void process( const hdrplus::burst& burst_images, \ std::vector>>>& aligements ); + private: const std::vector inv_scale_factors = { 1, 2, 2, 4 }; }; diff --git a/include/hdrplus/utility.h b/include/hdrplus/utility.h index 41bef41..d3ee40f 100644 --- a/include/hdrplus/utility.h +++ b/include/hdrplus/utility.h @@ -119,6 +119,4 @@ cv::Mat downsample_nearest_neighbour( cv::Mat src_image ) return dst_image; } -cv::Mat gaussian_blur( cv::Mat src_image, double sigma ); - } // namespace hdrplus diff --git a/src/align.cpp b/src/align.cpp index 124edde..d8a9be2 100644 --- a/src/align.cpp +++ b/src/align.cpp @@ -1,4 +1,6 @@ #include +#include +#include // std::runtime_error #include // all opencv header #include "hdrplus/align.h" #include "hdrplus/burst.h" @@ -7,46 +9,63 @@ namespace hdrplus { +// static function only visible within file +static void build_image_pyramid( \ + std::vector& images_pyramid, \ + const cv::Mat& src_image, \ + const std::vector& inv_scale_factors ) +{ + images_pyramid.resize( inv_scale_factors.size() ); + + for ( int i = 0; i < inv_scale_factors.size(); ++i ) + { + cv::Mat blur_image; + cv::Mat downsample_image; + + switch ( inv_scale_factors[ i ] ) + { + case 1: + images_pyramid[ images_pyramid.size() - i - 1 ] = src_image; + // cv::Mat use reference count, will not create deep copy + downsample_image = src_image; + break; + case 2: + // Gaussian blur + cv::GaussianBlur( downsample_image, blur_image, cv::Size(0, 0), 1.0 ); + + // Downsample + downsample_image = downsample_nearest_neighbour( blur_image ); + + // Add + images_pyramid[ images_pyramid.size() - i - 1 ] = downsample_image; + + break; + case 4: + cv::GaussianBlur( downsample_image, blur_image, cv::Size(0, 0), 2.0 ); + downsample_image = downsample_nearest_neighbour( blur_image ); + images_pyramid[ images_pyramid.size() - i - 1 ] = downsample_image; + break; + default: + throw std::runtime_error("inv scale factor " + std::to_string( inv_scale_factors[ i ]) + "invalid" ); + } + } +} + void align::process( const hdrplus::burst& burst_images, \ std::vector>>>& aligements ) { - /* // Build image pyramid std::vector> grayscale_images_pyramid; grayscale_images_pyramid.resize( burst_images.grayscale_images_pad.size() ); for ( int img_idx = 0; img_idx < burst_images.grayscale_images_pad.size(); ++img_idx ) { - grayscale_images_pyramid[ img_idx ].resize( inv_scale_factors.size() ); - - // pyramid image are laied out from coarse to fine - // level 0 is the original grayscale image - grayscale_images_pyramid[ img_idx ][ inv_scale_factors.size() - 1 ] = burst_images.grayscale_images_pad[ img_idx]; - - // downsample and gaussian blur - for ( int i = 1; i < inv_scale_factors.size(); ++i ) - { - int inv_scale_factor_i = inv_scale_factors[ i ]; - cv::Mat blurred_image = \ - hdrplus::gaussian_blur( grayscale_images_pyramid[ img_idx ][ i - 1 ], inv_scale_factor_i * 0.5 ); - - // Get around with template - cv::Mat downsampled_blurred_image; - if ( inv_scale_factor_i == 2 ) - { - hdrplus::downsample_nearest_neighbour( blurred_image ); - } - else if ( inv_scale_factor_i == 4 ) - { - hdrplus::downsample_nearest_neighbour( blurred_image ); - } - - grayscale_images_pyramid[ img_idx ][ inv_scale_factors.size() - i - 1 ] = downsampled_blurred_image; - } + build_image_pyramid( grayscale_images_pyramid[ img_idx ], \ + burst_images.grayscale_images_pad[ img_idx ], \ + inv_scale_factors ); } - cv::GaussianBlur( src_image, dst_image, cv::Size(0, 0), sigma ); - */ + } } // namespace hdrplus