Does Image Magick have Tonemapping operators?

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
ssilk
Posts: 15
Joined: 2011-01-19T21:10:17-07:00
Authentication code: 8675308

Does Image Magick have Tonemapping operators?

Post by ssilk »

Hi,

I thought I had read a while back that Image Magick included some tonemapping operators for compressing HDR images into 8-bit range. Am I wrong about this? I can't find any mention of it now, except that the HDRI Info page (http://www.imagemagick.org/script/high- ... -range.php) says you can apply tonemapping. It's unclear if that's supposed to mean there are tonemapping functions included, or you need to do your own.

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

I am not an expert on HDR images and processing of them. But to my knowledge IM in HDRI mode or any other mode does not have any specific function to do tonemapping on HDR images. I do have a tonemapping script, but do not know if that is what you need (see link below), but it was not designed for HDR images in particular. Perhaps you can explain the process you call tonemapping or give a reference. It is possible that IM can do it, but one would have to set up command lines or a script to do such. I will certainly defer to people more expert with HDR image processing experience.
ssilk
Posts: 15
Joined: 2011-01-19T21:10:17-07:00
Authentication code: 8675308

Re: Does Image Magick have Tonemapping operators?

Post by ssilk »

Hi,

Thanks for the response. Tonemapping, at least in the context of HDR images, refers to a class of algorithms designed to compress the large dynamic range of HDR images (often 6 orders of magnitude or greater) into a range that can be displayed on monitors, which are typically limited to around 500:1 at best for a static image. The simplest Tonemappers are global algorithms like linear compression, logarithmic compression. Unfortunately, these usually look terrible, so many much more complex tonemappers have been proposed.

I was hoping Image Magick might have some of these implemented. FreeImage and PFSTMO both have several tonemappers implemented.

The wiki article gives a good intro to Tonemapping: http://en.wikipedia.org/wiki/Tone_mapping

Thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

Imagemagick has some basic operators, such as -gamma, -evaluate pow, -evaluate log, -sigmoidal-contrast, and I have a dynamic range filter called kneemap (as well as duotonemap, redist and retinex) in my scripts. There is also the -fx operator which allows you to provide your own equation to process the image (though slow). But I am not sure that anything more sophisticated has been implemented directly into IM, though others may have scripted something. One would have to study the code or algorithms from the references you provided to know more. Perhaps Anthony can shed more light on this. You might want to look over my scripts as well. Some may be somewhat relevant? Also perhaps there are other IM users who have had more experience with HDR images and can shed more light on how they process them.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Does Image Magick have Tonemapping operators?

Post by anthony »

IM does not have Tone-Mapping directly implemented, but it can process images using the equivalent functions.

Basically we have not had a someone with HDR interest to detail precisely what such an operation should do, or a programmer to actually implement it. At least not yet. But IM is open source, and we are more than happy to help people in any way we can.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

Can you provide a link to a small example HDRI image that needs tone mapping? I could experiment with IM functions and possibly script something. Does one need to be in HDRI IM mode to process it correctly?
ssilk
Posts: 15
Joined: 2011-01-19T21:10:17-07:00
Authentication code: 8675308

Re: Does Image Magick have Tonemapping operators?

Post by ssilk »

Hi,

I've posted two versions of the same image below. I never got Radiance (.hdr) I/O working properly with IM, so I use EXR myself. If you're working in Linux, Radiance should be easy to setup. I've also included two tonemapped versions to give an idea of what they should look like. The simplest tonemapping besides linear (which looks terrible) is logarithmic, which literally just means taking the logarithm of pixel values then shifting and scaling the resulting image into and 8-bit range.

If you're just interested in working on this for your own enlightenment, please do because it's super interesting and fun, but please don't spend time on it on my behalf because I've already found some other tone mapping options that are working nicely.

If you're working in Linux and are interested in seeing tonemapping in action quickly and easily, you can get the package PFStmo. It's open source and available through the standard package manager (at least in Ubuntu), and offers 8 different well-known tonemapping algorithms. FreeImage also includes a few tonemappers.

