<p>As far as I remember, you need to input data in float array. Right now your input is unsigned char array. To quick check this, try loading any HDR image in float array &amp; pass it on to the setImage functions, this seems to work. </p>

<p>In your case, you would need to read individual image channel data &amp; cast it to float. If your image color is stored in [0-255] range, you would then need to bring it to [0-1] range (Not completely sure about this stage!) &amp; then pass the data in float array to denoiser functions.</p>

<p>Hope this helps. </p>

CC BY-SA 4.0
2019-12-15T17:10:37.500Z
this worked but I had to change Float4 to Float3 and had to use a jpg instead of png. Float4 format still gives me an error saying that the image format is unsupported.
Karthik
CC BY-SA 4.0
2019-12-16T05:47:13.490Z
I feel it's not supported as per official doc: 

https://openimagedenoise.github.io/documentation.html
TheOrestes
CC BY-SA 4.0
2019-12-15T06:15:32.847Z
2019-12-15T06:15:32.847Z
TheOrestes
<p>After some experimentation, I found a working code. Here we use a vector called float_data. This vector stores the colour data input as well as output. Here, pixel_data is a copy of the screen in RGBA 8-bits per channel.</p>
<pre><code>vector&lt;float&gt; float_data(3 * px * py);

for (size_t i = 0; i &lt; px; i++)
{
    for (size_t j = 0; j &lt; py; j++)
    {
        size_t float_index = 3 * (j * px + i);
        size_t index = 4 * (j * px + i);

        float_data[float_index + 0] = static_cast&lt;float&gt;(pixel_data[index + 0]) / 255.0f;
        float_data[float_index + 1] = static_cast&lt;float&gt;(pixel_data[index + 1]) / 255.0f;
        float_data[float_index + 2] = static_cast&lt;float&gt;(pixel_data[index + 2]) / 255.0f;
    }
}


oidn::DeviceRef dev = oidn::newDevice();
dev.commit();

oidn::BufferRef colorBuf = dev.newBuffer(px * py * 3 * sizeof(float));
colorBuf.write(0, px* py * 3 * sizeof(float), &amp;float_data[0]);

oidn::FilterRef filter = dev.newFilter(&quot;RT&quot;);

filter.setImage(&quot;color&quot;,  colorBuf, oidn::Format::Float3, px, py);
filter.setImage(&quot;output&quot;, colorBuf, oidn::Format::Float3, px, py);
filter.set(&quot;hdr&quot;, false);
filter.commit();
filter.execute();

// Check for errors
const char* errorMessage;
if (dev.getError(errorMessage) != oidn::Error::None)
    MessageBox(NULL, errorMessage, &quot;Error&quot;, MB_OK);

colorBuf.read(0, px * py * 3 * sizeof(float), &amp;float_data[0]);
</code></pre>
<p>The image can be then written to disk like:</p>
<pre><code>vector &lt;unsigned char&gt; uc_output_data(4 * px * py, 0);

for (size_t i = 0; i &lt; px; i++)
{
    for (size_t j = 0; j &lt; py; j++)
    {
        size_t uc_index = 4 * (j * px + i);
        size_t data_index = 3 * (j * px + i);

        uc_output_data[uc_index + 0] = static_cast&lt;unsigned char&gt;(fabsf(float_data[data_index + 0]) * 255.0f);
        uc_output_data[uc_index + 1] = static_cast&lt;unsigned char&gt;(fabsf(float_data[data_index + 1]) * 255.0f);
        uc_output_data[uc_index + 2] = static_cast&lt;unsigned char&gt;(fabsf(float_data[data_index + 2]) * 255.0f);
        uc_output_data[uc_index + 3] = 255;
    }
}

int result = stbi_write_png(filename, px, py, 4, &amp;uc_output_data[0], 0);
</code></pre>
<p>The result is an image -- denoised on the left, noisy on the right, for comparison purposes:</p>
<p><a href="https://i.sstatic.net/ffY8U.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ffY8U.png" alt="" /></a></p>

CC BY-SA 4.0
2024-02-10T18:49:19.557Z
2024-02-10T19:03:59.347Z
2024-02-10T19:03:59.347Z
shawn_halayka
<p>I'm trying to use open image denoise. Here's the code:</p>

<pre><code>int main() {
    int width, height, nrChannels;
    unsigned char* image = stbi_load("img_cornell_box.png", &amp;width, &amp;height, &amp;nrChannels, 0);
    unsigned char* resultImage = new unsigned char[width*height*nrChannels];
    oidn::DeviceRef device = oidn::newDevice();
    device.commit();
    std::cout &lt;&lt; nrChannels &lt;&lt; std::endl;
    oidn::FilterRef filter = device.newFilter("RT");
    filter.setImage("color", image, oidn::Format::Float4, width, height);
    filter.setImage("output", resultImage, oidn::Format::Float4, width, height);
    filter.set("hdr", false);
    filter.set("srgb", true);
    filter.commit();
    filter.execute();

    const char* errorMessage;
    if (device.getError(errorMessage) != oidn::Error::None) {
        std::cout &lt;&lt; "Error: " &lt;&lt; errorMessage &lt;&lt; std::endl;
    }
    else {
        stbi_write_png("img.png", width, height, nrChannels, resultImage, width * nrChannels);
    }
}
</code></pre>

<p>I'm getting an error saying that the image format is unsupported.</p>

<p>If I use jpg, and change Format::Float4 to Format::Float3 I get this error:</p>

<pre><code>Exception thrown at 0x00007FFC5588E1F5 (OpenImageDenoise.dll) in oidntest.exe: 0xC0000005: Access violation reading location 0x0000021D9C42FA70.
</code></pre>

