[SOLVED] Add Cosine windowing? (for Cosine-windowed Sinc)

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

[SOLVED] Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

Looking at frequency response plots, I starting wondering whether Cosine windowing would be worthwhile.
And indeed one of the best articles in the field recommends Cosine windowing over Sinc:
Erik H. W. Meijering, Wiro J. Niessen, Josien P. W. Pluim, Max A. Viergever: Quantitative Comparison of Sinc-Approximating Kernels for Medical Image Interpolation. MICCAI 1999, pp. 210-217.
Another example: Cosine windowing is one of the options of http://www.slicer.org/slicerWiki/index. ... alarVolume, which I would assume was competently put together.
So:

Could Cosine windowing be added to ImageMagick? (I'm actually making this request so I can easily test and compare. Self-interested, as usual.)

-----

Also, I've spent some time looking for motivation for using one window function over another in image resampling. It appears that all the windows were designed for filtering unidimensional signals with a very large window extent (2x64, say). The recommendations as to which window functions to try therefore generally come from this application. Unfortunately, inferring good preformance with a window extent of 2x2, 2x3 or 2x4 (twice the number of lobes), in a 2D context, is definitely a stretch.
That Sinc windowing got early recommendations by Turkowski & Gabriel and Blinn is definitely a score for Lanczos. But I think the topic stands to be revisited a bit more carefully.
In any case, there does not appear to be unanimity that Lanczos is best: http://www.cg.tuwien.ac.at/research/vis ... indows.pdf
Last edited by NicolasRobidoux on 2012-05-08T05:01:52-07:00, edited 4 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

If you'd like data that this is worthwhile, I would do my usual "hardwire in the source code" hack by replacing the guts of another window function to do preliminary testing. On the other hand, I'd be immensely surprised if Cosine is not a better windowing function than some of the ones already in IM.

-----

Another, kind of related, suggestion: The real name of the Hanning window function is Hann. (Hanning is a common variant of the name which appears to have bled over from the popularity of the closely related Hamming window function.)

Maybe IM7 would be an opportunity to set the name straight?
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

Note that I expect Lanczos to win overall.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by fmw42 »

Cosine windowing is a standard technique that is used in FFT processing. I used it some of my scripts (esp cepstrum). I also have an example command line of its use at http://www.fmwconcepts.com/imagemagick/ ... composite1
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

fmw42 wrote:...I used it some of my scripts (esp cepstrum)...
Don't you use the extended cosine bell window, which is a variant of Hann? http://www.cg.tuwien.ac.at/~theussl/DA/node34.html
I'm talking about "straight" cosine windowing: You take the plain cosine up to it's first root (on either size).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by fmw42 »

NicolasRobidoux wrote:
fmw42 wrote:...I used it some of my scripts (esp cepstrum)...
Don't you use the extended cosine bell window, which is a variant of Hann? http://www.cg.tuwien.ac.at/~theussl/DA/node34.html
I'm talking about "straight" cosine windowing: You take the plain cosine up to it's first root (on either size).
Yes it is similar to Hann, though not exactly the same, but simpler. The term I am familiar with for what I did was called cosine bell roll-off.

In my case examples, the partial cosine window is just a cosine from 0 to pi/2 that is used to taper the image from its edge inward. In the full cosine window, it is effectively from -pi/2 to pi/2 scaled to range 0 to 1.

Of course my tapering application is quite different from what you guys are doing in windowing the resizing filter.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

@Fred: Ah. OK.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

Hmmm. Cosine is not even mentioned in http://www.cg.tuwien.ac.at/~theussl/DA/ ... ming_width.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by anthony »

Do you mean a cosine as in hanning with nice curves at teh top and bottom.

In IM hanning definition is...
0.5 + 05.*cos(x*pi)

range 0 to 1

Or do you mean a cosine function that just 'stops' as it hits its first zero crossing. (a bit like a the welsh parabola)!

cos(x*pi/2)

range 0 to 1

welsh parbola windowing function, defined in IM as...

1.0-x*x

range 0 to 1
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

anthony wrote:...Or do you mean a cosine function that just 'stops' as it hits its first zero crossing. (a bit like a the welsh parabola)!

cos(x*pi/2)

range 0 to 1

welsh parbola windowing function, defined in IM as...

1.0-x*x

range 0 to 1
I mean this last one (the "Cosine parabola", basically described here: http://en.wikipedia.org/wiki/Window_fun ... ine_window, which is different in that n=0 has to do with the left limit of the filter instead of its center).
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by NicolasRobidoux »

@Anthony: You clearly figured it out because it's in this morning's svn.

Thank you!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Add Cosine windowing? (for Cosine-windowed Sinc

Post by anthony »

I figured it out as it's graph is very similar to both a Welsh and Sinc window filters.
I mean a parabola and Cosine are actually closely related!

I believe I added it like the other Sinc's as a 4 lobe, but it could be 3 lobe I suppose.

Do you have any info on whether three lobe is better than 4 lobe windowed sinc's?

All I know is that the 'zoom' program used a 4 lobe Sinc.

I have no idea where 3 lobed lanczos came from when all other windowed sinc was 4
I just kept it as it was when I reprogrammed it all.

Perhaps Welsh and Cosine should also use 3 lobe as they are also 'untapered' Windowing functions.

When IM Examples update these should include Cosine
Image
Image

Cosine is midway between Welsh and Lanczos

Note window filters seem to fall into two groups,
untapered: Welsh, Cosine, Lanczos, Barlett
tapering: Hamming, Hanning, Kaiser, Blackman, Bohman, Parzen

Note the order in the above is in the general amount the filter reduces the first lobe of the Sinc function.
and hence any ringing (sharpening) effect that lobe has.

WARNING: I discovered Kaiser was broken. _ I was texting for it as a filter, not as a window,
Result constants were not set, and graph data showed a serieis of +/- zeros!!!
Now I test for its use both as a window and as a filter. Ditto for Gaussian (as a window that is :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc

Post by NicolasRobidoux »

anthony wrote:...
Do you have any info on whether three lobe is better than 4 lobe windowed sinc's?
...
My student Adam Turcotte is still crunching numbers. (He currently tests 63 enlargement methods against 5 downsizing methods, using 7 enlargement ratios, 14 16-bit 840x840 test images, 13 error metrics, in both linear light and straight sRGB. Needless to say, this runs for a while on the i5 laptop I loaned him. If we had access to a supermachine on which we could install whatever we want...) Preliminary conclusion: It looks like your choice to have windowed Sincs default to 4 lobes except for Lanczos points to the best use of the various windows. But the better windows do OK with 3-lobes too, so you could change the default to 3 for IM7 across the board and I don't think anyone would complain. (You want tighter halo? Go down one lobe (down to 2). Marginally more accurate results when the image is smooth? Go up one lobe (up to 4). Above 4, you're almost certainly wasting flops.)

Generally, I think that Lanczos will come out on top, unless tuned Kaiser manages to beat it by a nose. (And I don't have time to fully optimize Kaiser right now, although I think this is a really promising direction.) With four lobes, however, some of the other windows are putting up a really good fight. (Again: This is from preliminary results, and it has to do with re-enlargment tests, from which one should draw general conclusions with a lot of caveat, proviso, footnotes and small print.)

Also: The Cosine window received some good press in the medical imaging literature as a "reconstructor" (basically, as an upsampler). That's why I'm interested: I'd like to "cover all bases" if possible, and people into medical imaging generally do a good job. But Cosine-windowed Sinc halos more than Lanczos, which should not surprise you at all.
Last edited by NicolasRobidoux on 2012-05-09T06:58:08-07:00, edited 4 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Add Cosine windowing? (for Cosine-windowed Sinc

Post by NicolasRobidoux »

There is a considerable amount of weak evidence that Lanczos 3-lobe is a really good scheme in terms of the sharpness to artifact ratio, considerably better overall than the other 3-lobe windowed-Sincs. I'm not surprised that this kind of wisdom was somehow built into Zoom and ImageMagick.
There is also some evidence that when you increase the number of lobes, some of the other windows claw their way back to the top.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Add Cosine windowing? (for Cosine-windowed Sinc)

Post by anthony »

Okay so we probably should use 3 lobe for Lanczos, Cosine, Welsh (the untapered windows)
and stick to 4 lobe for Hanning, Hamming, Blackman, and Bohman (tapered windows).

This is based on what you said about 'Lanczos' which Cosine and Welsh are similar (if slightly more halo).

I'll note it for future IMv7 option changes (after Adrin has finished), along with LanczosSharp and LanczosSharpest suggestions.

I also noted to make a separate discussion (and separate graphs) for tapered (3 lobe) and untapered (4 lobe) Sinc Windowing functions in documentation. It is a good division for something that has too many filters for easy comparison.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply