ejmahler / rust_dct

Rust library to compute the main four discrete cosine transforms
Apache License 2.0
38 stars 6 forks source link

2D dct and inverse dct for normals integration #8

Closed mpizenberg closed 2 years ago

mpizenberg commented 3 years ago

Hi, I'm currently working on a photometric stereo project and in need of a 2D cosine transform to reconstruct the surface volume with normal integration. Basically I'm trying to port this matlab function: https://github.com/yqueau/normal_integration/blob/master/Toolbox/DCT_Poisson.m. I haven't done signal processing things like fft and dct in a while so I don't remember much what the different dct types of this crate refer to.

I was wondering if you could point me in the correct direction on how to use this crate to write the dct2 and idct2 matlab functions used here and there and if there might be normalization pitfalls I could avoid.

Btw, awesome performance work of rustfft!

ejmahler commented 3 years ago

IDCT2 is a DCT3. So, compute a DCT2, do your processing, then compute a DCT3 on the processed data.

At some point within those steps (doesn't matter when), you'll need to scale each element by 1/N (or 1/(2N)?).

ejmahler commented 3 years ago

Ah, I see that matlab's dct2 is actually a 2D transform, not just 1D. So the steps are slightly more complicated.

To replicate the effects of dct2, on 2D array of size MxN:

  1. Plan DCT2s of size M and N
  2. Loop over data.chunks_exact_mut(M) and compute a DCT of size M on each chunk
  3. Transpose your data from a MxN array to a NxM array (See the transpose crate for a fast implementation of this)
  4. Loop over data.chunks_exact_mut(N) and compute a DCT of size N on each chunk
  5. Transpose your data back to MxN

And idct is the exact same thing, but with DCT3 instead of DCT2.

I looked up the normalization, and it's 2/N * 2/M, IE 4 / N*M. So you'll have to scale each element by that much, at some point in the process.

mpizenberg commented 3 years ago

Thank you! I'll try that and let you know how it goes.

mpizenberg commented 3 years ago

Hi, I'm just getting back to this after a long pause. I'll let you know soon how that goes but my first experiments with the fft library are working well so I'm getting back at this normals integration via dct now.

Side question, I've noticed that rustfft is now at version 6. And it seems from my quick check that everything is compatible with rust_dct. I'm getting all the code and tests / benches running normally. Do you mind if I make a PR to update the dependency? Or do you prefer do it yourself / or just leave it as is and not bother?

mpizenberg commented 2 years ago

It worked like a charm! thanks again for the help. Here are some images extracted for the examples of the fft2d crate I just published, reusing your rustfft and rustdct crates.

Low pass filtering with 2D Fourier transform:

fft2d_low_pass

Normal integration with a Poisson solver via a 2D DCT transform:

fft2d_normal_integration

ejmahler commented 2 years ago

If you make a PR to upgrade rustdct with v6 of rustfft, i'd be happy to accept it.

On Thu, Dec 9, 2021 at 2:23 PM Matthieu Pizenberg @.***> wrote:

It worked like a charm! thanks again for the help. Here are some images extracted for the examples of the fft2d https://github.com/mpizenberg/fft2d crate I just published, reusing your rustfft and rustdct crates.

Low pass filtering with 2D Fourier transform:

[image: fft2d_low_pass] https://user-images.githubusercontent.com/2905865/145476357-d15a5785-3204-47e3-8293-4bd5fc560738.jpg

Normal integration with a Poisson solver via a 2D DCT transform:

[image: fft2d_normal_integration] https://user-images.githubusercontent.com/2905865/145479695-1a915993-3435-4cbb-a97e-e5b0fcd3ce18.jpg

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ejmahler/rust_dct/issues/8#issuecomment-990347513, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAI2M6T7T5JQYECJ5ZLJSI3UQET6ZANCNFSM4WHM23XA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.