Re: ImageResize does not linearize before resize?
- To: mathgroup at smc.vnet.net
- Subject: [mg117023] Re: ImageResize does not linearize before resize?
- From: Matthias Odisio <matthias at wolfram.com>
- Date: Tue, 8 Mar 2011 05:33:25 -0500 (EST)
Jari, On 3/6/11 4:45 AM, Jari Paljakka wrote: > Hi, > > While trialing Mathematica 8, I noticed ImageResize[] doesn't seem to linearize > the colorspace before resizing. > > Is this intentional? > > Reasoning why it would be a nice option to have: > http://www.4p8.com/eric.brasseur/gamma.html > > Of course, to "fix" this properly, image needs to have information in what > colorspace it is in in the first place, but to have > the option available directly in ImageResize would be nice, as I suspect it > could be made quite a bit faster. > > This non-linearization is probably assumed for all other image functions too? > > Example how to expose the issue (and how to fix, albeit slow and convoluted): > > (used "Dalai Lama" pic from the link above, which is constructed to exhibit the > issue) > (remove compilation target etc if you don't have a compiler - was testing how > to make corrections run "faster") > > Clear[srgb2linear, linear2srgb, linearresize, image]; > srgb2linear = Compile[{{Csrgb, _Real, 1}}, > With[{\[Alpha] = 0.055}, Table[Piecewise[ > {{C/12.92, > C <= 0.04045}, {((C + \[Alpha])/(1 + \[Alpha]))^2.4, > C > 0.04045}}], {C, Csrgb}]], RuntimeAttributes -> > {Listable}, Parallelization -> True, CompilationTarget -> > "C", RuntimeOptions -> "Speed"]; > linear2srgb = Compile[{{Clinear, _Real, 1}}, > With[{\[Alpha] = 0.055}, Table[Piecewise[ > {{12.92*C, > C <= 0.0031308}, {(1 + \[Alpha])*C^(1/2.4) - \[Alpha], > C > 0.0031308}}], {C, Clinear}]], > RuntimeAttributes -> {Listable}, Parallelization -> True, > CompilationTarget -> "C", RuntimeOptions -> "Speed"]; > linearresize[image_Image, scaling_] := > Module[{}, ((Image[#1, ColorSpace -> "RGB"] & )[ > linear2srgb[ImageData[#1, Interleaving -> True]]] & )[ > ImageResize[(Image[#1, ColorSpace -> "RGB"] & )[ > srgb2linear[ImageData[image, Interleaving -> True]]], > scaling]]] > image = Image["Dalai Lama", "Real"] > > In[432]:= ImageResize[ImageResize[image, Scaled[1/2]], Scaled[2]] > > Out:= Screwed up picture > > In[433]:= linearresize[linearresize[image, Scaled[1/2]], Scaled[2]] > > Out:= Correct image (of course sampled) This genuine issue will be properly addressed in a future release of Mathematica. In the meanwhile, one needs to revert to bits of code, as you did. In this particular case, your code produces on my machine a white image. That gamma correction seems to give a correct result: lin = ImageAdjust[image, {0, 0, 1.8}]; res = ImageResize[ImageResize[lin, Scaled[1/2]], Scaled[2]]; res = ImageAdjust[res, {0, 0, 1/1.8}]; (These values may be machine and OS dependent.) Matthias Odisio Wolfram Research