Pages: 1 ... 3 4 [5]   Go Down

### AuthorTopic: Most favourable up-ressing percentage  (Read 45561 times)

#### BartvanderWolf

• Sr. Member
• Offline
• Posts: 7182
##### Re: Most favourable up-ressing percentage
« Reply #80 on: July 15, 2014, 01:24:30 PM »

I mean this: http://www.imagemagick.org/discourse-server/viewtopic.php?t=22432#p93883
Note: If your idea works well, it will work well enough with Gaussians. What the binomial would do is ensure that your filter is C^0, which is pretty much a necessity when enlarging a lot.
Note 2: There is another direction I'm exploring. I am not sure I can do so in public, however. Need to check.

Thanks, I'll first have to read about that before trying to achieve it with IM.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #81 on: July 15, 2014, 01:37:05 PM »

Here is the attractive property that resizing through a gamma space does have, and that resizing through a sigmoidal space does not:
Suppose (for simplicity) that the three channels of an image are proportional, for example that
R=rL
G=gL
B=bL
where L is a function of position, but r and g and b are constant.
Suppose also that the resampling operator is linear.
Then, if we resample by first transforming R, G and B with a power law (that is, if we map the colors to a "gamma color space"), apply the linear resampling operator, and then apply the inverse of the power transformation (that is, if we undo gamma), the resulting image will also have proportional channels.
Consequently, if (locally) having (approximately) proportional channels describes an important property of images (in an "anchor" color space), resampling through a gamma space is a better proposition than through a sigmoidal color space.
Note: For this to work out as intended in practice, we must have that L, R, G and B are >= 0.
P.S. This property, about the interaction of gamma transformations with linear filtering, has to be well known.
« Last Edit: July 15, 2014, 03:04:31 PM by NicolasRobidoux »
Logged

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #82 on: July 15, 2014, 02:24:11 PM »

Maybe binomials are not such a great idea: Gaussians have the nice property of being rotationally invariant. Truncated far enough, no one would notice the result is not C^0. On the other hand you could use a binomial as an EWA kernel
« Last Edit: July 15, 2014, 03:44:18 PM by NicolasRobidoux »
Logged

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #83 on: July 16, 2014, 04:43:49 AM »