<p>Am I doing something wrong?
This is the image I'm using: <a href="https://drive.google.com/open?id=1uSjsyWCRisXNFUJG6R8iL5AdVbM7_18u" rel="nofollow noreferrer">https://drive.google.com/open?id=1uSjsyWCRisXNFUJG6R8iL5AdVbM7_18u</a></p>

<image><denoise>
Trouble using Open Image Denoise
Karthik
# Trouble using Open Image Denoise

(Asked by: username_0 on 2019-12-11)

I'm trying to use open image denoise. Here's the code:

```
int main() {
    int width, height, nrChannels;
    unsigned char* image = stbi_load("img_cornell_box.png", &width, &height, &nrChannels, 0);
    unsigned char* resultImage = new unsigned char[width*height*nrChannels];
    oidn::DeviceRef device = oidn::newDevice();
    device.commit();
    std::cout << nrChannels << std::endl;
    oidn::FilterRef filter = device.newFilter("RT");
    filter.setImage("color", image, oidn::Format::Float4, width, height);
    filter.setImage("output", resultImage, oidn::Format::Float4, width, height);
    filter.set("hdr", false);
    filter.set("srgb", true);
    filter.commit();
    filter.execute();

    const char* errorMessage;
    if (device.getError(errorMessage) != oidn::Error::None) {
        std::cout << "Error: " << errorMessage << std::endl;
    }
    else {
        stbi_write_png("img.png", width, height, nrChannels, resultImage, width * nrChannels);
    }
}

```

I'm getting an error saying that the image format is unsupported.

If I use jpg, and change Format::Float4 to Format::Float3 I get this error:

```
Exception thrown at 0x00007FFC5588E1F5 (OpenImageDenoise.dll) in oidntest.exe: 0xC0000005: Access violation reading location 0x0000021D9C42FA70.

```

Am I doing something wrong?
This is the image I'm using: <https://drive.google.com/open?id=1uSjsyWCRisXNFUJG6R8iL5AdVbM7_18u>

**Tags:** image, denoise


---

## 2 answers

### Answer by username_1 on 2019-12-15. Score: 0 | Accepted answer

As far as I remember, you need to input data in float array. Right now your input is unsigned char array. To quick check this, try loading any HDR image in float array & pass it on to the setImage functions, this seems to work.

In your case, you would need to read individual image channel data & cast it to float. If your image color is stored in [0-255] range, you would then need to bring it to [0-1] range (Not completely sure about this stage!) & then pass the data in float array to denoiser functions.

Hope this helps.

> **Comments:**
> **username_0**: this worked but I had to change Float4 to Float3 and had to use a jpg instead of png. Float4 format still gives me an error saying that the image format is unsupported.  
> **username_1**: I feel it's not supported as per official doc:   https://openimagedenoise.github.io/documentation.html  

---

### Answer by username_2 on 2024-02-10. Score: 0

After some experimentation, I found a working code. Here we use a vector called float\_data. This vector stores the colour data input as well as output. Here, pixel\_data is a copy of the screen in RGBA 8-bits per channel.

```
vector<float> float_data(3 * px * py);

for (size_t i = 0; i < px; i++)
{
    for (size_t j = 0; j < py; j++)
    {
        size_t float_index = 3 * (j * px + i);
        size_t index = 4 * (j * px + i);

        float_data[float_index + 0] = static_cast<float>(pixel_data[index + 0]) / 255.0f;
        float_data[float_index + 1] = static_cast<float>(pixel_data[index + 1]) / 255.0f;
        float_data[float_index + 2] = static_cast<float>(pixel_data[index + 2]) / 255.0f;
    }
}

oidn::DeviceRef dev = oidn::newDevice();
dev.commit();

oidn::BufferRef colorBuf = dev.newBuffer(px * py * 3 * sizeof(float));
colorBuf.write(0, px* py * 3 * sizeof(float), &float_data[0]);

oidn::FilterRef filter = dev.newFilter("RT");

filter.setImage("color",  colorBuf, oidn::Format::Float3, px, py);
filter.setImage("output", colorBuf, oidn::Format::Float3, px, py);
filter.set("hdr", false);
filter.commit();
filter.execute();

// Check for errors
const char* errorMessage;
if (dev.getError(errorMessage) != oidn::Error::None)
    MessageBox(NULL, errorMessage, "Error", MB_OK);

colorBuf.read(0, px * py * 3 * sizeof(float), &float_data[0]);

```

The image can be then written to disk like:

```
vector <unsigned char> uc_output_data(4 * px * py, 0);

for (size_t i = 0; i < px; i++)
{
    for (size_t j = 0; j < py; j++)
    {
        size_t uc_index = 4 * (j * px + i);
        size_t data_index = 3 * (j * px + i);

        uc_output_data[uc_index + 0] = static_cast<unsigned char>(fabsf(float_data[data_index + 0]) * 255.0f);
        uc_output_data[uc_index + 1] = static_cast<unsigned char>(fabsf(float_data[data_index + 1]) * 255.0f);
        uc_output_data[uc_index + 2] = static_cast<unsigned char>(fabsf(float_data[data_index + 2]) * 255.0f);
        uc_output_data[uc_index + 3] = 255;
    }
}

int result = stbi_write_png(filename, px, py, 4, &uc_output_data[0], 0);

```

The result is an image -- denoised on the left, noisy on the right, for comparison purposes:

[![](https://i.sstatic.net/ffY8U.png)](https://i.sstatic.net/ffY8U.png)


---