Finally, if you're interested in reading about tonemapping, I've included a couple of references below (from textbooks) which give a good start.

Enjoy!

- Simon

HDR Images:
Radiance format: http://www.site.uottawa.ca/~ssilk083/IM ... 40x480.hdr
OpenEXR format: http://www.site.uottawa.ca/~ssilk083/IM ... 40x480.exr

Tonemapped versions:
Surreal (Mantiuk06): http://www.site.uottawa.ca/~ssilk083/IM ... zation.jpg
Photorealistic (Reinhard05): http://www.site.uottawa.ca/~ssilk083/IM ... _local.jpg


References (reviews of tonemapping algorithms):

Chapter 17 of "Color Imaging Fundamentals and Applications" by Reinhard et al, (2008)

Chapters 6-9 of "High Dynamic Range Imaging: Acquisition, Display, and Image-Based Lighting" by Reinhard et al. (2005)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

I am on a Mac and can open both .hdr and .exr though the former is more easily displayed. I will look into some trials with my current scripts. But please let me know if I need to be in IM Q16 HDRI mode so I can switch.

I don't have access to those references. Do you have any online references. I have found a few, but have not had time to study them.
ssilk
Posts: 15
Joined: 2011-01-19T21:10:17-07:00
Authentication code: 8675308

Re: Does Image Magick have Tonemapping operators?

Post by ssilk »

Hi,

Regarding the IM mode, I don't know sorry. I remember spending a lot of time enabling HDR modes and installing (or trying to install) dependencies. If you can tell me how to quickly check what mode IM is in on my Windows machine, I'll check. When I actually read/write HDR images in C++ using Magick++, I don't do anything special, I just include the Magick++ header file and then I can read/write them fine and if I use IM to read an HDR image then save a copy in a different format (i.e. convert .hdr -> .exr), this seems to work fine.

