RenderKit / oidn

Intel® Open Image Denoise library
https://www.openimagedenoise.org/
Apache License 2.0
1.74k stars 160 forks source link

Is that result expected? #157

Open ghost opened 1 year ago

ghost commented 1 year ago

Hi, Trying to test the denoiser on an input.

Before showing the code, I get much much darker result than expected. So I prepared a really small 4x4 example of how I use the lib, maybe there is something I am doing wrong.

Please have a look at the following code:

// =========================
// Image setup
// =========================
Magick::Image img;
Magick::Geometry geom;
const uint32_t size_ = 4;

geom.height( size_ );
geom.width( size_ );

img.size( geom );
img.magick( "png" );

// =========================
// Magick specific setup
// =========================

auto *k = img.getPixels( 0, 0, size_, size_ );
auto sp_magick = std::span <Magick::PixelPacket> ( k, size_ * size_ );

// =========================
// OIDN specific setup
// =========================
oidn::DeviceRef device = oidn::newDevice();

device.commit();
u32 final_out_size = size_ * size_ * 3 * sizeof( float );
auto buffer = device.newBuffer( final_out_size );
auto sp_oidn = std::span <std::array <float, 3> > ( std::bit_cast <std::array <float, 3> *> ( buffer.getData() ), size_ * size_ );

// =========================
// One gray pixel at each corner
// =========================
auto l = [&sp_oidn]( u32 idx, float r, float g, float b ){

        sp_oidn[idx][0] = r;
        sp_oidn[idx][1] = g;
        sp_oidn[idx][2] = b;

};

l( 0, 0.5, 0.5, 0.5 );
l( 3, 0.5, 0.5, 0.5 );
l( 12, 0.5, 0.5, 0.5 );
l( 15, 0.5, 0.5, 0.5 );

// =========================
// Filters
// =========================
oidn::FilterRef filter = device.newFilter( "RT" );

filter.setImage( "color", buffer, oidn::Format::Float3, size_, size_ );
filter.setImage( "output", buffer, oidn::Format::Float3, size_, size_ );
filter.set( "hdr", false );
filter.set( "srgb", false );
filter.commit();
filter.execute();

// =========================
// Set values to image magick : 16 bits per channel -> [0;65536[
// =========================

u32 sp_offset = 0;

for ( u32 row = 0; row < size_; row += 1 ){

        for ( u32 col = 0; col < size_; col += 1 ){

                sp_magick[sp_offset].red = sp_oidn[sp_offset][0] * std::numeric_limits <u16>::max();
                sp_magick[sp_offset].green = sp_oidn[sp_offset][1] * std::numeric_limits <u16>::max();
                sp_magick[sp_offset].blue = sp_oidn[sp_offset][2] * std::numeric_limits <u16>::max();
                sp_magick[sp_offset].opacity = std::numeric_limits <u16>::max();

                sp_offset += 1;

        }
}

img.syncPixels();
img.write( "my_file.png" );

Like I said, this code leads to a way too dark image. The colors are way too influenced by the dark colors.

I would like to be able to tell the algorithm that the pixels are not set and should not influence the calculation in any way. Right now, black/unset pixels will have a way too strong influence on the result.

The gray pixels are like the ray information, and they should be kept intact or almost intact. Not blended that much with the unsets.

How may I do?

thanks,