Imagecodecs and jpeg2000

Dear all,

I am trying to benchmark Jpeg2k for image compression. Normally it is supposed to work with a compression level (thresholding of the coefficients in the wavelet space). I tried the following:

  • ImageJ with OME tiff (Jpeg2k lossy image is garbage and you can’t set the level)
  • tifffile library calling imagecodecs. In this case the finale call is j2k_encode in imagecodecs_jpeg2k.pyx but the ‘level’ of compression is not working (i.e. whatever the level I set the output is the same) but the output is ok (compression ratio of around 3).

I have 2 questions:

  • Is there a way to read the _jpeg2k.pyx to understand what’s going on under the hood?
  • Is there another codec library that have a documentation?

There are many papers comparing jpeg2k compression with their own approach but I feel like no one is actually optimizing the behavior of the wavelet coefficient thresholding, especially with respect to the noise statistics.

Thank you for your help/tips.

The source code for the jpeg2k_encode function is at https://github.com/cgohlke/imagecodecs/blob/f38d0d4e5db1d08d92742a5d899325fc7c7b56e7/imagecodecs/_jpeg2k.pyx#L92

The level argument can be used to set OpenJPEG’s tcp_rates parameter. See https://github.com/cgohlke/imagecodecs/blob/f38d0d4e5db1d08d92742a5d899325fc7c7b56e7/imagecodecs/_jpeg2k.pyx#L119

See also the warning in the docstring. If you know a better way to set the compression level in OpenJPEG, let me know.

I remember now: the level control is essentially disabled in imagecoedcs.jpeg2k_encode. At the time the code was written, using multiple layers and tcp_rates would cause segfaults. Time to revisit…

Thanks Christoph! It’s weird that the source was not readable locally, is that normal behavior with .pyx?

I have never worked with OpenJPEG directly but I’ll have a look.

The .pyx files are just text files containing Cython code. They should be readable in any code editor.They are not included in the binary distribution, just the source distribution.

I changed the jpeg2000 encoder to use the peak signal-to-noise ratio (PSNR) instead of the rate (setting tcp_distoratio instead of tcp_rates). This works well with single-layer images. Is that what you had in mind? There are other parameters that control compression level such as subsampling and “reversibility”.

Something like PSNR or some metric linked to the noise (in order to remain within the noise limit).

By single layer image you mean keeping only the fully sampled one?

Regarding other parameters I think for microscopy application subsampling should not be used (or only in pyramidal fashion for display) and reversibility must be ensured.

I was referring to JPEG 2000 quality layers. They allow to have progressive increasing image quality, e.g. when transferring over network. Not sure it makes sense to enable multiple quality layers for scientific applications. Multiple resolutions (pyramids) and tiling are other nice features of JPEG 2000 that do not make much sense when used within a TIFF container.