Regarding references, the following list is from the old PFStmo website (http://www.mpi-inf.mpg.de/resources/tmo/). This lists operators that were included in PFS at the time. Some of these operators are no longer included in PFS, but this list gives a good run down of the state of the art circa 2007. In particular, "Dynamic Range Reduction Inspired by Photoreceptor Physiology" gives a good review of techniques in the first couple of pages, and has several comparisons of 12 different operators on multiple test images which will give you a good appreciation for different styles of tonemapping and how varied the approaches are. PDFs of all of these papers are easily through Google scholar currently.


Adaptive Logarithmic Mapping for Displaying High Contrast Scenes (*)
F. Drago, K. Myszkowski, T. Annen, and N. Chiba
In Eurographics 2003

Time-Dependent Visual Adaptation for Realistic Image Display
S.N. Pattanaik, J. Tumblin, H. Yee, and D.P. Greenberg
In Proceedings of ACM SIGGRAPH 2000

Dynamic Range Reduction Inspired by Photoreceptor Physiology
E. Reinhard and K. Devlin
In IEEE Transactions on Visualization and Computer Graphics, 2004

Photographic Tone Reproduction for Digital Images (*)
E. Reinhard, M. Stark, P. Shirley, and J. Ferwerda
In ACM Transactions on Graphics, 2002

Fast Bilateral Filtering for the Display of High-Dynamic-Range Images
F. Durand and J. Dorsey
In ACM Transactions on Graphics, 2002

A Tone Mapping Algorithm for High Contrast Images
M. Ashikhmin
In the 13th Eurographics Workshop on Rendering, 2002

Gradient Domain High Dynamic Range Compression
R. Fattal, D. Lischinski, and M. Werman
In ACM Transactions on Graphics, 2002

A Perceptual Framework for Contrast Processing of High Dynamic Range Images
R. Mantiuk, K. Myszkowski, and H.-P. Seidel
In ACM Transactions on Applied Perception, 2006


Tonemapping is still a big research area with new algorithms being published regularly. One promising paper I read recently is the following, and the authors have posted MATLAB code if you want to test it out.

Local Laplacian Filters: Edge-aware Image Processing with a Laplacian Pyramid
S. Paris, S. Hasinoff, and J. Kautz
SIGGRAPH 2011
http://people.csail.mit.edu/sparis/publi/2011/siggraph/

Hope this helps.

- Simon
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

what does

convert -version

tell you. It should identify hdri if enabled as well as your Q level.

I have found some of these papers online already, though have not had time to study them.

I will upload some of my tests later to show you what IM can do and what some of my scripts can do. Some are comparable or perhaps better (depending upon what you want out of them) compared to your two processed examples.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

This is the first time I have worked with a .hdr file. My tests have been in IM 6.6.9.9 Q16 (non-hdri). My first tries did not work well as the colors got very distorted. However, I noticed from the verbose info that file is in XYZ colorspace and figured that was causing the problems. So I tried to convert it to RGB, but still got distorted colors. So I figured that since the image looked fine when displayed, I would just reset the colorspace (not convert it) to sRGB. Then my tests went better. As I was not in HDRI mode, I just used png for the output.

convert IMG_3133And8more_640x480.hdr -set colorspace sRGB IMG_3133And8more_640x480_setsrgb.png
Image

As you can see the above brightened the image a bit, whereas if I had used -set colorspace RGB it would have looked just like your .hdr image. (Perhaps I should have used the RGB version rather than the sRGB version, but I did not notice the brightening until later. Perhaps later I will go back and redo another set using the result from -set colorspace RGB ).

Then I continued with that output for the following tests.

IM functions

Power Law function:
convert IMG_3133And8more_640x480_setsrgb.png -evaluate pow 0.4 IMG_3133And8more_640x480_setsrgb_pow_0p4.jpg
Image

Logarithmic function:
convert IMG_3133And8more_640x480_setsrgb.png -evaluate log 100 IMG_3133And8more_640x480_setsrgb_log_100.jpg
Image

Sigmoidal Contrast function:
convert IMG_3133And8more_640x480_setsrgb.png -sigmoidal-contrast 20,5% IMG_3133And8more_640x480_setsrgb_sigmoidal_20_5.jpg
Image

My Scripts

Soft Knee Dynamic Range Compressor:
kneemap -a 0.9 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_knee_0p9.jpg
Image

Retinex:
retinex -m hsl IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_retinex_hsl_f0.png
Image

Duotonemap:
duotonemap -l 0.4 -u 0.6 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_duotonemap_l0p4_u0p6.jpg
Image

Redist (Uniform Histogram Distribution):
redist -m rgb -s uniform IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_rgb_uniform.jpg
Image

redist -m hsl -s uniform IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_rgb_uniform.jpg
Image

redist -m YCbCr -s uniform IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_rgb_uniform.jpg
Image


Redist (Gaussian Histogram Distribution):
redist -m rgb -s gaussian 60,150,60 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_rgb_60_150_60.jpg
Image

redist -m hsl -s gaussian 60,150,60 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_hsl_60_150_60.jpg
Image

redist -m hsb -s gaussian 60,150,60 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_hsb_60_150_60.jpg
Image

redist -m YCbCr -s gaussian 60,150,60 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_redist_ycbcr_60_150_60.jpg
Image


Space (Spatially Adaptive Contrast Enhancement):

space -m 0.4 -b 1.5 -c 3 -g 3 -s 2 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_space_m0p4_b1p5_c3_g3_s2.jpg
Image


Duotonemap2:
duotonemap2 -m both -s 0,18 -h 18,100 -r 5 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_duotonemap2_both_s0x18_h18x100_r5.jpg
Image

duotonemap2 -m both -s 0,18 -h 40,100 -r 5 IMG_3133And8more_640x480_setsrgb.png IMG_3133And8more_640x480_setsrgb_duotonemap2_both_s0x18_h40x100_r5.jpg
Image
Last edited by fmw42 on 2011-05-18T11:14:09-07:00, edited 4 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Does Image Magick have Tonemapping operators?

Post by anthony »

Interesting discussion.

However I don't think any of those methods are specifically designed with HDR Images in mind.
Consequently the sky is becoming too bright, or the foreground objects too dark during any simple mathematical processing.

For this work HDRI version of IM is probably important to preserve quality, particularly of dark colors. However because the default IM is Q16 (16 bit intergers per value), it generally gets a very quality result (but not perfect) even without going to HDRI (using double-precision floats for memory storage).

ActuallyI got a fairly good result with just basic and simple histogram equalization (HDRI and non-HDRI versions of IM)

Code: Select all

  convert IMG_3133And8more_640x480.hdr -equalize \
          -set colorspace sRGB -colorspace RGB  IMG_3133And8more_640x480_equalize.jpg
The last colorspace handling is to convert RGB to sRGB (yes it is weird due to the weirdness of IMv6 implementation of sRGB. Colorspace handling will be completely different in IMv7!)

However I don't remember if the -equalize operation color distorts or not (does it equalize each channel separately?)
This should be a similar result to Fred's script redist - m rgb -s uniform (but not using RB space)

The big problem seems to be trying to improve the contrast of the square boxes of the middle ground.

ssilk: This is a great image for doing HDR testing. Do you mind if when this discussion is over I make use of it in ImageMagick Examples?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Image Magick have Tonemapping operators?

Post by fmw42 »

IM -equalize may distort the colors due to the fact that it equalized each channel separately. My redist has the uniform distribution as well, but equalizes the all channels in concert in RGB mode or by just equalizing the Lightness or Luma channels depending upon which colorspace is used. IM -equalize needs to be channel sensitive so that one can equalize each channel separately or in concert as most of the other IM functions such as -level and -contrast-stretch, etc, do.
ssilk
Posts: 15
Joined: 2011-01-19T21:10:17-07:00
Authentication code: 8675308

Re: Does Image Magick have Tonemapping operators?

Post by ssilk »

@fmw42:
These results look good pretty good for how simple the operators are. I've posted results below from some other tonemappers that I've tested recently. The references for all of these tonemappers are provided in my earlier post in case they interest you, except Larson97 one which is from [1]. The first three were done in MATLAB, the next six rest were done in PFStmo, and the last one with code from the authors' site.

The first six are all global algorithms, while the last four are local operators.

@anthony:
Feel free to use this image for examples, that would be cool. Could you maybe PM me if/when you do to let me know, I'd like to link to them from my site. Also, if you'd like a high res version, the original is 7MP. I can email it to you or provide a link to it. Thanks.


Linear compression to 8-bits with pixels above the 61st percentile clamped.
Image

Base ten logarithm of pixel values shifted and scaled into 8-bit range.
Image

Larson97 (MATLAB) - Histogram Adjustment
Image

Pattanaik00 - Time-dependant adpatation.
Image

Reinhard05 - Photoreceptor Physiology
Image

Drago03 - Adaptive Logarithmic base.
Image

Fattal02 - Gradient Domain compression
Image

Reinhard02 - Photographic Tone reproduction (local version)
Image

Mantiuk06 - Local Contrast Mapping
Image

Paris11 - Local Laplacian Filters
Image



G. Larson, H. Rushmeier, and C. Piatko, "A visibility matching tone reproduction operator for high dynamic range scenes," Visualization and Computer Graphics, IEEE Transactions on, vol. 3, no. 4, pp. 291-306, 1997.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Does Image Magick have Tonemapping operators?

Post by anthony »

The Larson97 seems to work well, but looses the sun-highlights on the clouds.

The last one Paris11 -- Local Laplacian filters seems to produce about the best result though it is a little two equal in the results between sky and foreground. That is Forgound is good, but clouds seems a bit too dark.

Perhaps a blending of Paris11 and another function would work best. Hmm we also have to watch the colorspace output in this sort of work. Make sure we save image with a sRGB colormap.

Basically it all looks like good fun, and adding general tone-mapping functions would make a great addition to IM.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply