Skip to content

Commit

Permalink
[image] downscaling dichotomy: image::downscaleImage for custom sampl…
Browse files Browse the repository at this point in the history
…er, imageAlgo::resizeImage for OIIO backend
  • Loading branch information
mugulmd committed Dec 14, 2022
1 parent d1e4a7b commit 991484d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 73 deletions.
84 changes: 15 additions & 69 deletions src/aliceVision/image/resampling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,13 @@ namespace oiio = OIIO;

namespace aliceVision {
namespace image {

/**
** Half sample an image (ie reduce its size by a factor 2) using bilinear interpolation
** @param src input image
** @param out output image
**/
template < typename Image >
void ImageHalfSample( const Image & src , Image & out )
{
const int new_width = src.Width() / 2 ;
const int new_height = src.Height() / 2 ;

out.resize( new_width , new_height ) ;

const Sampler2d<SamplerLinear> sampler;

for( int i = 0 ; i < new_height ; ++i )
{
for( int j = 0 ; j < new_width ; ++j )
{
// Use .5f offset to ensure mid pixel and correct bilinear sampling
out( i , j ) = sampler( src, 2.f * (i+.5f), 2.f * (j+.5f) );
}
}
}

* @brief Downscale an image using a given type of sampler.
* @param[in] src source image to downscale
* @param[out] out image to store the downscaled result
* @param[in] downscale downscale value
*/
template <typename SamplerType, typename Image>
void downscaleImage(const Image& src, Image& out, int downscale)
{
Expand All @@ -54,7 +35,7 @@ namespace image {

out.resize(new_width, new_height);

const Sampler2d<SamplerType> sampler; //TODO: rely on OIIO ? (as in downscaleImageInPlace)
const Sampler2d<SamplerType> sampler;
const float downscalef = downscale;
for(int i = 0; i < new_height; ++i)
{
Expand All @@ -66,52 +47,17 @@ namespace image {
}
}

template <typename ImageType>
void downscaleImageInplace(ImageType& inout, int downscale)
/**
** Half sample an image (ie reduce its size by a factor 2) using bilinear interpolation
** @param src input image
** @param out output image
**/
template < typename Image >
void ImageHalfSample( const Image & src , Image & out )
{
if(downscale <= 1)
return;
ALICEVISION_LOG_TRACE("downscaleImageInplace in: " << inout.Width() << "x" << inout.Height());
/*
{
// Gaussian filter + Nearest Neighbor: could be expensive for very large resolutions with large downscale factor.
Image otherImg;
const double sigma = downscale * 0.5;
ImageGaussianFilter(inout, sigma, otherImg); // TODO: error on RGBColor
downscaleImage<SamplerNearest>(otherImg, inout, downscale);
ALICEVISION_LOG_INFO("downscaleImageInplace otherImg: " << otherImg.Width() << "x" << otherImg.Height());
}
{
// Simple bilinear interpolation: only valid for <= 2x downscale
ImageType otherImg;
downscaleImage<SamplerLinear>(inout, otherImg, downscale);
std::swap(inout, otherImg);
}*/
{
// Rely on OpenImageIO to do the downscaling
const unsigned int w = inout.Width();
const unsigned int h = inout.Height();
const unsigned int nchannels = inout.Channels();

const unsigned int nw = (unsigned int)(floor(float(w) / downscale));
const unsigned int nh = (unsigned int)(floor(float(h) / downscale));

ImageType rescaled(nw, nh);

const oiio::ImageSpec imageSpecOrigin(w, h, nchannels, ColorTypeInfo<typename ImageType::Tpixel>::typeDesc);
const oiio::ImageSpec imageSpecResized(nw, nh, nchannels, ColorTypeInfo<typename ImageType::Tpixel>::typeDesc);

const oiio::ImageBuf inBuf(imageSpecOrigin, inout.data());
oiio::ImageBuf outBuf(imageSpecResized, rescaled.data());

oiio::ImageBufAlgo::resize(outBuf, inBuf);

inout.swap(rescaled);
}
ALICEVISION_LOG_TRACE("downscaleImageInplace out: " << inout.Width() << "x" << inout.Height());
downscaleImage<SamplerLinear, Image>(src, out, 2);
}


/**
** @brief Ressample an image using given sampling positions
** @param src Input image
Expand Down
20 changes: 16 additions & 4 deletions src/software/pipeline/main_panoramaSeams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ bool computeWTALabels(image::Image<IndexT> & labels, const std::vector<std::shar
ALICEVISION_LOG_TRACE("Load mask with path " << maskPath);
image::Image<unsigned char> mask;
image::readImageDirect(maskPath, mask);
image::downscaleImageInplace(mask, downscale);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, mask, mask);
}

// Get offset
oiio::ParamValueList metadata = image::readImageMetadata(maskPath);
Expand All @@ -69,7 +72,10 @@ bool computeWTALabels(image::Image<IndexT> & labels, const std::vector<std::shar
ALICEVISION_LOG_TRACE("Load weights with path " << weightsPath);
image::Image<float> weights;
image::readImage(weightsPath, weights, image::EImageColorSpace::NO_CONVERSION);
image::downscaleImageInplace(weights, downscale);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, weights, weights);
}

if (!seams.appendWithLoop(mask, weights, viewId, offsetX, offsetY))
{
Expand Down Expand Up @@ -107,14 +113,20 @@ bool computeGCLabels(image::Image<IndexT>& labels, const std::vector<std::shared
ALICEVISION_LOG_TRACE("Load mask with path " << maskPath);
image::Image<unsigned char> mask;
image::readImageDirect(maskPath, mask);
image::downscaleImageInplace(mask, downscale);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, mask, mask);
}

// Load Color
const std::string colorsPath = (fs::path(inputPath) / (std::to_string(viewId) + ".exr")).string();
ALICEVISION_LOG_TRACE("Load colors with path " << colorsPath);
image::Image<image::RGBfColor> colors;
image::readImage(colorsPath, colors, image::EImageColorSpace::NO_CONVERSION);
image::downscaleImageInplace(colors, downscale);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, colors, colors);
}

// Get offset
oiio::ParamValueList metadata = image::readImageMetadata(maskPath);
Expand Down

0 comments on commit 991484d

Please sign in to comment.