[SOLVED] Add Cosine windowing? (for Cosinewindowed Sinc)

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
[SOLVED] Add Cosine windowing? (for Cosinewindowed Sinc)
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 SincApproximating Kernels for Medical Image Interpolation. MICCAI 1999, pp. 210217.
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. Selfinterested, 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
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 SincApproximating Kernels for Medical Image Interpolation. MICCAI 1999, pp. 210217.
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. Selfinterested, 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 20120508T05:01:5207:00, edited 4 times in total.

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
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?

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?

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
Note that I expect Lanczos to win overall.
 fmw42
 Posts: 25562
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
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

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
Don't you use the extended cosine bell window, which is a variant of Hann? http://www.cg.tuwien.ac.at/~theussl/DA/node34.htmlfmw42 wrote:...I used it some of my scripts (esp cepstrum)...
I'm talking about "straight" cosine windowing: You take the plain cosine up to it's first root (on either size).
 fmw42
 Posts: 25562
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
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 rolloff.NicolasRobidoux wrote:Don't you use the extended cosine bell window, which is a variant of Hann? http://www.cg.tuwien.ac.at/~theussl/DA/node34.htmlfmw42 wrote:...I used it some of my scripts (esp cepstrum)...
I'm talking about "straight" cosine windowing: You take the plain cosine up to it's first root (on either size).
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.

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
@Fred: Ah. OK.

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
Hmmm. Cosine is not even mentioned in http://www.cg.tuwien.ac.at/~theussl/DA/ ... ming_width.
 anthony
 Posts: 8883
 Joined: 20040531T19:27:0307:00
 Authentication code: 8675308
 Location: Brisbane, Australia
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
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.0x*x
range 0 to 1
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.0x*x
range 0 to 1
Anthony Thyssen  Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
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).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.0x*x
range 0 to 1

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
@Anthony: You clearly figured it out because it's in this morning's svn.
Thank you!
Thank you!
 anthony
 Posts: 8883
 Joined: 20040531T19:27:0307:00
 Authentication code: 8675308
 Location: Brisbane, Australia
Re: Add Cosine windowing? (for Cosinewindowed Sinc
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
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
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
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/
https://imagemagick.org/Usage/

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc
My student Adam Turcotte is still crunching numbers. (He currently tests 63 enlargement methods against 5 downsizing methods, using 7 enlargement ratios, 14 16bit 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 3lobes 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.)anthony wrote:...
Do you have any info on whether three lobe is better than 4 lobe windowed sinc's?
...
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 reenlargment 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 Cosinewindowed Sinc halos more than Lanczos, which should not surprise you at all.
Last edited by NicolasRobidoux on 20120509T06:58:0807:00, edited 4 times in total.

 Posts: 1944
 Joined: 20100828T11:16:0007:00
 Authentication code: 8675308
 Location: Montreal, Canada
Re: Add Cosine windowing? (for Cosinewindowed Sinc
There is a considerable amount of weak evidence that Lanczos 3lobe is a really good scheme in terms of the sharpness to artifact ratio, considerably better overall than the other 3lobe windowedSincs. 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.
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.
 anthony
 Posts: 8883
 Joined: 20040531T19:27:0307:00
 Authentication code: 8675308
 Location: Brisbane, Australia
Re: Add Cosine windowing? (for Cosinewindowed Sinc)
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.
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/
https://imagemagick.org/Usage/