Bart:
If you feel like programming a mildly complex ImageMagick command and testing a prototype method, please let me know. I think ImageMagick Q16 is good enough.
I'm "whipping other cats" (gallicism)
(https://plus.google.com/110142949827261116931/posts/fCbeJMm5iQv) so this could speed up the exploration process.
« Last Edit: July 16, 2014, 04:46:10 AM by NicolasRobidoux »
Logged

#### BartvanderWolf

• Sr. Member
• Offline
• Posts: 7182
##### Re: Most favourable up-ressing percentage
« Reply #84 on: July 16, 2014, 06:48:42 AM »

Bart:
If you feel like programming a mildly complex ImageMagick command and testing a prototype method, please let me know. I think ImageMagick Q16 is good enough.

Hi Nicolas,

If I can figure out how to get it to work in Windows, and if time permits (don't know how complex a task you have in mind), I wouldn't mind giving a whirl. If you are looking for something very specific, we might start a new thread, so only the interested folks need to bother reading it.

Quote
I'm "whipping other cats" (gallicism)
(https://plus.google.com/110142949827261116931/posts/fCbeJMm5iQv) so this could speed up the exploration process.

Ah, characterizing a sensor (bias, response curve, PRNU, shot-noise, read-noise)? Fun but potentially time consuming. It does reveal a lot about the engine one uses to take images with.

If my modest contribution to doing something useful in ImageMagick Q16 can help you, I'd be glad to spend some time on it.

Cheers,
Bart

P.S. you can of course P.M. me if you want to discuss details.
« Last Edit: July 16, 2014, 07:09:03 AM by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #85 on: July 16, 2014, 07:53:33 AM »

Bart:

Thank you. (Start a new thread elsewhere if you want. You pick. Give it the title you want. This is "halo minimizing resizing by gamma blending")

Here it is:

Gist: Resample with the same filter both in linear light and gamma light, and blend the two results so that the darker a pixel is, the less the linear light result contributes.

Details:

Let's assume the image to be resized is sRGB for simplicity.

Choose a gamma. Suggestion: 3.

Choose a filter. Suggestion: EWA LanczosRadius. Robidoux or LanczosSharp or even LanczosSharpest 4 should work well too.

Choose a resampling operation. I'll use enlarging by 800%.

Step 1: Let's standardize to linear light:

IMG0 = result of convert input.png -set colorspace sRGB -colorspace RGB

Step 2: Resample in linear light:

IMG1 = result of convert IMG0 -filter LanczosRadius -distort Resize 800%

Step 3: Resample in gamma light:

IMG2 = result of convert IMG0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma .33333333333333333

Step 4: Compute a per pixel luminance value for IMG1. Any reasonable luminance (the Bayer one, for example, = 2*green + blue + green, or converting IMG1 to greyscale) should work. Normalize the luminance to [0,1] for the sake of exposition. (P.S. ImageMagick allows the use of actual images as alpha channel.) You may need to clamp IMG1 first (probably not needed if you don't use HDRI). P.S. Really nice normalization thanks to Bart Van Der Wolf: Use -constrast-stretch or -linear-stretch, possibly eliminating a few outliers in the process.

Call this normalized luminance (turned into an alpha, really) IMG3.

Step 5: Lerp (blend) the two results based on the luminance pseudo-image used as an alpha channel.

IMG4 = IMG3 * IMG1 + (1-IMG3) * IMG2

This, right there, is the key idea:

A pixel may be dark because it is part of an exaggerated dark halo. Since the version resized in linear light has the worst dark halos, use lots of the gamma version at this location. (IMG3 = 0 means black implies pure IMG2.)

Conversely, a light pixel is unlikely to be part of an offensive dark halo. If a pixel is light, use lots of the linear light result. (IMG3 = 1 means white implies pure IMG1.)

Note: The difference between IMG1 and IMG2 is only large where things are rapidly varying. Elsewhere, it does not matter which one we pick.

Step 6: Convert back to sRGB, basically getting the result of

convert IMG4 -colorspace sRGB output.png

-----

Pretty terse. Makes sense?
« Last Edit: July 17, 2014, 07:16:51 AM by NicolasRobidoux »
Logged

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #86 on: July 16, 2014, 08:11:15 AM »

I am particulary interested in whether it is useful for mild downsampling, and rotations.
Logged

#### BartvanderWolf

• Sr. Member
• Offline
• Posts: 7182
##### Re: Most favourable up-ressing percentage
« Reply #87 on: July 16, 2014, 07:47:06 PM »

Pretty terse. Makes sense?

Yes. I've got a first version working, but my skills with blending in IM are limited, so I probably need to simplify the code a lot (after reading more about the various options), and not use so many temporary images on disk for assembling the transparency. I also get artifacts in the dark regions, and a colorprofile shift.

So it will take more time to check for errors, and probably 16-bits/channel are not enough to avoid artifacts. Other than that, the results look pretty good (as expected) on different types of subject matter.

Cheers,
Bart

P.S. The artifacts in dark regions are fixed by forcing all input to 16-b/ch, so maybe 16-b/ch is afterall enough for a gamma 3 and a linear light conversion. Some more testing is required to make 100% sure there is no significant damage.

P.P.S The color profile shift is also solved. It had to do with that PNGs used to officially not be Color managed, but some programs do colormanage them, others don't. So it was not an IM issue.
« Last Edit: July 17, 2014, 05:33:17 AM by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #88 on: July 17, 2014, 04:15:43 AM »

Bart: I believe you can force an early conversion to Q16 in IM by inserting a command into the toolchain early on?

Also, at least in Unix dialects, you can create a "stack" of intermediate results you can select outputs from. (I'm guessing this works in Windows because it really is part of the ImageMagick command line language.) I've done this in the past, only I forget the exact syntax.

P.S. Has to do with image lists and the \( (the "\" is the Unix escape) and -clone operators.

http://www.imagemagick.org/Usage/basics/#list_ops
« Last Edit: July 17, 2014, 05:06:06 AM by NicolasRobidoux »
Logged

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #89 on: July 17, 2014, 04:35:46 AM »

Bart:

My apologies for not having the time to be more helpful right now (about my own experiment, no less).

Hints: Maybe the most efficient way to do this is to attach the luminance as an alpha channel to one of the two resized images, and then use something from the large tool box http://www.imagemagick.org/Usage/compose/.

Also, if you start a ImageMagick User Forum post on this project, I am guessing you'll get pertinent advice.
Logged

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #90 on: July 17, 2014, 04:50:36 AM »

Also: It is very likely that IM normalizes alpha channels to the "quantum" (0 to 255 in 8 bit, for example) instead of [0,1] so that they can be understood as regular greyscale images.
Logged

#### BartvanderWolf

• Sr. Member
• Offline
• Posts: 7182
##### Re: Most favourable up-ressing percentage
« Reply #91 on: July 17, 2014, 05:44:18 AM »

Also: It is very likely that IM normalizes alpha channels to the "quantum" (0 to 255 in 8 bit, for example) instead of [0,1] so that they can be understood as regular greyscale images.

Yes, I already had that covered, also by making it tweakable. That might help to avoid outliers from determining the maximum/minimum, but it's probably such a minor effect for resampling (all pixels will be new) that I'll relax that as well. Afterall, it's only a blending mask turned to an Alpha channel for a later 4-channel RGBA composite.

I'll send you a copy of my Windows batch file by PM as it currently is. It needs some more optimization (if possible), but that's now a smaller task).

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

#### BartvanderWolf

• Sr. Member
• Offline
• Posts: 7182
##### Re: Most favourable up-ressing percentage
« Reply #92 on: July 17, 2014, 05:55:09 AM »

Bart:

My apologies for not having the time to be more helpful right now (about my own experiment, no less).

No problem, the script contains some operations I wanted to learn better anyway, so this was a nice opportunity.

Quote
Hints: Maybe the most efficient way to do this is to attach the luminance as an alpha channel to one of the two resized images, and then use something from the large tool box http://www.imagemagick.org/Usage/compose/.

That's how I had already implemented it. Seems I was on the right track

Quote
Also, if you start a ImageMagick User Forum post on this project, I am guessing you'll get pertinent advice.

Yes, although I'd have to subscribe to yet another forum. I have enough distraction as it is, but maybe I'll join yet.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #93 on: July 17, 2014, 06:35:40 AM »

TTBOMK, in ImageMagick, you need to specify -set colorspace sRGB first thing if you want an sRGB png to be treated as sRGB.
This rather inconvenient choice was pushed by yours truly because, indeed, some people/programs shove non-sRGB content into PNGs, even though they are often sRGB. So, being agnostic seemed like a good idea. This probably was a mistake.
TTBOMK ImageMagick does not, by default, import based on colorspace information found in the metadata.
http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html
« Last Edit: July 17, 2014, 07:57:44 AM by NicolasRobidoux »
Logged

#### NicolasRobidoux

• Sr. Member
• Offline
• Posts: 280
##### Re: Most favourable up-ressing percentage
« Reply #94 on: July 17, 2014, 07:04:49 AM »

Bart:

Thank you. This was quick!

I really like your use of -contrast-stretch.

(No Windows machine so I'll translate back to unixese and see if I can use image lists to get rid of intermediate images, and also check whether there are 16-bit or other "computation" artifacts.)
« Last Edit: July 17, 2014, 07:25:38 AM by NicolasRobidoux »
Logged

#### BartvanderWolf

• Sr. Member
• Offline
• Posts: 7182
##### Re: Most favourable up-ressing percentage
« Reply #95 on: July 17, 2014, 12:00:32 PM »

Bart:

Thank you. This was quick!

I really like your use of -contrast-stretch.

(No Windows machine so I'll translate back to unixese and see if I can use image lists to get rid of intermediate images, and also check whether there are 16-bit or other "computation" artifacts.)

I've 'fancified' the Windows formatted batch file a bit for easier use in a Windows environment, and started a new thread about it here. With your permission it is available for all to use and enhance. Everyone is invited to Do share your thoughts and modifications, preferably in that thread.

Thanks for the inspiration and suggestions. Keep them coming...

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==
Pages: 1 ... 3 4 [5]   Go Up