Luminous Landscape Forum

Raw & Post Processing, Printing => Digital Image Processing => Topic started by: Bart_van_der_Wolf on July 17, 2014, 11:47:12 am

Title: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 17, 2014, 11:47:12 am
Hi folks,

As a result of a discussion in another thread (http://www.luminous-landscape.com/forum/index.php?topic=77949.0) in this forum, and with permission of Nicolas Robidoux who suggested the prodedure, you are free to download and use the attached ImageMagick (http://www.imagemagick.org/) resampling script in a ZIP archive. NOTE, an updated version is available here (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg762951#msg762951).

It is formatted as a Windows batch file that uses ImageMagick for the actual number crunching, and it allows to drag and drop an image on the batch-file icon which can be situated on the desktop or in a folder. It will ask the user for a percentage value by which to resize the image pixel dimensions. Do *not* enter a percentage sign, or any other non-numerical input, just a number. A number larger than 100 will enlarge the image, a number smaller than 100 will reduce the image size. So e.g. just entering 25 will reduce the linear pixel dimensions to one quarter (25%), and 300 will triple the linear pixel dimensions (300%). Decimal separators, e.g. 33.33333 for a reduction to a third of the dimensions are allowed (regional OS settings may allow other separators, I'm not sure).

For ease of use in a Windows computer system, the file can also be added to the User's 'SendTo' file location (e.g. 'C:\Users\YOURNAME\AppData\Roaming\Microsoft\Windows\SendTo' for Windows 7), which will then allow to right mouse-click on an image file in the File Explorer and send it to the script to be processed by ImageMagick (which is assumed to be already installed on the user's computer).

The Resampled image file with a PNG file type and extension will be placed in the same folder or directory as the original file, with a postfix to the filename that indicates the resampling percentage that was used. The script currently will *not* ask if overwriting with that (assumed new) filename is allowed, should it already exist (which is unlikely anyway, but you are now assumed to have been warned!).

Currently the script assumes input files with an sRGB gamma and colorspace. This is something that can be changed in the file itself, or may be added to a future edition as a selection option.

Which brings us to the following. This file is the product of a free exchange of ideas on various fora. People are encouraged to add and share improvements, and versions adapted to other operating systems for which ImageMagick is available, such as UNIX and Mac OS.

Feature requests may be considered, but implementing them is merely a request to volunteers who may or may not wish to spend they spare free time to actually implement and test such modifications. Consider this to be an open collaborative project, so your input is also valued.

Use at your own peril or for enjoyment.

Cheers,
Bart

P.S. This is a works in progress, and the initial file can be improved I'm sure. Feel free to tell me how ...
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 17, 2014, 04:33:53 pm
A more radical approach is to use a pair of gammas, that is, replacing, for example, the linear RGB result (used to compute the luminance alpha channel) by the result of ... -gamma .3333333333333333 -filter LanczosRadius -distort Resize 800% -gamma 3 when blending.
Somehow, I think that sticking to linear light is likely to be better for the "not too much light halo" component, but who knows?
P.S. I just gave a quick and very dirty try to a version in which instead of using luminance I used the average of the through gamma 1/3 and the through gamma 3 results, and the nice thing about it is that it restores the "black/white symmetry". I'm not saying this is the best way to do things, just that it did not turn out badly. HDRI (floating point computation toolchain) may be necessary here. Not sure.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 17, 2014, 04:49:26 pm
A more radical approach is to use a pair of gammas, that is, replacing, for example, the linear RGB result (used to compute the luminance alpha channel) by the result of ... -gamma .3333333333333333 -filter LanczosRadius -distort Resize 800% -gamma 3 when blending.
Somehow, I think that sticking to linear light is likely to be better for the "not too much light halo" component, but who knows?

Indeed Nicolas, that idea had crossed my mind. If it works for one layer, it should also work for the other layer, with opposite gamma boosts, QED.

I will explore that alternative after looking a bit more at some potential optimizations of the current code. And since the results are that well behaved as far as haloing is concerned, sharpening the result is also a possible option to add in a multi-line script. Having a working script will make implementing such alternatives almost trivial.

I've also tried a 'null' conversion, 100 percent same size resampling, which produced only minimal differences in high spatial frequency areas. We'd have to be careful to keep such neutral behavior intact. Need to test that some more.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on July 17, 2014, 06:58:55 pm
Hi Bart,

Thanks for sharing the script. I have followed with interest the other thread and will be trying this over the weekend.
I have a couple of questions related to the script (I'm on the road so I can't test for myself now):

I see that you convert to 16  bits, but does the input file has to be in a specific format or will it work with tiff, jpg or png?

When you mention sRGB gamma, are you referring to sRGB TRC or an approximate gamma (e.g. 2.2)?

From what I can follow in the script, you separate the RGB channels and then recombine adding the calculated alpha channel.
If the script is modified, will it work for monochrome images (not RGB, only gray channel)? Pardon my ingenuity, but I'm thinking of skipping the channel separation part, computing the alpha channel and then recombine the gray with the alpha.

Regards
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 03:56:45 am
Bart:

I have high hopes that something good can be squeezed of this research direction.

1) The reason for using "a pair of opposite gamma spaces" is to restore some black/white symmetry. I think that, however, the alpha should then be computed from either some sort of average of the two results or from linear light, in which case there would be three resampled images used to recombine: through RGB gamma 3 (for example), through RGB gamma 1/3, and linear RGB (gamma 1). These thoughts are a bit rough. Maybe one should work in a pair of direct/reverse video after normalizing the input image, then de-normalizing? Assuming the normalization anchors things at the black and white points (roughly), this would definitely restore black/white symmetry. (Quickly brainstorming, here.)

Poking really quickly into the first idea above, however, one would need a very accurate floating point toolchain to pull off an initial -gamma .3333333333333. Not sure, so this is more a "watch for this" than a true "jump up and down warning". But I'm a bit concerned that Q16 is not enough.

Mixing linear with gamma 3 sounds like a reasonable practical approach. But it definitely does not respect black/white symmetry.

2) EWA LanczosRadius is not interpolatory. Unless you explicitly used a hard wired "null" operation, there will be a difference when do "no-op" resampling. In practice, this difference could be argued to be a good thing (with EWA LanczosSharpest 4, for example). The real question is whether the final answer differs significantly from an infinite precision version of the result. I'm just making sure here, that you know (the reasonable assumption, namely that you do know, can be dangerous) that there are two sources for deviation from the original when resizing to the exact same dimensions: round off error and the fact that the filter actually does some filtering in all cases.

3) Fred Weinhaus has started optimizing the code further and poking around. We'll post things in the IM forums as soon as I've had time to check things over and organize results (and my thoughts) into something halfway intelligible.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 18, 2014, 03:57:17 am
Hi Bart,

Thanks for sharing the script. I have followed with interest the other thread and will be trying this over the weekend.
I have a couple of questions related to the script (I'm on the road so I can't test for myself now):

I see that you convert to 16  bits, but does the input file has to be in a specific format or will it work with tiff, jpg or png?

Hi Frank,

Good question.

In principle ImageMagick supports a whole lot of file formats. Here are the ones it reports on my Microsoft Windows [Version 6.1.7601] installation:
>identify -list format
   Format  Module    Mode  Description
-------------------------------------------------------------------------------
      3FR  DNG       r--   Hasselblad CFV/H3D39II
        A* RAW       rw+   Raw alpha samples
      AAI* AAI       rw+   AAI Dune image
       AI  PDF       rw-   Adobe Illustrator CS2
      ART* ART       rw-   PFS: 1st Publisher Clip Art
      ARW  DNG       r--   Sony Alpha Raw Image Format
      AVI  MPEG      r--   Microsoft Audio/Visual Interleaved
      AVS* AVS       rw+   AVS X image
        B* RAW       rw+   Raw blue samples
      BGR* BGR       rw+   Raw blue, green, and red samples
     BGRA* BGR       rw+   Raw blue, green, red, and alpha samples
      BIE* JBIG      rw-   Joint Bi-level Image experts Group interchange format
 (1.6)
      BMP* BMP       rw-   Microsoft Windows bitmap image
     BMP2* BMP       -w-   Microsoft Windows bitmap image (V2)
     BMP3* BMP       -w-   Microsoft Windows bitmap image (V3)
      BRF* BRAILLE   -w-   BRF ASCII Braille format
        C* RAW       rw+   Raw cyan samples
      CAL* CALS      rw-   Continuous Acquisition and Life-cycle Support Type 1
           Specified in MIL-R-28002 and MIL-PRF-28002
     CALS* CALS      rw-   Continuous Acquisition and Life-cycle Support Type 1
           Specified in MIL-R-28002 and MIL-PRF-28002
   CANVAS* XC        r--   Constant image uniform color
  CAPTION* CAPTION   r--   Caption
      CIN* CIN       rw-   Cineon Image File
      CIP* CIP       -w-   Cisco IP phone image format
     CLIP* CLIP      rw+   Image Clip Mask
CLIPBOARD* CLIPBOARD rw-   The system clipboard
     CMYK* CMYK      rw+   Raw cyan, magenta, yellow, and black samples
    CMYKA* CMYK      rw+   Raw cyan, magenta, yellow, black, and alpha samples
      CR2  DNG       r--   Canon Digital Camera Raw Image Format
      CRW  DNG       r--   Canon Digital Camera Raw Image Format
      CUR* CUR       rw-   Microsoft icon
      CUT* CUT       r--   DR Halo
      DCM* DCM       r--   Digital Imaging and Communications in Medicine image
           DICOM is used by the medical community for images like X-rays.  The
           specification, "Digital Imaging and Communications in Medicine
           (DICOM)", is available at http://medical.nema.org/.  In particular,
           see part 5 which describes the image encoding (RLE, JPEG, JPEG-LS),
           and supplement 61 which adds JPEG-2000 encoding.
      DCR  DNG       r--   Kodak Digital Camera Raw Image File
      DCX* PCX       rw+   ZSoft IBM PC multi-page Paintbrush
      DDS* DDS       rw+   Microsoft DirectDraw Surface
    DFONT* TTF       r--   Multi-face font package (Freetype 2.5.3)
     DJVU* DJVU      ---   Déjà vu
           See http://www.djvuzone.org/ for details about the DJVU format.  The
           DJVU 1.2 specification is available there and at
           ftp://swrinde.nde.swri.edu/pub/djvu/documents/.
      DNG  DNG       r--   Digital Negative
      DOT  DOT       ---   Graphviz
      DPS  DPS       ---   Display Postscript Interpreter
      DPX* DPX       rw-   SMPTE 268M-2003 (DPX 2.0)
           Digital Moving Picture Exchange Bitmap, Version 2.0.
           See SMPTE 268M-2003 specification at http://www.smtpe.org

     DXT1* DDS       rw+   Microsoft DirectDraw Surface
     DXT5* DDS       rw+   Microsoft DirectDraw Surface
      EMF  WMF       r--   Windows Enhanced Meta File
     EPDF  PDF       rw-   Encapsulated Portable Document Format
      EPI  PS        rw-   Encapsulated PostScript Interchange format
      EPS  PS        rw-   Encapsulated PostScript
     EPS2* PS2       -w-   Level II Encapsulated PostScript
     EPS3* PS3       -w+   Level III Encapsulated PostScript
     EPSF  PS        rw-   Encapsulated PostScript
     EPSI  PS        rw-   Encapsulated PostScript Interchange format
      EPT  EPT       rw-   Encapsulated PostScript with TIFF preview
     EPT2  EPT       rw-   Encapsulated PostScript Level II with TIFF preview
     EPT3  EPT       rw+   Encapsulated PostScript Level III with TIFF preview
      ERF  DNG       r--   Epson RAW Format
      EXR  EXR       ---   High Dynamic-range (HDR)
      FAX* FAX       rw+   Group 3 FAX
           FAX machines use non-square pixels which are 1.5 times wider than
           they are tall but computer displays use square pixels, therefore
           FAX images may appear to be narrow unless they are explicitly
           resized using a geometry of "150x100%".

     FITS* FITS      rw-   Flexible Image Transport System
      FPX  FPX       ---   FlashPix Format
  FRACTAL* PLASMA    r--   Plasma fractal image
      FTS* FTS       rw-   Flexible Image Transport System
        G* RAW       rw+   Raw green samples
       G3* FAX       rw-   Group 3 FAX
      GIF* GIF       rw+   CompuServe graphics interchange format
    GIF87* GIF       rw-   CompuServe graphics interchange format (version 87a)
 GRADIENT* GRADIENT  r--   Gradual linear passing from one shade to another
     GRAY* GRAY      rw+   Raw gray samples
   GROUP4* TIFF      rw-   Raw CCITT Group4
       GV  DOT       ---   Graphviz
     HALD*           r--   Identity Hald color lookup table image
      HDR* HDR       rw+   Radiance RGBE image format
HISTOGRAM* HISTOGRAM -w-   Histogram of the image
      HRZ* HRZ       rw-   Slow Scan TeleVision
      HTM* HTML      -w-   Hypertext Markup Language and a client-side image map

     HTML* HTML      -w-   Hypertext Markup Language and a client-side image map

      ICB* TGA       rw-   Truevision Targa image
      ICO* ICON      rw+   Microsoft icon
     ICON* ICON      rw-   Microsoft icon
     INFO  INFO      -w+   The image format and characteristics
   INLINE* INLINE    r--   Base64-encoded inline images
      IPL* IPL       rw+   IPL Image Sequence
   ISOBRL* BRAILLE   -w-   ISO/TR 11548-1 format
      J2C* JP2       rw-   JPEG-2000 Code Stream Syntax (2.1.0)
      J2K* JP2       rw-   JPEG-2000 Code Stream Syntax (2.1.0)
      JBG* JBIG      rw+   Joint Bi-level Image experts Group interchange format
 (1.6)
     JBIG* JBIG      rw+   Joint Bi-level Image experts Group interchange format
 (1.6)
      JNG* PNG       rw-   JPEG Network Graphics
           See http://www.libpng.org/pub/mng/ for details about the JNG
           format.
      JNX* JNX       r--   Garmin tile format
      JP2* JP2       rw-   JPEG-2000 File Format Syntax (2.1.0)
      JPC* JP2       rw-   JPEG-2000 Code Stream Syntax (2.1.0)
     JPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (80)
      JPG* JPEG      rw-   Joint Photographic Experts Group JFIF format (80)
      JPT* JP2       rw-   JPEG-2000 File Format Syntax (2.1.0)
     JSON  JSON      -w+   The image format and characteristics
        K* RAW       rw+   Raw black samples
      K25  DNG       r--   Kodak Digital Camera Raw Image Format
      KDC  DNG       r--   Kodak Digital Camera Raw Image Format
    LABEL* LABEL     r--   Image label
        M* RAW       rw+   Raw magenta samples
      M2V  MPEG      rw+   MPEG Video Stream
      M4V  MPEG      rw+   Raw MPEG-4 Video
      MAC* MAC       r--   MAC Paint
      MAP* MAP       rw-   Colormap intensities and indices
     MASK* MASK      rw+   Image Clip Mask
      MAT  MAT       rw+   MATLAB level 5 image format
    MATTE* MATTE     -w+   MATTE format
      MEF  DNG       r--   Mamiya Raw Image File
     MIFF* MIFF      rw+   Magick Image File Format
      MNG* PNG       rw+   Multiple-image Network Graphics (libpng 1.6.10)
           See http://www.libpng.org/pub/mng/ for details about the MNG
           format.
     MONO* MONO      rw-   Raw bi-level bitmap
      MOV  MPEG      rw+   MPEG Video Stream
      MP4  MPEG      rw+   MPEG-4 Video Stream
      MPC* MPC       rw+   Magick Persistent Cache image format
     MPEG  MPEG      rw+   MPEG Video Stream
      MPG  MPEG      rw+   MPEG Video Stream
      MRW  DNG       r--   Sony (Minolta) Raw Image File
      MSL* MSL       rw+   Magick Scripting Language
     MSVG  SVG       rw+   ImageMagick's own SVG internal renderer
      MTV* MTV       rw+   MTV Raytracing image format
      MVG* MVG       rw-   Magick Vector Graphics
      NEF  DNG       r--   Nikon Digital SLR Camera Raw Image File
      NRW  DNG       r--   Nikon Digital SLR Camera Raw Image File
     NULL* NULL      rw-   Constant image of uniform color
        O* RAW       rw+   Raw opacity samples
      ORF  DNG       r--   Olympus Digital Camera Raw Image File
      OTB* OTB       rw-   On-the-air bitmap
      OTF* TTF       r--   Open Type font (Freetype 2.5.3)
      PAL* UYVY      rw-   16bit/pixel interleaved YUV
     PALM* PALM      rw+   Palm pixmap
      PAM* PNM       rw+   Common 2-dimensional bitmap format
    PANGO* PANGO     r--   Pango Markup Language (Pangocairo 1.36.0)
  PATTERN* PATTERN   r--   Predefined pattern
      PBM* PNM       rw+   Portable bitmap format (black and white)
      PCD* PCD       rw-   Photo CD
     PCDS* PCD       rw-   Photo CD
      PCL  PCL       rw+   Printer Control Language
      PCT* PICT      rw-   Apple Macintosh QuickDraw/PICT
      PCX* PCX       rw-   ZSoft IBM PC Paintbrush
      PDB* PDB       rw+   Palm Database ImageViewer Format
      PDF  PDF       rw+   Portable Document Format
     PDFA  PDF       rw+   Portable Document Archive Format
      PEF  DNG       r--   Pentax Electronic File
      PES* PES       r--   Embrid Embroidery Format
      PFA* TTF       r--   Postscript Type 1 font (ASCII) (Freetype 2.5.3)
      PFB* TTF       r--   Postscript Type 1 font (binary) (Freetype 2.5.3)
      PFM* PFM       rw+   Portable float format
      PGM* PNM       rw+   Portable graymap format (gray scale)
    PICON* XPM       rw-   Personal Icon
     PICT* PICT      rw-   Apple Macintosh QuickDraw/PICT
      PIX* PIX       r--   Alias/Wavefront RLE image format
    PJPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (80)
   PLASMA* PLASMA    r--   Plasma fractal image
      PNG* PNG       rw-   Portable Network Graphics (libpng 1.6.10)
           See http://www.libpng.org/ for details about the PNG format.
    PNG00* PNG       rw-   PNG inheriting bit-depth and color-type from original

    PNG24* PNG       rw-   opaque or binary transparent 24-bit RGB (zlib 1.2.7)
    PNG32* PNG       rw-   opaque or transparent 32-bit RGBA
    PNG48* PNG       rw-   opaque or binary transparent 48-bit RGB
    PNG64* PNG       rw-   opaque or transparent 64-bit RGBA
     PNG8* PNG       rw-   8-bit indexed with optional binary transparency
      PNM* PNM       rw+   Portable anymap
      PPM* PNM       rw+   Portable pixmap format (color)
  PREVIEW* PREVIEW   -w-   Show a preview an image enhancement, effect, or f/x
       PS  PS        rw+   PostScript
      PS2* PS2       -w+   Level II PostScript
      PS3* PS3       -w+   Level III PostScript
      PSB* PSD       rw+   Adobe Large Document Format
      PSD* PSD       rw+   Adobe Photoshop bitmap
     PTIF* TIFF      rw+   Pyramid encoded TIFF
      PWP* PWP       r--   Seattle Film Works
        R* RAW       rw+   Raw red samples
RADIAL-GRADIENT* GRADIENT  r--   Gradual radial passing from one shade to anothe
r
      RAF  DNG       r--   Fuji CCD-RAW Graphic File
      RAS* SUN       rw+   SUN Rasterfile
      RGB* RGB       rw+   Raw red, green, and blue samples
     RGBA* RGB       rw+   Raw red, green, blue, and alpha samples
     RGBO* RGB       rw+   Raw red, green, blue, and opacity samples
      RGF* RGF       rw-   LEGO Mindstorms EV3 Robot Graphic Format (black and w
hite)
      RLA* RLA       r--   Alias/Wavefront image
      RLE* RLE       r--   Utah Run length encoded image
      RMF  DNG       r--   Raw Media Format
      RW2  DNG       r--   Panasonic Lumix Raw Image
      SCR* SCR       r--   ZX-Spectrum SCREEN$
SCREENSHOT* SCREENSHO r--   Screen shot
      SCT* SCT       r--   Scitex HandShake
      SFW* SFW       r--   Seattle Film Works
      SGI* SGI       rw+   Irix RGB image
    SHTML* HTML      -w-   Hypertext Markup Language and a client-side image map

SPARSE-COLOR* TXT       -w+   Sparse Color
      SR2  DNG       r--   Sony Raw Format 2
      SRF  DNG       r--   Sony Raw Format
  STEGANO* STEGANO   r--   Steganographic image
      SUN* SUN       rw+   SUN Rasterfile
      SVG  SVG       rw+   Scalable Vector Graphics (RSVG 2.40.1)
     SVGZ  SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.40.1)
     TEXT* TXT       rw+   Text
      TGA* TGA       rw-   Truevision Targa image
THUMBNAIL* THUMBNAIL -w+   EXIF Profile Thumbnail
     TIFF* TIFF      rw+   Tagged Image File Format (LIBTIFF, Version 4.0.3)
   TIFF64* TIFF      rw-   Tagged Image File Format (64-bit) (LIBTIFF, Version 4
.0.3)
     TILE* TILE      r--   Tile image with a texture
      TIM* TIM       r--   PSX TIM
      TTC* TTF       r--   TrueType font collection (Freetype 2.5.3)
      TTF* TTF       r--   TrueType font (Freetype 2.5.3)
      TXT* TXT       rw+   Text
     UBRL* BRAILLE   -w-   Unicode Text format
      UIL* UIL       -w-   X-Motif UIL table
     UYVY* UYVY      rw-   16bit/pixel interleaved YUV
      VDA* TGA       rw-   Truevision Targa image
    VICAR* VICAR     rw-   VICAR rasterfile format
      VID* VID       rw+   Visual Image Directory
     VIFF* VIFF      rw+   Khoros Visualization image
     VIPS* VIPS      rw+   VIPS image
      VST* TGA       rw-   Truevision Targa image
     WBMP* WBMP      rw-   Wireless Bitmap (level 0) image
     WEBP* WEBP      rw-   WebP Image Format (libwebp 0.4.0 (0202))
      WMF  WMF       r--   Windows Meta File
      WMV  MPEG      rw+   Windows Media Video
      WPG* WPG       r--   Word Perfect Graphics
      X3F  DNG       r--   Sigma Camera RAW Picture File
      XBM* XBM       rw-   X Windows system bitmap (black and white)
       XC* XC        r--   Constant image uniform color
      XCF* XCF       r--   GIMP image
      XPM* XPM       rw-   X Windows system pixmap (color)
      XPS  XPS       r--   Microsoft XML Paper Specification
       XV* VIFF      rw+   Khoros Visualization image
        Y* RAW       rw+   Raw yellow samples
    YCbCr* YCbCr     rw+   Raw Y, Cb, and Cr samples
   YCbCrA* YCbCr     rw+   Raw Y, Cb, Cr, and alpha samples
      YUV* YUV       rw-   CCIR 601 4:1:1 or 4:2:2

* native blob support
r read support
w write support
+ support for multiple images

However, as you know, e.g. TIFF is a flexible format that can contain complex image data, including e.g. Photoshop Layers. IM uses the public LIBTIFF, Version 4 library to decode TIFFs. It therefore depends on others how well that works.

In my experience, that can cause confusion in the interpretation, and ImageMagick can report TAG errors as a result. It may even abort operation. I want to explore that further, to either have the IM code adjusted or find another way around it, to take away the hassle for users. PNG, and JPEG, and GIF seem to convert withot issues, and probably SVG's as well although that may require additional open-source resources. The IM documentation is constantly being updated.

To prevent such issues, one can try using a flattened version of the TIFF, or export it as a PNG (16-b/ch PNGs are fully supported, and produce relatively compact files). While inconvenient, we are after all talking about a specific targeted operation such as preparation for web-publishing. What's more, scripts can be adjusted to do batch processing of all files in subdirectories (even recursive), so one can prepare large volumes of flattened files in advance in their own subdirectories for unattended processing of multiple output sizes can be created, e.g. thumbnails and web display size and mobile device display size, all in their own subdirectories. All it takes is a bit of coding in an easy batch script language.

It may also be a good thing to investigate/test the use of ImageMagick's own MIFF file format (http://www.imagemagick.org/script/miff.php), instead of the current PNGs since it seems to also retain color profile conversions.
 
Quote
When you mention sRGB gamma, are you referring to sRGB TRC or an approximate gamma (e.g. 2.2)?

I have to go by the documented description (http://imagemagick.org/script/command-line-options.php#colorspace) of that IM option. The sRGB colorspace conversion option (-colorspace) is described as:
Quote
sRGB
if R ≤ .0.0031308 then Rs=R/12.92 else Rs=1.055 R ^ (1.0 / 2.4) ? 0.055
if G ≤ .0.0031308 then Gs=B/12.92 else Gs=1.055 R ^ (1.0 / 2.4) ? 0.055
if B ≤ .0.0031308 then Bs=B/12.92 else Bs=1.055 R ^ (1.0 / 2.4) ? 0.055
So that would be a correct sRGB TRC adjustment with linear slope shadow detail based on linear RGB.

Quote
From what I can follow in the script, you separate the RGB channels and then recombine adding the calculated alpha channel.

Correct. The current alpha channel calculation uses the Gray colorspace (Gray = 0.298839*R+0.586811*G+0.114350*B) conversion. But that is just one of many possible alpha channel creations. One could even add a Sigmoidal S-curve to the alpha channel, should that provide better blending between the other layers (one with 'shadow halos' and the other with 'highlight halos)'. It's flexible enough to optimize any way one desires.

Quote
If the script is modified, will it work for monochrome images (not RGB, only gray channel)? Pardon my ingenuity, but I'm thinking of skipping the channel separation part, computing the alpha channel and then recombine the gray with the alpha.

Exactly what I was looking for, a way of avoiding the channel separation and recombination, and 'simply' adding the Alpha channel. That might require promoting a single channel file to a 4 channel RGBA file, with identical RGB channels. This has yet to be tested.

Thanks for thinking along.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 04:16:43 am
Courtesy of Fred Weinhaus, first pass (shell script)
# setup arguments

infile="input_small.png"
scale=800
colorspace1="RGB"
filt="LanczosRadius"
filtering="-filter $filt"
gammaval=3
igammaval=`convert xc: -format "%[fx:1/$gammaval]" info:`
inname=`convert $infile -format "%t" info:`

# resize in RGB with and without gamma and blend by (intensity of non-gamma resized image)
# line1: read input, convert to 16bits and desired colorspace (RGB)
# line2: clone the first image and use LanczosRadius to scale by 800 (in RGB)
# line3: clone the first image, apply gamma, use LanczosRadius to scale by 800 and apply inverse gamma
# line4: clone the resized RGB (no gamma applied) image and convert to gray (intensity) and use -auto-level to stretch to full dynamic range
# line5: composite (-compose over) the images from lines 2,3,4 (in RGB) using the last image as a mask to do the blending
# line6: delete the temps
# line7: convert back to sRGB and save output
convert \( $infile -depth 16 -set colorspace sRGB -colorspace $colorspace1 \) \
\( -clone 0 $filtering -distort Resize $scale% -write tmp1.png \) \
\( -clone 0 -gamma $gammaval $filtering -distort Resize $scale% -gamma $igammaval -write tmp2.png \) \
\( -clone 1 -colorspace gray -auto-level \) \
\( -clone 2 -clone 1 -clone 3 -compose over -composite -write tmp3.png \) \
-delete 0-3 \
-set colorspace $colorspace1 -colorspace sRGB ${inname}_${filt}_blended_s${scale}_g${gammaval}_$colorspace1.png
The -write commands are unnecessary. Quoting Fred: "I also save 3 tmp.png files to show the non-gamma applied resize in RGB, the gamma applied resize in RGB and blended resize in RGB just to be sure the blending was working."
P.S. As pointed out by Bart van der Wolf below, there was a typo, which is now fixed in the now correct code above.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 04:22:10 am
Another brainstorm: Fix the pair of gammas so that near the relevant midtones (which are generally quite low in linear RGB) the blended combination of them resembles linear RGB.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 18, 2014, 04:46:48 am
Bart:

I have high hopes that something good can be squeezed of this research direction.

Nicolas: Same hopes that I have. Thanks for sparking that idea, the results already look promising. A down sample of a star target or a zoneplate/rings target look very nice, which is important for photographers who produce web publishing content. Low aliasing and ringing/haloing is paramount for such operations.

Linear Light mixing of colors with downsampling avoids color issues that can arise with gamma pre-compensated downsampling. So that is also something to keep in mind.

Quote
1) The reason for using "a pair of opposite gamma spaces" is to restore some black/white symmetry. I think that, however, the alpha should then be computed from either some sort of average of the two results or from linear light, in which case there would be three resampled images used to recombine: through RGB gamma 3 (for example), through RGB gamma 1/3, and linear RGB (gamma 1). These thoughts are a bit rough. Maybe one should work in a pair of direct/reverse video after normalizing the input image, then de-normalizing? Assuming the normalization anchors things at the black and white points (roughly), this would definitely restore black/white symmetry. (Quickly brainstorming, here.)

Chaos rules the universe, so a bit of brain storm cannot hurt ;)

Quote
Poking really quickly into the first idea above, however, one would need a very accurate floating point toolchain to pull off an initial -gamma .3333333333333. Not sure, so this is more a "watch for this" than a true "jump up and down warning". But I'm a bit concerned that Q16 is not enough.

Definitely something to keep in mind, especially since only 16-b/ch pre-compiled binaries are available for download and installation by mere mortals. Having said that, we can 'blend away' the erroneous ranges by a good choice of thresholds or shape of the Alpha channel response curve(s). Lot's of testing ahead.

Quote
Mixing linear with gamma 3 sounds like a reasonable practical approach. But it definitely does not respect black/white symmetry.

Agreed.

Quote
2) EWA LanczosRadius is not interpolatory. Unless you explicitly used a hard wired "null" operation, there will be a difference when do "no-op" resampling. In practice, this difference could be argued to be a good thing (with EWA LanczosSharpest 4, for example). The real question is whether the final answer differs significantly from an infinite precision version of the result. I'm just making sure here, that you know (the reasonable assumption, namely that you do know, can be dangerous) that there are two sources for deviation from the original when resizing to the exact same dimensions: round off error and the fact that the filter actually does some filtering in all cases.

Yes, that's why it was only a quick test on a single image to see if there are huge issues with the current implementation. Things are not totally screwed up, they on the contrary produce only small changes in suspect regions with the current implementation.

Quote
3) Fred Weinhaus has started optimizing the code further and poking around. We'll post things in the IM forums as soon as I've had time to check things over and organize results (and my thoughts) into something halfway intelligible.

Great, Fred knows IM scripting (http://www.fmwconcepts.com/imagemagick/) like few others do, although he uses a specific scripting dialect. Anyway, the image results should be identical if the scripting can be ported to other dialects.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 04:51:51 am
For now, http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935) will be the centralized point for discussion of this new approach within the ImageMagick Forums.
I'll jump back and forth between there and here.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 05:19:02 am
With very preliminary results courtesy of Fred Weinhaus (on the "wizard" image, which is a drawing, not a photograph), enlarging through gamma 3 looks better than enlarging through L*a*b*; I suspect that this has to do with the "non-smooth" correspondence between linear light and L*a*b*).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 05:56:42 am
...
Correct. The current alpha channel calculation uses the Gray colorspace (Gray = 0.298839*R+0.586811*G+0.114350*B) conversion. But that is just one of many possible alpha channel creations. One could even add a Sigmoidal S-curve to the alpha channel, should that provide better blending between the other layers (one with 'shadow halos' and the other with 'highlight halos)'. It's flexible enough to optimize any way one desires.
...
IMHO it's really important that the alpha channel be "stable" (this roughly means "boring", with as few kinks and slope discontinuities and whatever) w.r.t. image data so that the blending not add aliasing. A straight line in linear light between the min and max image values (possibly filtered to reject outliers, although noise reduction should have been performed before resampling) fits the bill. It may be possible to do better, but this is a case of "proceed with caution".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 12:57:23 pm
Again courtesy of Fred Weinhaus, the Windows version of the basic command:
convert ( image.png -depth 16 -set colorspace sRGB -colorspace RGB ) ^
( -clone 0 -filter LanczosRadius -distort Resize 800% ) ^
( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.33333333 ) ^
( -clone 1 -colorspace gray -auto-level ) ^
( -clone 2 -clone 1 -clone 3 -compose over -composite ) ^
-delete 0-3 ^
-set colorspace RGB -colorspace sRGB ^
result.png
P.S. As pointed out below by Bart van der Wolf, there was a typo in the code above, which is now fixed.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 18, 2014, 03:42:08 pm
Again courtesy of Fred Weinhaus, the Windows version of the basic command:
convert ( image.png -depth 16 -set colorspace sRGB -colorspace RGB ) ^
( -clone 0 -filter LanczosRadius -distort Resize 800% ) ^
( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.33333333 ) ^
( -clone 1 -colorspace gray -auto-level ) ^
( -clone 1 -clone 2 -clone 3 -compose over -composite ) ^
-delete 0-3 ^
-set colorspace RGB -colorspace sRGB ^
result.png

Hi Nicolas,

Sweet, nice and compact, and totally in memory, no temporary files on disk. My compliments to Fred.

Just one remark, an error IMHO:
( -clone 1 -clone 2 -clone 3 -compose over -composite ) ^
should be
( -clone 2 -clone 1 -clone 3 -compose over -composite ) ^

Clone 1 and Clone 2 should be reversed to remove the shadow-edge undershoot.

Also, '%' signs must be preceded by another percentage sign like this %%, if used in a batch script file instead of on the commandline. So 800% becomes 800%% as Resize percentage in a batch file.

With a small alteration, a batch file also becomes more flexible in drag-and-drop behavior (it accepts any filename dragged on the batch file, and drops a resampled PNG version in the same directory as the original):
convert ( "%~dpnx1" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
( -clone 0 -filter LanczosRadius -distort Resize 800%% ) ^
( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800%% -gamma 0.33333333 ) ^
( -clone 1 -colorspace gray -auto-level ) ^
( -clone 2 -clone 1 -clone 3 -compose over -composite ) ^
 -delete 0-3 ^
 -set colorspace RGB -colorspace sRGB ^
 "%~dpn1_800%%.png"

I'd also have to take another look at the '-auto-level' option to see if it is the best choice for non-synthetic images, rather than a '-contrast-stretch 0x0' or similar.

I'm also still looking for a better solution to retain the original file's color profile (important in a color managed environment), e.g. Adobe RGB or ProPhoto RGB, and assign it again to the output file. If Fred has any suggestions, I'd be a happy camper.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 18, 2014, 05:05:49 pm
...
Clone 1 and Clone 2 should be reversed to remove the shadow-edge undershoot.
...
Indeed.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 18, 2014, 07:13:52 pm
As a result of the code optimization suggested by Fred Weinhaus, and a few tweaks of my own doing, I've updated the Windows batch script file that I posted earlier. I've attached it as a Zip archive.

An important change to the size input was made!

Now one does need to enter a percentage sign (%) after the value if one wants to do a resizing based on percentage. So a 50% entry will reduce the size of the image to half of the original size. The reason I changed it was to add more flexibility in getting an exact output size, instead of a percentage with lots of decimals to avoid 1 pixel round-off errors.

Now, one can alternatively enter the required size of the output in pixels, e.g. 800x800. This will fit the largest original image dimension to 800 pixels in output, regardless whether it's in a landscape or portrait orientation, and scale the shorter dimension proportionally.

I've also made some preparations that will save the resampled image in the highest possible JPEG or PNG quality, by preventing chroma sub-sampling, and setting the compression quality to the maximum, i.e. 100. This is in preparation to a fix (if possible) that will maintain the original file's embedded ICC color profile and embed it in the resampled version as well. For the time being, the output will be a PNG filetype without embedded profile.

Enjoy.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 19, 2014, 07:31:07 am
I've also made some preparations that will save the resampled image in the highest possible JPEG or PNG quality, by preventing chroma sub-sampling, and setting the compression quality to the maximum, i.e. 100. This is in preparation to a fix (if possible) that will maintain the original file's embedded ICC color profile and embed it in the resampled version as well. For the time being, the output will be a PNG filetype without embedded profile.

*** UPDATE ***

I've found a solution for the embedding of the color profile, it seems ...

-convert "input.tif" -write "IMsource.icc" apparently saves a copy of the embedded color profile, and
-profile ÏMsource.icc" will assign that profile to the file that will be written. I'm still searching for a way to do it without having to write a temporary file ...

The script (or rather ImageMagick) will produce warning messages if no profiles are embedded, but the conversion is still performed as intended, just without profile.

Attached is an updated version (version 1.0.3). The tool now copies the original profile that is embedded in the original image, and embeds that in the resized version. It also retains the original file type, so a JPEG remains a JPEG, and a TIF remains a TIF, it just has a different size, as indicated by the new file name, but now with the same profile as the original source.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 19, 2014, 08:36:27 am
Fred Weinhaus http://www.fmwconcepts.com/fmw/fmw.html (http://www.fmwconcepts.com/fmw/fmw.html) has asked me what's wrong with sigmoidization, which does suppress both dark and light halos and has been found to work fairly well. Wrong to the extent that I declare it "dead" as a general purpose resampling method.
(Sigmoidization is an ancestor of the methods discussed here and is described here: http://www.imagemagick.org/Usage/resize/#resize_sigmoidal (http://www.imagemagick.org/Usage/resize/#resize_sigmoidal).)
Basically, what is wrong with it is that it leads to "color separation", and this failing means that if sigmoidization is strong, one has to visually inspect the images to make sure taming the halos did not lead to other artifacts.
This is (apparently) not the case with the new methods discussed here: Resampling through a pair of gamma spaces, and blending with an alpha which is the same over all three channels (a normalized luminance or luma), means that the resulting method satisfies http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796 (http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796). I believe that, provided one works in a colorspace in which channels are monotone w.r.t. a reasonable luminance (RGB or XYZ, for example), this is a very very important property.
-----
There is something else that I believe >>may<< be wrong with sigmoidization: As much as possible, the intermediate color space(s) should >>locally<< be linear, and if nonlinear it should be nonlinear in a very smooth way. The sigmoidal color spaces have "kinks". I believe that in some situations this adds aliasing and color artifacts. "Gamma spaces" are very smooth, and consequently they "locally" (in the sense of near a given color value) are "almost linear". The same could be said of sigmoidal color spaces, but for the same amount of (one of the types of) halo suppression, the extent over which the tangent to a sigmoidal curve approximates it is smaller, overall, than a similarly strong gamma space.
-----
(My apologies if this is not particularly well explained. I edited this poor explanation a bit after Fred Weinhaus made it clear that some of it was more confusing than informative.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 19, 2014, 11:32:24 am
Suggested variant:
Instead of blending the gamma resample with linear light, negate the linear light version of the input image, use the same gamma, resample the same way, undo the gamma, negate again (to be back in linear light).
Then, use the luminance of the average of the two results instead of the linear light resample's luminance (which is not computed anymore), normalized of course, as alpha (blending parameter).
Why? The core of the method is then black/white symmetric.
-----
I am far from certain that this, or something like it, would give better results than the "blend linear light with gamma" version. The HVS (like "the real world) is >>not<< black/white symmetric.
The other difficulty is that for this to work well we most likely need to set the "white" used by the negation operators to fit the input and resampled images well.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 19, 2014, 02:01:03 pm
Great work guys, this is very useful.  A couple of questions:

1) Is the script optimized for enlarging as opposed to shrinking?  I am asking because I see gammas greater than one flying around :-)
2) If one works in AdobeRGB, is it enough to simply change all references of 'sRGB' to 'AdobeRGB' in the script?
3) I sometimes generate a B&W image directly from a Bayer raw file. The resulting full resolution 'grayscale' TIF is still linear (gamma=1).   Assuming one wanted to downsize only, could all the colorspace changing, gamma and cloning be replaced by a single line such as
convert "%~dpnx1" -filter LanczosRadius -distort Resize %Msize% ?
4) If one wanted a TIFF as an output would they simply leave out the -quality line?

Thank you very much!
Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 19, 2014, 02:02:53 pm
Yes it's optimized for enlarging. But I'm hoping something like it can make downsampling better too.
This being said, downsampling through linear light is really hard to beat.
P.S. However, with moderate gamma, this could give you a slider that tames the halos just enough to make up for the fact that linear light can introduce pretty brutal ones with a sharpening resampling filter (like LanczosRadius).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 19, 2014, 03:30:35 pm
Suggested variant:
Instead of blending the gamma resample with linear light, negate the linear light version of the input image, use the same gamma, resample the same way, undo the gamma, negate again (to be back in linear light).
Then, use the luminance of the average of the two results instead of the linear light resample's luminance (which is not computed anymore), normalized of course, as alpha (blending parameter).
Why? The core of the method is then black/white symmetric.
-----
I am far from certain that this, or something like it, would give better results than the "blend linear light with gamma" version. The HVS (like "the real world) is >>not<< black/white symmetric.
The other difficulty is that for this to work well we most likely need to set the "white" used by the negation operators to fit the input and resampled images well.
Again courtesy of Fred Weinhaus, here is sample code
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
\( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 \) \
\( -clone 0 -negate -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 -negate \) \
\( -clone 1 -clone 2 -evaluate-sequence mean -colorspace gray -auto-level \) \
-delete 0 -compose over -composite \
-set colorspace RGB -colorspace sRGB output.png
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 19, 2014, 04:00:04 pm
Crude as it is, the "positive/negative gamma space blending" works amazingly well in quick testing performed by Fred.

Pushing this one step further for downsampling use:

The gamma used for the "positive" resampling (higher gamma -> less dark halos) and the gamma used for the "negative" resampling (higher gamma -> less light halos) could be parameters.

Users would then, for example, downsample using a sharpening filter (like EWA LanczosRadius or, even LanczosSharpest 4, one of the Robidoux ones, LanczosSharp or Lanczos) and move each gamma up from 1 to reduce the corresponding type of halo. The gammas can be set independently.

The goal would be to stay as close as possible to gamma 1 for both (because this is conducive to accurate midtones) without letting halos mess things up.

At least this is the idea.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 19, 2014, 04:12:28 pm
Great work guys, this is very useful.  A couple of questions:

1) Is the script optimized for enlarging as opposed to shrinking?  I am asking because I see gammas greater than one flying around :-)
2) If one works in AdobeRGB, is it enough to simply change all references of 'sRGB' to 'AdobeRGB' in the script?
3) I sometimes generate a B&W image directly from a Bayer raw file. The resulting full resolution 'grayscale' TIF is still linear (gamma=1).   Assuming one wanted to downsize only, could all the colorspace changing, gamma and cloning be replaced by a single line such as
convert "%~dpnx1" -filter LanczosRadius -distort Resize %Msize% ?
4) If one wanted a TIFF as an output would they simply leave out the -quality line?

Thank you very much!
Jack
This is the kind of questions you'd get answers for at the ImageMagick forums http://imagemagick.org/discourse-server/viewforum.php?f=1 (http://imagemagick.org/discourse-server/viewforum.php?f=1)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 03:08:47 am
Samples of the "black/white symmetric" version (compared to resizing through linear RGB with sRGB primaries and L*a*b*) courtesy of Fred Weinhaus: http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935&p=113483#p113483 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935&p=113483#p113483)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 04:33:47 am
Great work guys, this is very useful.  A couple of questions:

1) Is the script optimized for enlarging as opposed to shrinking?  I am asking because I see gammas greater than one flying around :-)

Hi Jack,

Indeed, it does best with enlarging, but it's not all that bad for shrinking either. Afterall, the first implementation, is a blend between a linear gamma and a gamma 3 (= even darker brighter looking) image, which will be approx. gamma 1/2.2 reverted after the blend. As Nicolas said, it's hard to beat linear light for downsampling though.

Quote
2) If one works in AdobeRGB, is it enough to simply change all references of 'sRGB' to 'AdobeRGB' in the script?

To clarify, 'RGB' is used to linearize gamma (assuming sRGB source), and 'sRGB' is used to apply a gamma adjustment (assuming linear gamma source). A linearization followed by a non-linearization should therefore produce the exact same result as one started with. Only the pixel values that were changed in linear light will be different in the result.

For specific profile conversions, such as from Adobe RGB to something else and back, one must use the  '-profile'  option. That option allows to convert between two profiles (by specifying two ICC or ICM profiles), or assign a profile (by specifying a single profile) before saving the new image.

Quote
3) I sometimes generate a B&W image directly from a Bayer raw file. The resulting full resolution 'grayscale' TIF is still linear (gamma=1).   Assuming one wanted to downsize only, could all the colorspace changing, gamma and cloning be replaced by a single line such as
convert "%~dpnx1" -filter LanczosRadius -distort Resize %Msize% ?

When starting with a (adjusted for Bayer CFA transmission differences) linear gamma image, one can indeed skip the prior 'RGB' linearization step, and directly execute the '-distort Resize' option with an amount. Different filters are available, all with different characteristics with regards to sharpness or blurriness and types of artifacts that are suppressed or boosted. The 'LanczosRadius' generally produces excellent results, so that's why it is used for the moment.

Quote
4) If one wanted a TIFF as an output would they simply leave out the -quality line?

Yes, I've added the -quality line to specifically maximize the compression quality of JPEGs and PNGs.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 05:14:22 am
...
Indeed, it does best with enlarging, but it's not all that bad for shrinking either. Afterall, the first implementation, is a blend between a linear gamma and a gamma 3 (= even darker looking) image, which will be approx. gamma 1/2.2 reverted after the blend.
...
There are basically three stages of colorspace conversions:
Preprocessing: Convert from whatever colorspace you are using to something that can be interpreted as linear RGB (or linear w.r.t. some "primaries", like XYZ) or linear grayscale.
If the input is sRGB, this is done with
convert input -set colorspace sRGB -colorspace RGB
If the input is not a built in IM colorspace, you must use http://www.imagemagick.org/Usage/formats/#color_profile (http://www.imagemagick.org/Usage/formats/#color_profile). If the input is Adobe RGB and the profile is imbedded, I think all you need to do is
convert input -profile
and then you are in business. I've posted on the ImageMagick Forums to make sure: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945). Waiting for an educated reply.
Processing (resampling): Besides applying a linear filter, the resampling stage contains colorspace operations. Every single colorspace operation performed here must be undone (in reverse order) so as to accurately return to linear RGB. So, if one applies -gamma 3 before a resizing operation (-distort Resize 800%, for example), one must apply -gamma .33333333333333 after. If one applies -negate before, one must apply -negate after. Not doing so will lead to unacceptable color (or intensity, if in grayscale) drifts. In the colorspace department, what you "do", you must "undo" in a round trip. (The blending is a different story, but what is blended is supposed to be two linear light versions of enlarging the same image. We are staying put in linear RGB in the blending stage.)
Post-processing: Then, you need to return from linear RGB to Adobe RGB (if this is what you want your output image to be). Again, the ImageMagick Forum should illuminate how this is done.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 20, 2014, 05:56:41 am
This is the kind of questions you'd get answers for at the ImageMagick forums http://imagemagick.org/discourse-server/viewforum.php?f=1 (http://imagemagick.org/discourse-server/viewforum.php?f=1)

Ok, thank you.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 20, 2014, 05:59:53 am
Hi Jack,

Indeed, it does best with enlarging, but it's not all that bad for shrinking either. Afterall, the first implementation, is a blend between a linear gamma and a gamma 3 (= even darker looking) image, which will be approx. gamma 1/2.2 reverted after the blend. As Nicolas said, it's hard to beat linear light for downsampling though.

To clarify, 'RGB' is used to linearize gamma (assuming sRGB source), and 'sRGB' is used to apply a gamma adjustment (assuming linear gamma source). A linearization followed by a non-linearization should therefore produce the exact same result as one started with. Only the pixel values that were changed in linear light will be different in the result.

For specific profile conversions, such as from Adobe RGB to something else and back, one must use the  '-profile'  option. That option allows to convert between two profiles (by specifying two ICC or ICM profiles), or assign a profile (by specifying a single profile) before saving the new image.

When starting with a (adjusted for Bayer CFA transmission differences) linear gamma image, one can indeed skip the prior 'RGB' linearization step, and directly execute the '-distort Resize' option with an amount. Different filters are available, all with different characteristics with regards to sharpness or blurriness and types of artifacts that are suppressed or boosted. The 'LanczosRadius' generally produces excellent results, so that's why it is used for the moment.

Yes, I've added the -quality line to specifically maximize the compression quality of JPEGs and PNGs.

Cheers,
Bart

Excellent, thank you Bart.  I have an unrelated question for you but I'll start a separate thread on it.
Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 06:59:50 am
Again courtesy of Fred Weinhaus, here is sample code
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
\( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 \) \
\( -clone 0 -negate -gamma 3 -filter LanczosRadius -distort Resize 800% -gamma 0.333333333333333 -negate \) \
\( -clone 1 -clone 2 -evaluate-sequence mean -colorspace gray -auto-level \) \
-delete 0 -compose over -composite \
-set colorspace RGB -colorspace sRGB output.png

And here is the Windows batch file version:
convert ( input.png -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize 800%% -gamma 0.333333333333333 ) ^
 ( -clone 0 -negate -gamma 3 -filter LanczosRadius -distort Resize 800%% -gamma 0.333333333333333 -negate ) ^
 ( -clone 1 -clone 2 -evaluate-sequence mean -colorspace gray -auto-level ) ^
 -delete 0 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB output.png

However, I find that this (Gamma 3 , Gamma 3) resampled version produces much more visible aliasing that the previous (Linear , Gamma 3) resampled version, both blended in linear light. I know that the goal is to reduce haloing, but we should be careful about the trade-off with other artifacts.

Try the attached top left 150x150 pixel crop of a 1000x1000 pixel Rings (zoneplate) target. The previous version already introduces some aliasing, but on most normal images this would be even less noticeable. This new version produces much more visible aliasing because the lighter aliases are amplified. Perceptually that would be much easier to see, so I wonder if this introduces a higher risk of visibility in average images.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 07:13:58 am
..
Try the attached top left 150x150 pixel crop of a 1000x1000 pixel Rings (zoneplate) target. The previous version already introduces some aliasing, but on most normal images this would be even less noticeable. This new version produces much more visible aliasing because the lighter aliases are amplified. Perceptually that would be much easier to see, so I wonder if this introduces a higher risk of visibility in average images.
...
Indeed, the aliasing is ugly.
I totally agree that artifacts need to be "balanced".
(Note that gamma 3 in reverse video is quite strong. Gamma 3, in and by itself, is quite strong.)
Is this an enlargement? Downsample? Details?
-----
I must say that the "gamma 3/linear" blend, that does not use reverse video gamma 3 instead of linear light, has a lot of things going for it that suggest that it will be more foolproof. Your test points in this direction.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 07:48:22 am
Is this an enlargement? Downsample? Details?

Nicolas, it's a crop from the attached image, and converted from GIF to PNG.
It's also a nice test for display gamma, because viewing an LCD at an angle can change the perceived gamma.

A warning for Photoshop users, if you want to explore in PS you need to first convert this indexed color GIF to RGB, because resizing the GIF in PS produces ugly artifacts, as I mentioned on my (old and needs an update to correct some bug fixed IM filter example) web page (http://bvdwolf.home.xs4all.nl/main/foto/down_sample/down_sample.htm) about the perils of down-sampling.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 07:49:40 am
Did you just resize to the same exact size to check for awful nonsense?
P.S. This is what I missed: input image = ?, operation = ?.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 08:05:12 am
I must say that the "gamma 3/linear" blend, that does not use reverse video gamma 3 instead of linear light, has a lot of things going for it that suggest that it will be more foolproof. Your test points in this direction.

Yes, it will be interesting to see what the ImageMagick audience appreciates more.

The differences between both methods on up-sampled 'normal' images (the few that I have tried) are hardly perceptible.

Down-sampling is worse with the double Gamma 3 approach, although one could create an other optimization specifically for down-sampling, and maybe just switch to linear light for the downsizing with a good filter, potentially followed by a bit of sharpening (High-pass convolution is accurately tweakable in floating point precision, yet quick because it requires only a small support size appropriate for down-sampled detail).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 08:07:31 am
My current inclination is this:

Use the linear/gamma blend, increasing gamma from 1 until dark halos are bearable (without reaching the point where light halos become unberable). This approach works when downsampling too. In this case, however, it's quite likely that gamma will stay near 1 quite often <- guess.
Question: With a sharpening filter (LanczosRadius, for example), is the gamma blended result (with 3 or some other, lesser, value) ever better than the linear light result? (Or course taste enters here, since halo is a personal taste/distaste.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 08:10:38 am
Did you just resize to the same exact size to check for awful nonsense?
P.S. This is what I missed: input image = ?, operation = ?.

I'm not sure I understand.
 
ImageMagic handles GIFs correctly when downsampling, so it should not matter if the GIF is used as source image, or a PNG conversion (the PNG crop was only a file format conversion, no resizing).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 08:12:33 am
Bart: You showed a rings image obtained, if I understand, with the "blend of gamma 3 straight and through negation".
I imagine you resized some input image. What were the dimensions of this input image? In other words, what you showed (both in full and cropped form), is the output of what operation? Your last answer states that what you showed us is the result of downsampling. What ratio/percentage reduction in dimensions?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 08:21:27 am
Bart:
I would guess that we both know that it is unlikely that downsampling more than a trivial amount through something else than linear light should be the basis of a general recommendation.
This being said, when the filter sharpens strongly, it is my guess that deviating a little may sometimes bring benefits.
Now that's a strong statement.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 08:27:36 am
Bart: You showed a rings image obtained, if I understand, with the "blend of gamma 3 straight and through negation".

The only full rings (GIF) image I attached in this thread (Reply #31) is an original Computer Generated Image (CGI), no resampling was involved. The CropTL.png I posted was a simple crop of the top-left corner of that GIF and saved as PNG.

Quote
I imagine you resized some input image. What were the dimensions of this input image?

No, just a CGI original, original size. It's the same GIF as linked to in the Down-sampling webpage.

Quote
In other words, what you showed (both in full and cropped form), is the output of what operation?

CGI generated, original size. It was inspired by this (http://www.realitypixels.com/turk/opensource/ZonePlate.c.txt), but I'm not sure (don't remember) if I used exactly that approach or something I made in Mathematica.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 08:32:00 am
So, what you were basically saying was: "Put this rings image through downsampling with the new methods and see what happens? You'll see that the "gamma 3 negative" version is worse than the other."?
P.S. Just checked by downsampling to 12.5%. Linear light clearly wins, followed by blending the result through gamma 3 with linear light, followed by blending the result through gamma 3 with the result of resampling through negated gamma 3. The last two positions are reversed, however, if one considers overall tone preservation (instead of aliasing). Which makes me wonder if rounding error is part of the problem.
P.S. 2 If using blending with linear light but using gamma 2 instead of 3 as intermediate gamma space, the result of downsampling is visibly less aliased than when resampling through linear light. Maybe I overdid it with gamma 3? Or is this particular to how this Rings test image was constructed?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 08:35:02 am
So, what you were basically saying was: "Put this rings image through downsampling with the new methods and see what happens? You'll see that the "gamma 3 negative" version is worse than the other."?

Yep, although I realise it was not designed/optimized for downsampling in particular.

It's not bad, the Linear/Gamma3 variant is better than the Gamma 3/Gamma 3 variant though.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 01:28:41 pm
Based on the fact that the 1/8 downsampling of the rings image with the luminance-weighted blend of the results of resampling through linear RGB and gamma 2 beats the others (gamma 3 among others), from now on I'll probably stick to this has the central "experimental scheme".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 02:59:26 pm
Based on the fact that the 1/8 downsampling of the rings image with the luminance-weighted blend of the results of resampling through linear RGB and gamma 2 beats the others (gamma 3 among others), from now on I'll probably stick to this has the central "experimental scheme".

Okay, so the initial scheme but with one linear, and one Gamma 2 adjusted linear, resampled layer, blended with a 'boring' luminance alpha mask, and a restoration to an sRGB gamma space. Seems like a good compromise for a flexible up/down-sample algorithm.

That would then be the following in Windows batch file dialect:
convert ( input.png -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -filter LanczosRadius -distort Resize 800%% ) ^
 ( -clone 0 -gamma 2 -filter LanczosRadius -distort Resize 800%% -gamma 0.5 ) ^
 ( -clone 1 -colorspace gray -auto-level ) ^
 ( -clone 2 -clone 1 -clone 3 -compose over -composite ) ^
  -delete 0-3 ^
  -set colorspace RGB -colorspace sRGB output.png

I'll also use that for further optimizations and tests.

Thanks,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 20, 2014, 03:08:38 pm
Exactly, except that it can be made a bit shorter/fast/less memory hungry:
convert ( input.png -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 2 -filter LanczosRadius -distort Resize 800%% -gamma 0.5 ) ^
 ( -clone 0 -filter LanczosRadius -distort Resize 800%% ) ^
 ( -clone 2 -colorspace gray -auto-level ) ^
  -delete 0 ^
  -compose over -composite ^
  -set colorspace RGB -colorspace sRGB output.png
or, in unixese,
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
\( -clone 0 -gamma 2 -filter LanczosRadius -distort Resize 800% -gamma 0.5 \) \
\( -clone 0 -filter LanczosRadius -distort Resize 800% \) \
\( -clone 2 -colorspace gray -auto-level \) \
-delete 0 \
-compose over -composite \
-set colorspace RGB -colorspace sRGB output.png
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 20, 2014, 04:02:05 pm
Exactly, except that it can be made a bit shorter/fast/less memory hungry:

Great, especially for upsampling it can help to reduce memory requirements.

However, I did see (after differencing the file version results and boosting contrast) a tiny bit of difference in a few pixels near the borders.
I need to explore some more ...

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 21, 2014, 04:49:49 am
TTBOMK, ImageMagick does not analyse the pipeline to prevent "trivial conversions". Most likely, this is where the difference comes from: the data conversion/buffer copy should be the identity in infinite precision arithmetic/"perfect" memory management... but is not.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 21, 2014, 11:22:02 am
Fred Weinhaus has added a number of results to http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935#p113483 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935#p113483).
He turned the final results to 8 bit so they don't take so much space by adding -depth 8 just before the output file name.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 21, 2014, 01:30:21 pm
Fred Weinhaus has added a number of results to http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935#p113483 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935#p113483).
He turned the final results to 8 bit so they don't take so much space by adding -depth 8 just before the output file name.

That gives a nice impression. The Gamma 3 variants are not bad on these crops, but the amount of aliasing they introduce is not clear from the posted examples. Maybe adding a small version corner crop of the Rings (Zoneplate) target, will help in getting that alternate side of the coin across. If needed, I have a 16-b/ch PNG version (AdobeRGB profile embedded, therefore Gamma 2.2) of the Rings target available.

I've also implemented the new linear+Gamma 2 blend as default resampling method, with your more efficient code version, in my Windows Batch script file tool (attached). I've also added the option to downsample in linear gamma (with/without sharpening), which currently just uses a single layer resampling with the LanczosRadius filter. The added sharpening will create halos, but they are perceptually invisible for web-publishing when viewed at 100% zoom. I'm still optimizing that, because I want to restrict clipping that may also result.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on July 21, 2014, 05:49:12 pm
WoW!
Excellent script!!
Thanks!!!

BTW, does anybody have a Windows x64 ImageMagick compiled with HDRI and OpenCL enabled?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 21, 2014, 06:14:02 pm
I've also added the option to downsample in linear gamma (with/without sharpening), which currently just uses a single layer resampling with the LanczosRadius filter. The added sharpening will create halos, but they are perceptually invisible for web-publishing when viewed at 100% zoom. I'm still optimizing that, because I want to restrict clipping that may also result.

Excellent, Bart, now downsampling too.

1) May I ask how the sharpening works?  Is it final-size appropriate?
2) Does it make sense to capture sharpen before a 2-3x downsampling?

Thank you.
Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 21, 2014, 06:22:06 pm
WoW!
Excellent script!!
Thanks!!!

Hi,

You're welcome. To show a bit more of what it's about, I've attached a sample of the down-sampling quality as it is right now. I've used the well known Lighthouse image from the PhotoCD sampler CD, and reduced the full resolution to 25%, with sharpening (make sure you watch it at real 100% zoom resolution, by downloading it). Aliasing is very well controlled, fully automatic.

Quote
BTW, does anybody have a Windows x64 ImageMagick compiled with HDRI and OpenCL enabled?

Unfortunately not me. The Q16 version does keep all 8 cores on my computer busy, but precision is limited to 16-bits per channel data round offs.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 21, 2014, 07:12:56 pm
Excellent, Bart, now downsampling too.

1) May I ask how the sharpening works?  Is it final-size appropriate?

Hi Jack,

Yes, the down-sampling is a little better than with the algorithm that Nicolas suggested, but that algorithm isn't all that bad at down-sampling either (it's just better at up-sampling).

It assumes that you started with a decent image. Then down-sampling will effectively result in oversampled detail (more than 1 input pixel per output pixel, preferably more than 2) , which is guaranteed to be the case below 50% of the original size. That automatically means that the highest possible spatial frequencies in the image are at the Nyquist frequency. What follows is that we need to use a proper resampling filter, of which ImageMagick offers several, for down-sampling in order to avoid aliasing artifacts.

When we can assume that the finest detail is reasonably bandwidth limited (by the filtering) before down-sampling, we can in principle use the same generic post-sharpening for all down-sampled images to mitigate the blur that is inherent in (proper) down-sampling. Deconvolution is a detail restoration procedure, and a simple non-iterative version of it is High-pass sharpening. Given the purpose of web-publishing, one can 'exaggerate a little', which is subject to taste.

The High-pass implementation I chose, was aimed at doing it's best at the extreme detail case, so it would not be too obvious (less efficient) in lower detail situations, such as between 50-100% of the original size (or larger). The code line:
-define convolve:scale=^65%%,100 -morphology Convolve DoG:0,0,0.45
will add an amount of 65% of the High-pass effect of a 0.45 sigma pixel radius Difference of Gaussians relative to an impulse signal filter. One can vary the amount of 65% to taste by changing the code, or choose a larger radius by changing the radius.

So, yes it is final size appropriate, especially below 50% of the original size, while assuming 'normal' viewing distances.

Quote
2) Does it make sense to capture sharpen before a 2-3x downsampling?
No it doesn't, that is, assuming we are talking about Capture sharpening. It might even hurt (by increasing aliasing) when using lower quality resampling filters than the ones offered by ImageMagick. However, with Creative 'sharpening' we usually address larger spatial frequencies and local contrast, which would affect tonality or local contrast more than actual sharpness, and thus would still affect the down-scaled image.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 03:41:10 am
Quick notes (gotta run):
1) I am pretty sure that it used to be possible to get HDRI Windows binaries of IM by downloading ImageMagick 7 instead of 6 from the main IM site. Not sure these are maintained anymore. I also vaguely remember astrophysics guys putting out some HDRI versions as well. (Fuzzy pre-coffee memory.)
2) Bart: For downsampling, could you test EWA LanczosSharpest 4 (with halo reduction)?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 22, 2014, 03:56:43 am
Hi Jack,

Yes, the down-sampling is a little better than with the algorithm that Nicolas suggested, but that algorithm isn't all that bad at down-sampling either (it's just better at up-sampling).

It assumes that you started with a decent image. Then down-sampling will effectively result in oversampled detail (more than 1 input pixel per output pixel, preferably more than 2) , which is guaranteed to be the case below 50% of the original size. That automatically means that the highest possible spatial frequencies in the image are at the Nyquist frequency. What follows is that we need to use a proper resampling filter, of which ImageMagick offers several, for down-sampling in order to avoid aliasing artifacts.

When we can assume that the finest detail is reasonably bandwidth limited (by the filtering) before down-sampling, we can in principle use the same generic post-sharpening for all down-sampled images to mitigate the blur that is inherent in (proper) down-sampling. Deconvolution is a detail restoration procedure, and a simple non-iterative version of it is High-pass sharpening. Given the purpose of web-publishing, one can 'exaggerate a little', which is subject to taste.

The High-pass implementation I chose, was aimed at doing it's best at the extreme detail case, so it would not be too obvious (less efficient) in lower detail situations, such as between 50-100% of the original size (or larger). The code line:
-define convolve:scale=^65%%,100 -morphology Convolve DoG:0,0,0.45
will add an amount of 65% of the High-pass effect of a 0.45 sigma pixel radius Difference of Gaussians relative to an impulse signal filter. One can vary the amount of 65% to taste by changing the code, or choose a larger radius by changing the radius.

So, yes it is final size appropriate, especially below 50% of the original size, while assuming 'normal' viewing distances.
No it doesn't, that is, assuming we are talking about Capture sharpening. It might even hurt (by increasing aliasing) when using lower quality resampling filters than the ones offered by ImageMagick. However, with Creative 'sharpening' we usually address larger spatial frequencies and local contrast, which would affect tonality or local contrast more than actual sharpness, and thus would still affect the down-scaled image.

Cheers,
Bart

Excellent, thank you, that 's one fine looking downsized lighthouse.   This is truly useful stuff for all those times (and it's most of the time in my case) that we fine tune the full-rez image but save it without printing, downsizing a copy of it to HDTV size for posting or emailing instead.

One note that may help others: the batch file at first did not work on my Win7 system apparently because MS-DOS was getting confused as to which 'convert' command to use, Imagemagick's or its own.  Inserting the following line just below '@echo off' fixed it:

set PATH=x:...\ImageMagick;%PATH%

with x:...\ImageMagick the full path of where the ImageMagick executables are located on your computer.  Note that %PATH% needs to be AFTER the path to ImageMagick.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 04:02:58 am
2) Bart: For downsampling, could you test EWA LanczosSharpest 4 (with halo reduction)?

Will do. Changing the filter is easy, although it will probably require tweaking of the sharpening parameters.

UPDATE, based on:
-filter Lanczos -define filter:lobes=4 -define filter:blur=0.88451002338585141
Okay, my first comparisons reveal small differences. Some I like, some I don't. It's a trade-off, as usual.

What I like is that it does indeed produce marginally sharper results, however it also produces a slightly more pronounced halo and a tiny bit of ringing. The halo is not a surprise, since a Lanczos windowed Sinc will produce a more pronounced first lobe as the support size is increased. This will allow to get a slight improvement for an unsharpened downsample, so I may change the no-sharpening downsample to use this filter. The ringing is too minor to be noticed at normal display viewing distances.

However, subsequent sharpening might start to boost the ringing as well, so I'll probably leave the LanczosRadius filter in place there (until I find an even better filter for additional sharpening).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 06:31:03 am
Bart: Double checking: You know that LanczosSharpest 4 is meant to be used with -distort Resize, yes?
Otherwise, this matches my experience. The upsampled noise power spectrum plot you showed for it in http://www.luminous-landscape.com/forum/index.php?topic=77949.20 (http://www.luminous-landscape.com/forum/index.php?topic=77949.20) is pretty impressive and it did well in the tests of my former student Adam Turcotte web.cs.laurentian.ca/nrobidoux/misc/AdamTurcotteMastersThesis.pdf (http://web.cs.laurentian.ca/nrobidoux/misc/AdamTurcotteMastersThesis.pdf) so I was hoping your results would not match my rough opinion :)
Also, I thought to tell you that it probably should be used to get rid of the final sharpening step, but it sounds like you figured this out pretty quickly.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 06:36:09 am
If you add final sharpening, I would be very tempted to use the now "standard" EWA LanczosSharp, overcoming the softness with post-sharpening.
It's amazingly good at anti-aliasing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 06:37:59 am
Note that, to the best of my recollection, the lighthouse original is quite strongly sharpened.
In an ideal toolchain, you don't sharpen before downsampling.
P.S. If this is what you used to evaluate EWA LanczosSharpest 4, this pre-sharpening may have skewed the evaluation.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 06:38:13 am
Excellent, thank you, that 's one fine looking downsized lighthouse.   This is truly useful stuff for all those times (and it's most of the time in my case) that we fine tune the full-rez image but save it without printing, downsizing a copy of it to HDTV size for posting or emailing instead.

Jack, that's correct, and given the high quality of the ImageMagick filter implementation, I wouldn't worry about prior Capture sharpening or not, ImageMagick will probably handle it just fine, either way. Of course, proper Capture sharpening doesn't introduce halos either, but that's a different topic.

Quote
One note that may help others: the batch file at first did not work on my Win7 system apparently because MS-DOS was getting confused as to which 'convert' command to use, Imagemagick's or its own.  Inserting the following line just below '@echo off' fixed it:

set PATH=x:...\ImageMagick;%PATH%

with x:...\ImageMagick the full path of where the ImageMagick executables are located on your computer.  Note that %PATH% needs to be AFTER the path to ImageMagick.

Indeed, however, if one allows the ImageMagick installer to do it (see third attachment), that is already taken care of as it prefixes the new Path to Imagemagick to the existing startup file. The drawback of using that option is that new installations will keep prefixing new paths to the PATH environment variable, so one needs to occasionally edit the older locations out (always tricky, so don't make mistakes).

The drawback of your suggestion, is that one needs to edit the batch script file(s) one uses to point to the correct path, each time a new version is installed. Also, the path is expanded each time the script is run until restating the computer (could be solved by saving the old path as a variable (e.g. SET OLDPATH=%PATH%) at the start, and restoring it at the end of the script). So, it's up to the users how they want to tackle this.

Cheers,
Bart


The Windows specific 'convert' topic is documented here (http://www.imagemagick.org/Usage/windows/#convert_issue) in considerable detail.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 06:41:58 am
Bart: Double checking: You know that LanczosSharpest 4 is meant to be used with -distort Resize, yes?

Yes. Thanks for verifying.

Quote
Also, I thought to tell you that it probably should be used to get rid of the final sharpening step, but it sounds like you figured this out pretty quickly.

Just like chaos, trade-offs are a fact of life ;)

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 06:45:51 am
Bart:
With the lighthouse, you use gamma 2 blend, or straight through linear RGB?
P.S. Irregardless, this is a score 1 for EWA LanczosRadius.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 07:34:11 am
Note that, to the best of my recollection, the lighthouse original is quite strongly sharpened.
In an ideal toolchain, you don't sharpen before downsampling.
P.S. If this is what you used to evaluate EWA LanczosSharpest 4, this pre-sharpening may have skewed the evaluation.

Hi Nicolas,

No worries. The original Lighthouse PhotoCD file (IMG0019.PCD) is not (over)sharpened (it's basically a filmscan) at its highest packed resolution (16BASE = 3072x2048 pixels). See attached crops of the original 16BASE size. You are correct that there are versions floating about the internet as 'reference images', using the middle resolution (BASE = 768x512 pixels), which are over'sharpened' with halos. I used the original's 16BASE.

The cruel but real life image I use for testing is the one I posted a link to in another thread (http://www.luminous-landscape.com/forum/index.php?topic=91500.msg744978#msg744978). Here is a direct image link (http://bvdwolf.home.xs4all.nl/temp/0920_Tr_30pct.jpg) for this thread. I will post a down-sampled example, for those who cannot try it themselves (unfortunate non-Imagemagick users), once I've settled on which method to use.

Also a downsample of the 1000x1000px 'Rings' image (link to 16-b/ch PNG version, with AdobeRGB profile (=gamma 2.2) embedded (https://www.dropbox.com/s/e3wmc4lqxervncb/Rings.png)) to 200x200 pixels, another torture test, will tell a lot about the capability to withstand the most tasking aliasing prone cases.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 07:41:09 am
Bart:
With the lighthouse, you use gamma 2 blend, or straight through linear RGB?
P.S. Irregardless, this is a score 1 for EWA LanczosRadius.

Straight linear gamma (EWA) -distort Resize,
-filter LanczosRadius -distort Resize %Msize%
nothing else, except for added sharpening in output gamma space
(which avoids differences between light and dark halo amplitude).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 07:46:22 am
Ok, thank you.
If I read http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113584#p113553 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113584#p113553) correctly, Adobe RGB is a "straight" gamma space (without a linear segment: the power law is used wall to wall), which means that going to some linear RGB is as easy as applying gamma. (See Section 4.3.1.2 of the linked tech report.) Then, the Adobe RGB to linear and linear to gamma 2 (say) conversions can be merged into one single gamma, which can be undone at once. This should improve the acccuracy in a 16 bit toolchain. The "merged gamma" should be
2./2.19921875
P.S. Also, instead of using gamma 2, one could simply blend the Adobe RGB result "as is" with the linear light result. The difference between 2 and 2.19921875 is not great, so why introduce truncation errors? We've found that both gamma 2 and 3 work OK, so there is no reason why gamma 2.19921875 should not work just fine.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 07:48:53 am
If you add final sharpening, I would be very tempted to use the now "standard" EWA LanczosSharp, overcoming the softness with post-sharpening.
It's amazingly good at anti-aliasing.

I'll give it a try. Initial results look interesting, slightly different from LanczosRadius, both are nice.

I'll try it with my 'Rings' image and see what happens. Edit: See P.S.!

Cheers,
Bart

P.S. The verdict is in, LanczosSharp looks slightly cleaner and has an overall higher resolution (see logarithmic FFT attachment, amplitude is higher for LanczosSharp, and slightly less ragged). It really required this FFT analysis to spot the differences, so we are fitting a few more angels on a pinhead.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 08:00:21 am
I'll give it a try. Initial results look interesting, slightly different from LanczosRadius, both are nice.

I'll try it with my 'Rings' image abd see what happens.

Cheers,
Bart
EWA Lanczos should also work fine, especially if you shove your output into JPEG.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 08:04:38 am
If I read http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113584#p113553 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113584#p113553) correctly, Adobe RGB is a "straight" gamma space, which means that going to some linear RGB is as easy as applying gamma. (See Section 4.3.1.2 of the linked tech report.) Then, the Adobe RGB to linear and linear to gamma 2 (say) conversions can be merged into one single gamma, which can be undone at once. This should improve the acccuracy in a 16 bit toolchain. The "merged gamma" should be
2./2.19921875

Correct, the AdobeRGB 1998 profile is 'straight' gamma 2.2 (EDIT: actually 1/2.2, or precompensated gamma 2.2).

Accuracy in the nth significant digit may indeed be improved fractionally, provided one always uses Adobe RGB, but precision will probably be good enough for normal use anyway. A simple to code generic sRGB -> RGB -> sRGB roundtrip will not really be an issue even though the shadows will be a tad lighter (if I'm not mistaken, need to check) in linearized light space (which may even help avoid some clipping).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 08:08:50 am
It makes sense to me to use 2.19921875 as "standard gamma" in which case we already have it when using Adobe RGB, and we only need to undo it to move things through linear RGB.
In the case of sRGB, the toolchain remains as before, except for the value of gamma.
P.S. I really don't like the "linear segment" of sRGB: Fine for compressing, not fine for manipulating the image.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 08:28:49 am
It makes sense to me to use 2.19921875 as "standard gamma" in which case we already have it when using Adobe RGB, and we only need to undo it to move things through linear RGB.
In the case of sRGB, the toolchain remains as before, except for the value of gamma.
P.S. I really don't like the "linear segment" of sRGB: Fine for compressing, not fine for manipulating the image.

I'm not sure, but it may have to do with the popular use of the term 'gamma'. AdobeRGB is actually gamma 1/2.2 (precompensating for the display gamma of 2.2). So it would require a gamma 2.2 to linearize, and a gamma 0.45454545454... to restore.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 09:26:12 am
P.S. I really don't like the "linear segment" of sRGB: Fine for compressing, not fine for manipulating the image.

I also wondered about why that was a 'default' colorspace in ImageMagick (maybe the answer can be found on their forums). From a scientific viewpoint a straightforward gamma 2.2 / 0.454545 pair would seem easier to analyze than one with a straight line segment in the shadows ...

However, ImageMagick is flexible enough to use any real ICC Profile conversion we would like, or even a synthetic 'Custom' one created in Photoshop.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 10:19:51 am
...
P.S. The verdict is in, LanczosSharp looks slightly cleaner and has an overall higher resolution (see logarithmic FFT attachment, amplitude is higher for LanczosSharp, and slightly less ragged). It really required this FFT analysis to spot the differences, so we are fitting a few more angels on a pinhead.
Would you be kind enough to add EWA Lanczos's plot?
P.S. Note: The rings image is very smooth. I believe that with sharp natural images, the difference between, say, LanczosRadius and LanczosSharp (and Lanczos) are more pronounced.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 12:44:05 pm
Would you be kind enough to add EWA Lanczos's plot?

See attachments, a comparison between LanczosSharp and Lanczos filters, both used with -distort Resize in Linear gamma. The attached images are zoomed in (NearestNeighbor) to see the details. The LanczosSharp FFT amplitude is higher suggesting better signal, but otherwise they look rather similar.

Quote
P.S. Note: The rings image is very smooth. I believe that with sharp natural images, the difference between, say, LanczosRadius and LanczosSharp (and Lanczos) are more pronounced.

We'll see, based on the other torture test image of the ship's rigging.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 12:49:25 pm
Bart:

Thank you.

Score 1 for LanczosSharp.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 12:50:32 pm
...
No worries. The original Lighthouse PhotoCD file (IMG0019.PCD) is not (over)sharpened (it's basically a filmscan) at its highest packed resolution (16BASE = 3072x2048 pixels).
...
Indeed, this version is clean.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 01:08:33 pm
...To show a bit more of what it's about, I've attached a sample of the down-sampling quality as it is right now. I've used the well known Lighthouse image from the PhotoCD sampler CD, and reduced the full resolution to 25%, with sharpening (make sure you watch it at real 100% zoom resolution, by downloading it). Aliasing is very well controlled, fully automatic...
It looks really good, although it has too much haloing, dark esp., for my taste.
However, I dislike haloing more than many.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 02:03:56 pm
It looks really good, although it has too much haloing, dark esp., for my taste.
However, I dislike haloing more than many.

I agree the sharpening is still a bit heavy handed, I have yet to calculate a more exact Point Spread Function after down-sampling (PSF). Now that the methods seem to be decided (for the moment), I'll work a bit more on the sharpening characteristics (maybe offer a couple of options, mild/normal/strong) so that one doesn't have to edit the script for certain types of images.

To recap the (current) method choices:
Cheers,

P.S. Descriptions were adjusted
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on July 22, 2014, 02:07:16 pm
Indeed, however, if one allows the ImageMagick installer to do it (see third attachment), that is already taken care of as it prefixes the new Path to Imagemagick to the existing startup file. The drawback of using that option is that new installations will keep prefixing new paths to the PATH environment variable, so one needs to occasionally edit the older locations out (always tricky, so don't make mistakes).

That is why I prefer to install ImageMagick to a directory without the version number, here is
always at c:\Program Files\ImageMagick\ so the path stays the same in all the upgrades.

BTW, for a sharper downsampling without sharpening, EWA Catrom is interesting...
Not "perfect", but usable most of the times.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 02:09:15 pm
Pictus: My opinion is that EWA RobidouxSharp is generally better than EWA CatRom in the "hit me over the head with a baseball bat but hold the second halo" sharpness department.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 02:11:48 pm
To recap the (current) method choices:
  • For general resizing, but especially upsampling: a blend between a gamma 1 and a gamma 1+2 resampling with the LanczosDetail filter.
  • For down-sampling without sharpening: a gamma 1 resampling with the Lanczos4Sharper filter recipe.
  • For down-sampling with sharpening: a gamma 1 resampling with the LanczosSharp filter.
Bart: I don't really understand the first two list items.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on July 22, 2014, 02:38:26 pm
(...)"hit me over the head with a baseball bat but hold the second halo"(...)

LoL!!!!!!!!!

BTW, I was unable to find ImageMagick 7 Win x64 binaries.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 02:47:16 pm
To recap the (current) method choices:
  • For general resizing, but especially upsampling: a blend between a gamma 1 and a gamma 1+2 resampling with the LanczosDetail filter.
  • For down-sampling without sharpening: a gamma 1 resampling with the Lanczos4Sharper filter recipe.
  • For down-sampling with sharpening: a gamma 1 resampling with the LanczosSharp filter.

Bart: I don't really understand the first two list items.

Probably the way I worded it.

First one uses a gamma linearization (conversion to linear light), and a gamma 2 variant of that linearization (1+2 gamma) for actual resampling, and then both are blended in linear light, followed by a gamma conversion from linear light to the original image's gamma. Best used for upsampling, but not too bad for down-sampling either.

Second one uses a linear light resampling with the recipe to simulate a "Lanczos4Sharper" filter. Better for down-sampling, but may need some more sharpening for some images.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 02:51:38 pm
Second one uses a linear light resampling with the recipe to simulate a "Lanczos4Sharper" filter. Better for down-sampling, but may need some more sharpening for some images.
Favor: Could you please refer to it as LanczosSharpest 4 or Lanczos4Sharpest (with or without spaces). With fixed number of lobes, it's constructed as "the one" EWA Jinc-windowed Jinc (a.k.a. EWA Lanczos) deblurred with the minimiser of worst case deviation from the identity when used as a no-op. Hence the "sharpest" (mis)nomer. (Yes, I know there is no "sharpest" EWA lanczos, even if one fixes the number of lobes. But this is what I've been calling those for a long time. What the definition above suggests is that it is the sharpest one that is reasonable to use.)
Also, when I have time, I'll put it into ImageMagick under the name LanczosSharpest so people don't have to type the whole long enchilada with 15 digits numbers and -define filter:lobes=4 etc etc.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 22, 2014, 03:43:53 pm
P.S. I really don't like the "linear segment" of sRGB: Fine for compressing, not fine for manipulating the image.
I also wondered about why that was a 'default' colorspace in ImageMagick (maybe the answer can be found on their forums). From a scientific viewpoint a straightforward gamma 2.2 / 0.454545 pair would seem easier to analyze than one with a straight line segment in the shadows ...

However, ImageMagick is flexible enough to use any real ICC Profile conversion we would like, or even a synthetic 'Custom' one created in Photoshop.

Much of this is over my head so perhaps I am misunderstanding, but well behaved gamma functions need a linear portion near the origin because otherwise they amplify noise uncontrollably there upon conversion to a different gamma.

As Burger says, 'the tangent to the function at the origin is either horizontal (for gamma >1), diagonal (for gamma =1) or vertical (for gamma <1) with no intermediate values.  For gamma <1 this causes extremely high amplification of small intensity values and thus increased noise in dark image regions.'

So the fact that AdobeRGB does not have a linear portion near the origin is actually a big problem as far as deep shadow noise is concerned when going back and forth liberally to linear space.  I think Adobe's ACE engine linearizes the deep shadows when converting.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 22, 2014, 04:03:17 pm
Jack, that's correct, and given the high quality of the ImageMagick filter implementation, I wouldn't worry about prior Capture sharpening or not, ImageMagick will probably handle it just fine, either way. Of course, proper Capture sharpening doesn't introduce halos either, but that's a different topic.

Indeed, however, if one allows the ImageMagick installer to do it (see third attachment), that is already taken care of as it prefixes the new Path to Imagemagick to the existing startup file. The drawback of using that option is that new installations will keep prefixing new paths to the PATH environment variable, so one needs to occasionally edit the older locations out (always tricky, so don't make mistakes).

The drawback of your suggestion, is that one needs to edit the batch script file(s) one uses to point to the correct path, each time a new version is installed. Also, the path is expanded each time the script is run until restating the computer (could be solved by saving the old path as a variable (e.g. SET OLDPATH=%PATH%) at the start, and restoring it at the end of the script). So, it's up to the users how they want to tackle this.

Thanks Bart, I'll look into your instructions.  I copy ImageMagick into a homonymous folder without installing it to keep things simple.  New versions simply get copied on top of it. 

The path command as I added it to your batch file appears to behave like a temporary environment variable, so every time the Command window closes (at the end of each conversion) it is forgotten.  Seems to work for me.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 22, 2014, 04:09:04 pm
I agree the sharpening is still a bit heavy handed

Yes

To recap the (current) method choices:
  • For down-sampling with sharpening: a gamma 1 resampling with the LanczosSharp filter.

Bart/Nicolas,

Is the sharpening in the LanczosSharp filter a separate routine, or does the image just come out sharper off the filter?  If the former, what kind of sharpening does it apply?

Thanks,
Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 04:11:49 pm
Thanks Bart, I'll look into your instructions.  I copy ImageMagick into an homonymous folder without installing it to keep things simple.  New versions simply get copied on top of it.

Yes, that's one other possibility.

Quote
The path command as I added it to your batch file appears to behave like a temporary environment variable, so every time the Command window closes (at the end of each conversion) it is forgotten.

Ah, yes of course. It is probably only effective for the current console session, sweet. I'll see what I can do with the script to implement that, need a bit of time to think of all possible consequences.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 04:18:03 pm
Is the sharpening in the LanczosSharp filter a separate routine, or does the image just come out sharper off the filter?  If the former, what kind of sharpening does it apply?
The EWA Lanczoses are resampling filters with varying sharpness characteristics.
Without any additional sharpening, from sharpest to less sharp:
EWA LanczosSharpest 4 (lobes), EWA LanczosRadius (3 lobes by default), EWA LanczosSharp (3 lobes by default), EWA Lanczos (3 lobes by default).
They are all sharper than, say downsampling with box filtering (a rather low bar :) ). EWA LanczosRadius is about as sharp as the standard tensor (orthogonal) Lanczos Sinc-windowed Sinc 3 lobe (what most people think of when they hear "Lanczos resampling filter").
Bart suggested using EWA LanczosSharp when additional sharpening is applied because it is not a strongly sharpening filter in and by itself. Sharpening after resizing with, say, EWA LanczosSharpest 4, is like putting a big nail through the above mentioned baseball bat. After EWA Lanczos, sticking on a hardened piece of gum (presumably to leave one's teeth imprint on the intended victim, without sullying one's dentures).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 22, 2014, 04:18:39 pm
Yes

But, but, it's already down to 65% ... ;)
Just kidding, I need to sit down and do a proper PSF analysis, and encode that. It's on the ToDo list.

Quote
Is the sharpening in the LanczosSharp filter a separate routine, or does the image just come out sharper off the filter?  If the former, what kind of sharpening does it apply?

It's the latter, it comes out sharper, without additional help (but with some other drawbacks). As usual, it's a balancing act between various types of artifacts. At a small image scale, the drawbacks may be imperceptible.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 22, 2014, 04:51:20 pm
Thanks for the info, the metaphors and the spell check* guys.

Jack

*I was going for homonymous (http://www.merriam-webster.com/dictionary/homonymous), but anonymous works just as well :)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 07:36:20 pm
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113615#p113615 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113615#p113615)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 22, 2014, 09:18:17 pm
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113621#p113621 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25945&p=113621#p113621)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 23, 2014, 03:03:19 am
Thanks for the info, the metaphors and the spell check* guys.

Jack

*I was going for homonymous (http://www.merriam-webster.com/dictionary/homonymous), but anonymous works just as well :)

Fixed in the quoted post, sorry about the wrong guess.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 23, 2014, 09:52:37 am
First: I really like the upsampling result that results from the blend in linear light of a linear gamma resample with a linear gamma with additional gamma 2 resample. It is not perfect in the sense of being totally halo free, but the over and undershoots are at least balanced and relatively mild. Also aliasing is not excessive.

Second: I'm working on the down-sampling side of the coin which also produces good results with the above procedure, but could potentially be better because we need not use such a sharpness preserving filter, as long as we can restore most of the sharpness with e.g. a controlled sharpness restoration operation. After all, downsampling quickly produces over-sampled data which could be sharpened the same, regardless of percentage of down-sampling (especially below 50% of the original size).

Third: In this quest for sharpening, I looked at the Point Spread Function (PSF) of data that was downsampled in linear light, which is known to produce correct blends of RGB colors, but has the nasty habit of producing larger undershoot halos than overshoot halos, which was the reason for the original solution proposed by Nicolas Robidoux. The main issue appears to be the Lanczos windowed Sinc filtering which, while preserving sharpness, generates a huge undershoot in linear light space. That also makes it almost impossible to sharpen further without making matters worse.

This led me to a desire to revisit the downsampling+sharpening procedure, so bare with me for a while as I attempt to tackle the situation. I'm making good progress I think, I've found the Gaussian filter combined with an EWA -distort Resize, to produce no halos in linear light space but is also a bit soft, as expected. However, due to it being so well behaved, it does allow to add a decent amount of sharpening without creating excessive halos. There are some, but not all that visible in normal 100 zoom web conditions, and they are symmetrical (Nicolas will like that).

The down-sampling with standard sharpening code in Windows Batch script dialect would then become something like:
convert ( "input.png" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 -filter Gaussian -distort Resize 800x800 ^
 -set colorspace RGB -colorspace sRGB ^
 -define convolve:scale=^130%%,100 -morphology Convolve DoG:0,0,0.5413815663895142 ^
 "output.png"

The Point Spread Function of the unsharpened (rather soft) version predicts zero haloing (but also a soft image), remove the -convolve code line for that. The Point Spread Function of the sharpened version (see code) predicts some haloing, depending on how the details align with the pixel grid, but generally creates very decent/crisp results with an amount of 130% High-pass filtering. Boosting the amount further will do even more detail enhancement, but may also lead to more clipping (something I try to find a remedy for). Sharpening will also make some aliasing more visible, but that will probably not be too visible in common non-synthetic images.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 23, 2014, 10:40:59 am
Attached a few examples of what the current state of affairs does to some samples that are available online:
1. A demonstration image with lots of nasty brick detail (but also other structures and surfaces) that either renders as mush or breaks up in aliasing artifacts, can be found here (http://bvdwolf.home.xs4all.nl/temp/7640_CO40_FM1-175pct_sRGB.jpg).

2. Another demonstration image also used as a target for some upsampling discussions here on LuLa (http://www.luminous-landscape.com/forum/index.php?topic=91500.0) can be found here (http://bvdwolf.home.xs4all.nl/temp/0920_Tr_30pct.jpg)

3. An image from the PhotoCD sampler disk, the Lighthouse in Maine, by photographer Alan Fink.

All were downsampled with the same settings (maximum 800 pixels, optimized algorithm, with standard (@130%) sharpening), as explained earlier.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 23, 2014, 10:46:24 am
And here (attached) is the updated version (1.1.1) of the full Windows Batch script file.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 23, 2014, 01:17:39 pm
I'm making good progress I think, I've found the Gaussian filter combined with an EWA -distort Resize, to produce no halos in linear light space but is also a bit soft, as expected. However, due to it being so well behaved, it does allow to add a decent amount of sharpening without creating excessive halos. There are some, but not all that visible in normal 100 zoom web conditions, and they are symmetrical (Nicolas will like that).

The down-sampling with standard sharpening code in Windows Batch script dialect would then become something like:
convert ( "input.png" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 -filter Gaussian -distort Resize 800x800 ^
 -set colorspace RGB -colorspace sRGB ^
 -define convolve:scale=^130%%,100 -morphology Convolve DoG:0,0,0.5413815663895142 ^
 "output.png"

The Point Spread Function of the unsharpened (rather soft) version predicts zero haloing (but also a soft image), remove the -convolve code line for that. The Point Spread Function of the sharpened version (see code) predicts some haloing, depending on how the details align with the pixel grid, but generally creates very decent/crisp results with an amount of 130% High-pass filtering. Boosting the amount further will do even more detail enhancement, but may also lead to more clipping (something I try to find a remedy for). Sharpening will also make some aliasing more visible, but that will probably not be too visible in common non-synthetic images.

Hi Bart,

I am a bit out of my league here thinking in the frequency domain, but it seems like a bit of a pity to choose a well-behaved-yet-weaker filter to perform the initial filtering because it's well behaved, and then apply a stronger poorly behaved one in the reconstruction phase to make up for it. 

Does ImageMagick allow to perform some form of decent real deconvolution (such as LR) in-line?   If so, wouldn't it be better to use a gaussian deconvolution operator in the -define line instead of DoG to undo the gaussian filtering?  Or perhaps in addition to a weaker DoG?

As you may have guessed I am allergic to artificially amplified acutance (and alliteration) :)

Jack


Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 23, 2014, 02:46:20 pm
Bart:
Resampling with a sharpening filter through linear light will only give "correct colors" across the board if there is no clipping (unless the clipping happens to be perfectly matched on both sides, a measure 0 event).
Again, I think you know this, but look at this:
Suppose you have a grey impulse on a black background. Resampling with any sharpening filter in any colorspace (does not matter which provided values past black are clipped at some point) will produce undershoots which will be clipped sooner or later. The overshoot of the gray, however, will not be clipped unless the chosen grey is nearly white.
Because of the clipping, the average value has just gone up a nudge.
(Things are a bit more complicated than this, but this gives the drift.)
Conclusion: A method that suitably compresses halos near black and white has the potential of more accurately preserving colors than resampling through linear light, at least in some situations.
It's a balancing game.
My line of attack on this has been to have a very smooth color transformation from linear to "perceptual" and back (gamma "passes" this criterion) so that locally (in colorspace) the gamma space is "almost linear" (meaning that locally it corresponds to an affine transformation applied to linear light). Hopefully, this means mid-tones are accurately resampled (as they are in linear light) but also that tight patterns with components near extreme values don't drift too much either.
(Not totally sure this is intelligibly written. Hopefully enough.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 24, 2014, 11:02:03 am
http://tonykuyper.wordpress.com/2013/05/11/choosing-sides-part-2-using-luminosity-masks-to-sharpen-images/ (http://tonykuyper.wordpress.com/2013/05/11/choosing-sides-part-2-using-luminosity-masks-to-sharpen-images/) which I found in http://www.dpreview.com/forums/post/54065214 (http://www.dpreview.com/forums/post/54065214)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 24, 2014, 12:03:14 pm
Hi Bart,

I am a bit out of my league here thinking in the frequency domain, but it seems like a bit of a pity to choose a well-behaved-yet-weaker filter to perform the initial filtering because it's well behaved, and then apply a stronger poorly behaved one in the reconstruction phase to make up for it.

Hi Jack,

It isn't as grim as you may have been led to believe. With down-sampling we effectively discard a number of pixels, which is an unrecoverable in most situations. We will also introduce some blur, due to the weighted averaging of pixels before creating new ones. Some filters are used to control this weighted averaging. All of these filters have benefits and drawbacks, so we need to find a nice (fit for purpose) filter with the most pleasing properties. We can basically fit the trade-offs into the following diagram.

(http://www.imagemagick.org/Usage/img_diagrams/cubic_survey.gif)

We want to strike some balance between blurring, aliasing, blocking and ringing. Optimizing for one (reducing its prominence), will create more of the other artifacts, so many solutions tend to be optimally balanced somewhere in the middle of all. One may prefer a certain trade-off for certain types of image content (e.g. line drawings versus real photographic images).

Based on Nicolas' particular disgust for dark halo overshoots (ringing), and the general consensus that if we were to tolerate some (to enhance the sensation of sharpness, actually similar to what our eyes do) it should be balanced between over- and undershoot amplitude.

I've been going through most of the available filters in ImageMagick, and looked specifically for those that had relatively symmetrical (low) halo behavior in linear light (because of color mixing/blending benefits), but at the same time exhibited little tendency to generate aliasing artifacts (very likely to occur in downsampling). That takes care of two of the four possible artifact quadrant choices, and leaves blur versus blocking to avoid.

Blur is beneficial in reducing blocking (and noise), but can also be restored to a certain degree with deconvolution. The more advanced the Deconvolution process is, the better the restoration of original detail will be. When noise is restored less than signal is, we get a better Signal-to-Noise ratio.

Quote
Does ImageMagick allow to perform some form of decent real deconvolution (such as LR) in-line?   If so, wouldn't it be better to use a gaussian deconvolution operator in the -define line instead of DoG to undo the gaussian filtering?  Or perhaps in addition to a weaker DoG?

ImageMagic does have a (de)convolution function, but it is (currently) not as advanced as a more involved programs can offer. The 'Lightroom/ACR' version of deconvolution is not all that spectacular (it generate lot of artifacts very quickly), but there are other solutions that can do a spectacular job (e.g. FocusMagic).

ImageMagick's Difference of Gaussians (DoG) function can be used to generate a high-pass filter, but it is used as a single pass deconvolution kernel in the spatial domain, the way I use it. In the way I implemented that line of code, the two Gaussians now become an 'Impulse function', from which a Gaussian is subtracted, effectively producing a High-Pass filtered (high-spatial frequency) version of the image, that is then added to the original image.

Quote
As you may have guessed I am allergic to artificially amplified acutance (and alliteration) :)

And so am I, but a High-Pass filtering can be a very intelligent enhancer of actual detail, provided that the correct dimensions are chosen (!!!). It's the latter that can be achieved with adding a modest amount of the recovered High spatial frequency content, and using a properly dimensioned Gaussian component in the High-pass filter. It can also be (ab)used to add more of the high spatial frequency detail back than is recoverable in a single convolution pass. That is where taste and viewing conditions come into play.

To allow to follow one's own preferences, and allow to do more for certain viewing conditions, or less for specific further processing requirements (e.g. more advanced Deconvolution), I've added a user definable 'Amount' setting in the next release version of the script, so one can pick one's own amount of poison.

I've also found a better (slightly sharper but with less aliasing) filter than Gaussian. It is ....., drum roll ..., an non-standard as yet filter coined RobidouxSoft (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=19823&p=109820#p109820). It is soft, but not as soft as some others, it produces less aliasing artifacts than many others, has relatively symmetrical halo performance in linear light down-sampling, and as such is IMHO one of the best performers if one also allows for a sharpening step to follow its use. And even with sharpening, aliasing performance is not bad (of course depending on image source).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 24, 2014, 12:40:39 pm
http://tonykuyper.wordpress.com/2013/05/11/choosing-sides-part-2-using-luminosity-masks-to-sharpen-images/ (http://tonykuyper.wordpress.com/2013/05/11/choosing-sides-part-2-using-luminosity-masks-to-sharpen-images/) which I found in http://www.dpreview.com/forums/post/54065214 (http://www.dpreview.com/forums/post/54065214)

Hi Nicolas,

Yes, Tony Kuyper addresses the perceptual effects of sharpening. We'll get to that. I'm currently more interested in avoiding clipping, similar to a way I routinely do it in Photoshop with a sharpened version of the image in a layer on top of the unsharpened image.

(http://bvdwolf.home.xs4all.nl/main/downloads/Non-clipped-sharpening.png)

I then blend the amount of the sharpened layer in proportion to the original layer's contrast. The higher/lower the brightness already is, the sharper the image already looks, the less sharpening it requires (maybe perceptually dark tone tolerate more). The extreme contrast that can lead to clipping is tapered of as it approaches the clipping points (White / Black). Now to find a way to simulate that in ImageMagick.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 25, 2014, 03:52:26 am
Hi all,

The down-sampling with sharpening code in Windows Batch script dialect is now:
convert ( "input.png" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 800x800 ^
 -set colorspace RGB -colorspace sRGB ^
 -define convolve:scale=^75%%,100 -morphology Convolve DoG:0,0,0.5007503035749775 ^
 "output.png"

As coded here, it assumes the original is larger that fits in an 800x800 pixel boundary, and will be down-sampled. I've selected an 'amount' setting of 75 (to allow to spot it easily) in the code snippet, but values from 50 to 100 can be considered normal. When set to normal at 50, there is minimal halo risk, and when set to crisp at 100, there is some halo risk (but usually too small to see at 100% display zoom). Therefore, in the full script it is a variable that takes user input, thus allowing to adjust based on personal preference and image content. Higher values can also be used, but currently have a higher risk of causing clipping if there is already high contrast detail near the clipping points (although it does allow to bring out structure in otherwise lower contrast regions in the medium brightness tonal range).

If no post-down-sample sharpening is anticipated to be done, one may better use another algorithm that's sharper by definition than the purposely blurry 'RobidouxSoft', because without additional sharpening there is no risk of boosting halos.

Again, this is specific code for down-sampling, since the Point Spread Function that is used for deconvolution is optimized for that type of image content when using the 'RobidouxSoft' filter settings. When cheating a bit, one can select this algorithm for upsampling, and use a significantly higher 'Amount', but in general one gets better results with the initial more general blending algorithm optimzed for those upsampling scales.

Cheers,
Bart

P.S. Attached you will find the most recent version (1.1.2)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 03:57:23 am
Irregardless (wonderful word) of the occasional invocation of my name, I am watching this thread with immense interest while tending to unrelated urgent tasks.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 04:01:27 am
Hi all,

The down-sampling with sharpening code in Windows Batch script dialect is now:
convert ( "input.png" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 800x800 ^
 -set colorspace RGB -colorspace sRGB ^
 -define convolve:scale=^75%%,100 -morphology Convolve DoG:0,0,0.5007503035749775 ^
 "output.png"
Opinion: Instead of doing your deconvolution in sRGB, convert to a suitably chosen gamma space, deconvolve there, ungamma back to linear RGB, convert to sRGB.
It's that annoying linear bit near black.
Another thing you could try (?) is deconvolve in linear, deconvolve in gamma (converted back to linear) and then blend with the weight of the linear result proportional to itself after auto level.
P.S. I actually like the ring of this last idea a lot. Someone who wants less sharpening (because the above blend is a sharpened result) can blend the result with the plain (not deconvolved) linear RGB result. After that, ship everything back to sRGB (or Adobe RGB).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 25, 2014, 04:53:38 am
Opinion: Instead of doing your deconvolution in sRGB, convert to a suitably chosen gamma space, deconvolve there, ungamma back to linear RGB, convert to sRGB.

Hi Nicolas, opinion noted.

My main concern (well, one of them) as a photographer is the accuracy of color blending when downsampling (e.g. for Web publishing), which by definition requires linear light space. The example image from Eric Brasseur's web page (http://www.4p8.com/eric.brasseur/gamma.html) holds up better in (also my current) linear light resampling than in a different gamma, or even a partial linear gammas blend.

EDIT:Deconvolution is also a weighted blend of multiple neighboring pixels, although perhaps less destructive to color accuracy due to the lower weights of more distant pixels. It does attempt to restore the original signal's color better by 'pulling in' some blur and linearly mixed contamination from surrounding pixels.

I've attached an example of a 50% downsample with your Gamma 1 and additional Gamma 2 blend (which is pretty decent for down-sampling, although better for upsampling), and my current 'RobidouxSoft' Cubic implementation in simple linear light with a sharpening amount of 100 (much better color).

Quote
It's that annoying linear bit near black.

Yes, it's a bitch. Lanczos is pretty awesome (and theoretically optimal) if it were not for that dark side edge halo in linear light.

Quote
Another thing you could try (?) is deconvolve in linear, deconvolve in gamma (converted back to linear) and then blend with the weight of the linear result proportional to itself after auto level.

I'll have a look (when time allows), but I'm afraid that the deconvolution in linear space again amplifies any dark side edge (halo) more that we'd like, making blending mandatory.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 04:57:03 am
My main concern (well, one of them) as a photographer is the accuracy of color blending when downsampling (e.g. for Web publishing), which by definition requires linear light space. The example image from Eric Brasseur's web page (http://www.4p8.com/eric.brasseur/gamma.html) holds up better in (also my current) linear light resampling than in a different gamma, or even a partial linear gammas blend.
See http://www.luminous-landscape.com/forum/index.php?topic=91754.msg747632#msg747632 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg747632#msg747632). If there is clipping, the "by definition" is bent. Brasseur used (TTBOMK) bilinear: no overshoots, no clipping. Boringly soft, yet aliased. (Gotta run.)
P.S. Moire is another example of why it may not pay to worship the linear light god to idolatry level: A non-linear light resampling/sharpening method that is sharp and yet avoids moire beats a pure linear light resampling methods that does. The "thought experiment" I linked above assumes that the pixel values we get are representative of the result of filtering at infinite density and locally averaging somehow. Near identity transformations (resize down by one half of one percent) often violate this.
P.S. I don't mean to trivialize how important linear light is. I just mean that it should be treated as a harsh mistress, not a god. A powerful tool, not a panacea.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 05:55:31 am
Bart:
As you probably know, RobidouxSoft is explicitly designed to preserve (an approximation of what can be crudely described as) mid-tones.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 01:13:17 pm
Bart:
From your latest examples, it looks like the "always downsample through linear light" still stands, despite all the caveats I can muster.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 01:18:23 pm
...
I'll have a look (when time allows), but I'm afraid that the deconvolution in linear space again amplifies any dark side edge (halo) more that we'd like, making blending mandatory.
It's the same trick, namely "luminance weighted gamma resampling" except that resampling is replaced by deconvolution. Then, the downsampling itself is pure linear light processing. Only the deconvolution mixes in gamma. But indeed if the "dark edge" is already ugly in gamma, blending in some linear is not going to help matters. All I could suggest, then, is to push the gamma to 3 and then convert back to linear then sRGB.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 25, 2014, 02:05:06 pm
Bart:
It is possible that, among Keys cubic splines, there is nothing particularly great about RobidouxSoft, Robidoux and RobidouxSharp when used as EWA filter kernels. Maybe, maybe not.
Really: Set c to any value between 0 and .5 (and possibly beyond), or equivalently set b to any value between 1 and 0 (and possibly beyond).
The Robidoux* have c values chosen using reasonable criterions. However, this does not mean that there is not one with a different parameter that is "better".
P.S. OK, OK, OK, a number of independent sources have confirmed that EWA Robidoux is quite good at downsampling, and not all of them are relatives of students. So it probably does not suck. Maybe the newer RobidouxSoft and RobidouxSharp don't suck either. And maybe there is a magical c value just waiting to be discovered.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on July 26, 2014, 12:47:43 am
RobidouxSoft + deconvolution is TOP for down-sampling!
Bye Bye Cornelius Lanczos...  ;D

Here to get the right taste for the amount of sharpness and some experiments, the output
is now a PSD with 3 layers, one soft, one sharp and the last is a sharp with Canny Edge Detection (http://www.imagemagick.org/discourse-server/viewtopic.php?f=4&t=25405).
Experimenting the Canny to help tame down some halos...  

BTW,  ImageMagick 7 beta is at http://www.imagemagick.org/download/binaries/
UPDATE: Scripts not working and the command Convert is now Magick.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 03:48:01 am
http://www.clarkvision.com/articles/image-restoration2 (http://www.clarkvision.com/articles/image-restoration2)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 26, 2014, 05:11:01 am
RobidouxSoft + deconvolution is TOP for down-sampling!
Bye Bye Cornelius Lanczos...  ;D

Hi,

'RobidouxSoft' (with sharpening) is very good, in combination with deconvolution/sharpening. The reason I tried it was due to it's low halo generation and reasonably good resistance to generating aliasing artifacts (typical risk with down-sampling). The low tendency to produce halos was welcome, because simple deconvolution/sharpening tends to create or increase halos.

As Nicolas said, 'RobidouxSoft' is but one of a series of possible filters in the 'Keys Cubic spline' filter family (which takes 2 parameters 'B' and 'C' to define it's behavior), with the most probable candidates along the 2C+B=1 line (see the earlier posted diagram in reply #98), basically ranging from blurred to sharp, while avoiding blocking and aliasing artifacts as much as possible in such a trade-off.

However, when we consider Nicolas' suggestion to mitigate the typical dark side of edge undershoot halo of e.g. a 'Lanczos2' down-sample with a partially linear gamma adjusted blend of two deconvolutions, also looks very promising. What makes 'Lanczos2' interesting is that it naturally exhibits an even better aliasing suppression than 'RobidouxSoft' (which is indeed blurry and needs a lot of sharpening which restores some aliasing). That's why I am currently experimenting with the 'best' settings to tame the undershoot halo which is further exaggerated by deconvolution/sharpening.

So don't say goodbye to Cornelius yet ... ;)

Cheers,
Bart

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 26, 2014, 06:26:10 am
http://www.clarkvision.com/articles/image-restoration2 (http://www.clarkvision.com/articles/image-restoration2)

Yes, I'm a supporter of the Richardson-Lucy deconvolution capabilities (http://www.luminous-landscape.com/forum/index.php?topic=45038.0) in a (photon-shot) noisy image. On very low noise images, a Van Cittert deconvolution does even better in a fewer number of iterations.

However, ImageMagick unfortunately doesn't have such an option/function, so the best would be to eliminate all sharpening, use a somewhat blurry (down-sampling) filter, and post-process with another application. RL deconvolution also removes some of the upsampling blur (but may reveal residual aliasing and blocking). So I consider still using something like a blurry Keys Cubic spline for a '0' sharpening amount setting, even if I ultimately use something else for amounts larger than absolute zero.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on July 26, 2014, 08:57:00 am
Hi Bart,  wonderful stuff.

I unfortunately :)  cannot give feedback on the visuals because I will basically have phone only access over the next three weeks.  Thanks for the tutorial up thread,  very informative.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 10:59:19 am
Bart:
I've not tested it, and I hope it does what I want, but this is what I suggested earlier: Use the luminance gamma blending trick at the deconvolution stage.
convert input.png -depth 16 -set colorspace sRGB -colorspace RGB \
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 800x800 \
 \( -clone 0 -gamma 3 -define convolve:scale=^75%%,100 -morphology Convolve DoG:0,0,0.5007503035749775 -gamma .3333333333333333333 \) \
 \( -clone 0  -define convolve:scale=^75%%,100 -morphology Convolve DoG:0,0,0.5007503035749775 \) \
 \( -clone 2 -colorspace gray -auto-level \) \
 -delete 0 \
 -compose over -composite \
 -set colorspace RGB -colorspace sRGB -depth 8 output.png

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 11:23:15 am
...
BTW,  ImageMagick 7 beta is at http://www.imagemagick.org/download/binaries/
Pictus is pointing this out because ImageMagick 7 is HDRI (floating point) by default. This may mitigate rounding when, for example, doing radical gamma (although some of these operations are done with LUTs internally, in which case it does not matter so much; maybe should use -exec Pow 3 instead of -gamma .333333333333333333 etc).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 26, 2014, 12:19:16 pm
Bart:
I've not tested it, and I hope it does what I want, but this is what I suggested earlier: Use the luminance gamma blending trick at the deconvolution stage.

Yes, that's what I've been trying, but it doesn't work for some reason. It looks like convolving a clone doesn't work as expected, or something like that. I've been trying to find out what's wrong with that, but no success so far.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 12:35:04 pm
Maybe there is a need for more parentheses:
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 800x800 \) \
 \( -clone 0 -gamma 3 -define convolve:scale=^75%%,100 -morphology Convolve DoG:0,0,0.5007503035749775 -gamma .3333333333333333333 \) \
 \( -clone 0  -define convolve:scale=^75%%,100 -morphology Convolve DoG:0,0,0.5007503035749775 \) \
 \( -clone 2 -colorspace gray -auto-level \) \
 -delete 0 \
 -compose over -composite \
 -set colorspace RGB -colorspace sRGB -depth 8 output.png
Also my command line is a bastard child of unixese and windowese.
P.S. I do get results that look fine. Just not sure if they are the way they are supposed to.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 12:42:16 pm
See Blending Kernel with the Identity Kernel at http://www.imagemagick.org/Usage/convolve (http://www.imagemagick.org/Usage/convolve). The command I've been using under linux has a different meaning in Windows because of the double percent. http://www.imagemagick.org/Usage/windows (http://www.imagemagick.org/Usage/windows)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 12:53:32 pm
Bart:
I am enclined to think there is a bug. Unless you are happy with using double double percent ("%%%%") in windows, I think that you may want to file something at http://www.imagemagick.org/discourse-server/viewforum.php?f=3 (http://www.imagemagick.org/discourse-server/viewforum.php?f=3). You'll get help, at least. If I had time I'd search myself through the list.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 12:55:43 pm
I'll try two things: use -set instead of -define. And only set/define the property once (I did it inside every parenthesis for safety and because I don't remember how things are passed around under IM6).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 01:06:18 pm
Reading the documentation, I need to put single quotes around the specification in linux, but you need to use double quotes in Windows. Bastard command line indeed :(
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 01:23:01 pm
Unixese (IM6 Q16: running this on OSX, not linux) that seems to work:
convert input.png -depth 16 -set colorspace sRGB -colorspace RGB \
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 800x800 \
 \( -clone 0 -gamma 3 -set option:convolve:scale '^75%%,100' -morphology Convolve DoG:0,0,0.5007503035749775 -gamma .3333333333333333333 \) \
 \( -clone 0  -set option:convolve:scale '^75%%,100' -morphology Convolve DoG:0,0,0.5007503035749775 \) \
 \( -clone 2 -colorspace gray -auto-level \) \
 -delete 0 \
 -compose over -composite \
 -set colorspace RGB -colorspace sRGB -depth 8 new.png
Watch out for the double quotes: they are ImageMagick's, not the conversion of linux single quotes to Windows double quotes.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 01:38:40 pm
For comparison, here is the result of downsampling with the bilinear filter through linear light.
convert input.png -depth 16 -set colorspace sRGB -colorspace RGB \
 -filter Triangle -resize 800x800 \
 -set colorspace RGB -colorspace sRGB -depth 8 BilinearLinearRGB.png
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 26, 2014, 01:49:33 pm
See Blending Kernel with the Identity Kernel at http://www.imagemagick.org/Usage/convolve (http://www.imagemagick.org/Usage/convolve). The command I've been using under linux has a different meaning in Windows because of the double percent. http://www.imagemagick.org/Usage/windows (http://www.imagemagick.org/Usage/windows)

Hi Nicolas,

It is working, I missed a '^'  . I have yet to analyse if it makes a significant enough difference, and I'm also investigating something else. Another thing is that I also check the results for aliasing, with the Rings-target down-sampled to 200x200 of the original. A simple Lanczos2 down-sample looks similar/better than a sharpened 'RobidouxSoft', unless I can constrain the sharpening halo further. A Bi-linear/Triangle filter looks slick, until one looks at the aliasing performance ..., it's also anout those pesky trade-offs between the four quadrants in the Keys Cubic study.

Cheers,
Bart

P.S. I'm getting better at translating between Windowese and Linuxese.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 01:50:21 pm
Also had a look at the fly downsampled with RobidouxSharp through linear RGB (no more and no less) and it looks quite good too.
Will have to look at more results and candidate methods. But indeed linear light is a harsh mistress.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 03:37:29 pm
These are just three examples out of many possibilities, but RobidouxSharp through linear RGB looks best of the three to me.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 26, 2014, 04:11:52 pm
I also checked His Holiness. RobidouxSharp through linear RGB does look good here too. Not that there can't be something else better of course.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 26, 2014, 06:13:02 pm
These are just three examples out of many possibilities, but RobidouxSharp through linear RGB looks best of the three to me.

Yes, it is sharp, but check with the Rings target, and you may prefer another filter ..., especially if you want to be able and control sharpening, yet limit the risk of generating aliasing artifacts.

I've done my initial analysis of the 'RobidouxSoft' filter and it is an improvement to deconvolve in a blended linear light scenario, compared to the earlier non-blended approach. It suffers less from dark edge side undershoots, and holds up decently with aliasing torture tests (Rings target). More normal subject matter may survive other filters, but I prefer to avoid unpleasant surprises.

For the time being (which may only last a little while), I'll adjust the down-sampling optimized method to this improved method in the next release of the Windows Batch script file version. I have one more optimization to do before releasing that Version 1.1.3, and that is the deconvolution radius which is likely to be slightly different in different gamma spaces. I've also increased the deconvolution kernel's support from 5x5 to 7x7 by adjusting the DoG parameters:
DoG:0,0,0.5007503035749775
to
DoG:3,0,0.5007503035749775
. This should allow to increase the precision to 1 in 16-bit numbers. Maybe overkill, but why drop a few bits when they come almost for free?

The difference will be a slightly slower speed of execution (each pixel will be composed of a weighted average sum of a 49 pixel area instead of a 25 pixel area), and the new blended algorithm will do this twice instead of once, but since we're talking about a smaller image size after down-sampling, the toll will remain tolerable for most applications that require optimal quality (TANSTAAFL (https://en.wikipedia.org/wiki/There_ain%27t_no_such_thing_as_a_free_lunch)).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 02:46:33 am
Bart:
I am a bit concerned that the Rings target being greyscale allows one to differenciate schemes on the basis of types of aliasing that are not the most important. I am way more concerned with colour moire (and tonal drift).
Does it make sense to use a Rings target which uses another colour than white as "1"? The reason for this suggestion is that once you use nonlinear schemes, you need to worry about "colour separation".
In other words, it's not single colour channel aliasing that worries more the most, it's the interference between "independent" aliasing in different colour channels.
Ideally, a monochrome (but not greyscale) Rings image should still be monochrome after resampling. (Of course, monochrome is a bit ambiguous from a perceptual viewpoint but something with proportional RGB channels in linear RGB should work.)
I am not sure, but I think such a test would have killed sigmoidization right off the bat.
P.S. Of course such a target is going to hurt one's eyes, at least when there is color separation. That's kind of the point.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 05:09:30 am
...it is an improvement to deconvolve in a blended linear light scenario, compared to the earlier non-blended approach. It suffers less from dark edge side undershoots, and holds up decently with aliasing torture tests (Rings target). More normal subject matter may survive other filters, but I prefer to avoid unpleasant surprises...
\o/
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 05:14:28 am
Bart:
I am a bit concerned that the Rings target being greyscale allows one to differenciate schemes on the basis of types of aliasing that are not the most important. I am way more concerned with colour moire (and tonal drift).
Does it make sense to use a Rings target which uses another colour than white as "1"? The reason for this suggestion is that once you use nonlinear schemes, you need to worry about "colour separation".

I'm not exactly sure what you mean. If there is no luminance aliasing, and we resample in linear light, how could there be color aliasing? Maybe you have a specific example in mind to spark an idea or a better torture test (I know that faceted insect eyes are hard).

Quote
In other words, it's not single colour channel aliasing that worries more the most, it's the interference between "independent" aliasing in different colour channels.
Ideally, a monochrome (but not greyscale) Rings image should still be monochrome after resampling. (Of course, monochrome is a bit ambiguous from a perceptual viewpoint...)
I am not sure, but I think such a test would have killed sigmoidization right off the bat.

This Rings image (https://www.dropbox.com/s/e3wmc4lqxervncb/Rings.png) is in 16-bit RGB, with an Adobe RGB profile embedded, it's monochrome, not greyscale. I can easily make a version that has a slightly lower contrast (not too low because that will reduce the visibility of low contrast aliases), to avoid clipping.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 05:18:36 am
Bart: I'm being vague because I'm not 100% sure of what I mean.
(How to appear smarter than you are: Keep it vague and let other people figure it out. Then take credit.)
-----
Colorspace and gamma conversions are nonlinear. Luminance weighted gamma blending is nonlinear. Once you do something nonlinearly, you risk "separating colors".
Such effects are hidden if you go through a toolchain with an image in which the channel values are the same, and you are doing things in colour spaces in which every channel is treated identically. To see some of the side effects of nonlinearity, you need different values in individual color channels (assuming you are using colour spaces that treat various channels the same way, like linear RGB, sRGB, Adobe RGB, gamma obtained from linear RGB, XYZ, etc, but not LCh, HSL etc if processed sanely).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 05:52:03 am
Bart:
I may be using the wrong terminology. What I mean is that I'd find more informative the results of processing a rings image that is not "black and white", for example in which "white" is replaced (in 16 bit linear RGB) by [21845,65535,43690], and when producing the "new Rings" test image (in linear light) everything is kept proportional. At least, it would make a better torture test IMHO.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 06:17:47 am
Bart:
I meant something sort of like this: http://web.cs.laurentian.ca/nrobidoux/misc/RingsAlt.png (http://web.cs.laurentian.ca/nrobidoux/misc/RingsAlt.png).
Note: When you do your torture tests, do you take into account that this AdobeRGB (as opposed to sRGB) by doing something like
convert -set colorspace RGB -gamma 0.4547069271758437 ... -set colorspace RGB -gamma 2.19921875 ...
(or importing using the embedded profile)?
The reason is that if you do conversions that assume it is an sRGB image at the input stage, this may introduce mild aliasing right there.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 06:28:31 am
Bart:
Am I missing something: When I look at your 1000x1000 Rings original test image, importing it carefully into my favorite open source image manipulation GUI (NIP2), I see aliasing North, South, East and West (but not in the corners). In the original.
???
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 06:39:19 am
Bart:
Am I missing something: When I look at your 1000x1000 Rings original test image, importing it carefully into my favorite open source image manipulation GUI (NIP2), I see aliasing North, South, East and West (but not in the corners). In the original.
???

Probably caused by gamma differences due to your viewing angle towards your display (anything not perpendicular would cause that). If you're using an LCD display, try moving your head up/down/left right. Do you still see it?

Of course, display zoom must be at 100%, also graphics card settings (larger fonts) may influence how some applications display the real zoom factor.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 07:08:45 am
Probably caused by gamma differences due to your viewing angle towards your display (anything not perpendicular would cause that). If you're using an LCD display, try moving your head up/down/left right. Do you still see it?

Of course, display zoom must be at 100%, also graphics card settings (larger fonts) may influence how some applications display the real zoom factor.

Cheers,
Bart
It's not viewing angle. It does make sense that this may be a rendering effect. The OSX Preview tool (viewing on the laptop screen) is no better (but shows things at a different size than NIP2 even when requesting original size). I'll let this go for now.
P.S. On my MacBook Pro laptop, I see some aliasing when enlarged with (hopefully: linux tool shoved into OSX) as neutral an open source viewer as I know. Hmmm. But this is not calibrated monitor, so who knows.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 07:16:32 am
Bart:
Indeed, when I move my head the aliasing changes!
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 07:43:13 am
I have analyzed the DoG radius settings again, because for down-sampling in linear light and (de)convolution, they are now used in Linear and gamma space. The differences are not huge, but we might as wel use the marginally better radii (even less risk of halo creation). I've also changed the kernel support size from 5x5 to 7x7 as I indicated earlier.

Here are the relevant code lines, in Windowese Batch script file dialect:
( -clone 0 -gamma 3 -define convolve:scale=^75%%,100 -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 ) ^
 ( -clone 0 -define convolve:scale=^75%%,100 -morphology Convolve DoG:3,0,0.4806768770037563 ) ^

I also (for the time being) changed the resampling method used when an amount of '0' sharpening is selected. Instead of a 'RobidouxSoft' Cubic spline with a zero amount of sharpening, it switches to a Quadratic Bi-linear (Triangle filter, in linear light resampling. While boring and soft, Bi-linear does have a bit more tendency towards generating aliasing (e.g. compared to an amount of '1'), so I may change this '0' amount behavior.

Attached is the updated complete Windows Batch script file.

Cheers,
Bart

P.S. Small edit to the code was implemented, better '0' amount sharpening results.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 07:46:19 am
May I suggest
-filter Quadratic -distort Resize
instead of
-filter Triangle -resize
? (Untested.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 07:48:07 am
Bart:
Indeed, when I move my head the aliasing changes!

That shows how critical it is! It's fighting with the regular pixel grid of the LCD display, and even a tiny amount of gamma change will show. The presumably 8-bit/channel display driver does not help either. I'm glad it wasn't something else, because the pixels also have maximum contrast, which is not exactly how the theoretical non-discretely quantized / analog pattern would look.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 08:01:20 am
May I suggest
-filter Quadratic -distort Resize
instead of
-filter Triangle -resize
? (Untested.)

Yes! It behaves better on preventing aliasing, is only marginally blurrier, and as far as halo generation is concerned, there is none.
I've re-packed the ZIP archive with the code changed, kept the same version (1.1.3), and adjusted my earlier post.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 09:12:08 am
Yes! It behaves better on preventing aliasing, is only marginally blurrier, and as far as halo generation is concerned, there are none...
In the tensor (orthogonal = -resize, not -distort Resize) world, quadratic B-spline smoothing is the "forgotten middle child" in the chain of B-splines obtained by successive box filtering on a unitorm grid:
Box, bilinear (linear splines, called Triangle in ImageMagick), quadratic B-spline smoothing (Quadratic), B-spline smoothing (cubic as people normally assume, called Spline in ImageMagick), ...
In libvips, NIP2, and the Masters thesis of my former student Chantal Racette, there is a related interpolatory scheme called VSQBS (Vertex Split followed by Quadratic B-Spline smoothing), which is sharper, more aliased, and also 100% halo-free, like the standard (ImageMagick) version.
Of course we're using it in a cylindrical way here so that story does not quite apply, but still: A good, simple and cheap, halo free scheme.
An important fact about it: The key disc has radius 1.5. That's .5 less than BC-cubic methods (and .5 more than the Triangle filter.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 09:48:02 am
On the subject of quantifying the halo suppression performance, and sharpness; I've attached the target I use for that purpose. The Slanted edge allows to use the procedure discussed here (http://www.luminous-landscape.com/forum/index.php?topic=68089.msg538932#msg538932).

It allows to analyze a 10x oversampled edge profile with a reasonably high contrast. One could use a lower contrast version to specifically target certain brighness ranges, but as it is it gives a good impression of how e.g. roof tops against sky edge transitions would be rendered, and how sharpening affects the resolution and / or acutance.

To keep track of the overall tonality (linearity versus gamma, or sigmoidal tone curve changes), the step-wedge was added, and it also allow to see how the orthogonal (lower contrast) stepped increments of edges are handled. Small edge halos created by down-sampling halos enforce the "Mach band effect (https://en.wikipedia.org/wiki/Mach_bands)" that also human vision uses to improve pattern recognition.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 10:22:00 am
Note: When you do your torture tests, do you take into account that this AdobeRGB (as opposed to sRGB) by doing something like
convert -set colorspace RGB -gamma 0.4547069271758437 ... -set colorspace RGB -gamma 2.19921875 ...
(and possibly using the embedded profile)?

ImageMagick will happily accept it if one overrides (ignores) any embedded profile. It can also make use of it, but for our tests I let it ignore it, and just assume it's sRGB, let it linearize and then do stuff, and revert it to sRGB. I finally re-assign (not convert) the original profile that was temporarily saved.

I do understand that for a fully color-managed test one would need to start with a formal profile conversion, but not all sample images have a profile attached, so one then would assume it's sRGB, but it might be something else. Also, since most tests use monochrome targets, the color part (illuminant, color primaries, black-point) is not critical at all, we're mainly looking at the dominant Luminance component. The proof of the pudding is of course with real color image content, but that works pretty much as expected, especially with linear gamma blending, although the gamma conversions do worry me a bit but they are for a large part only temporary, and blended with a (more) linear component.

I think that a color version of some of the test targets, or very good stress-test images (like the Dalai Lama image) would ensure that an 'sRGB assignment- Linearize - do stuff - convert back to sRGB' will be acceptable in most cases. After all, we are trying to change as few pixels as possible, only remove some or add some ... and who knows what colors are correct there. We can only assume.

It would be a different thing if we wanted to change the entire image's tone curve, boosting shadows and controlling highlights. That would lead to contrast and brightness changes that can (and perceptually should) also change colors.
 
Quote
The reason is that if you do conversions that assume it is an sRGB image at the input stage, this may introduce mild aliasing right there.
P.S.Of course importing with the profile should work too.

Yes, and that is possible when the input images are in fact profiled. One can then use the ImageMagick '-profile' option to assign, or convert from the original profile through the conversions and back to the original profile. But that would for the moment complicate the code a little, with probably few differences in the end (QED).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 10:33:27 am
...The Slanted edge allows to use the procedure discussed here (http://www.luminous-landscape.com/forum/index.php?topic=68089.msg538932#msg538932)...
Cool!
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 10:38:51 am
Bart:
The reason I am concerned about pretending that Adobe RGB is the same as sRGB, or vice versa, is not about primaries or even gamma discrepancies. It's about the fact that sRGB has a linear segment near 0 before turning into a gamma space :( Adobe RGB is a "pure" gamma space :)
This worry is erased if an accurate enough conversion to some linear light is done.
-----
I do agree that most likely this has insignificant impact given that we do round trips.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 10:58:35 am
Cool!

I think so, but I'm biased ...

It does assume photographic source images which can apparently often be characterized by having a Gaussian blur profile (due to multiple blur sources usually adding to a Gaussian distribution, see the central limit theorem (http://www.dspguide.com/ch7/2.htm)). This doesn't allow a good fit to modeling halo overshoots, but it can approximate the underlying blur source (which I used to determine the optimal DoG radii).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Les Sparks on July 27, 2014, 03:10:28 pm
Bart and Nicolas
Thank you for this interesting and informative discussion--some of which is over my head. I've tried the tool and find it does produce great results.
Thanks again.
Les
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on July 27, 2014, 03:55:27 pm
The script is now stunning, thank you! (https://dl.dropboxusercontent.com/u/4686872/opr0BI86.gif)
I was thinking...
It is possible to have default values if we just click enter?

BTW, ImageMagick 7 beta have HDRI, but will not work for the script.
http://www.imagemagick.org/discourse-server/viewtopic.php?t=22934
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 27, 2014, 04:12:41 pm
The script is now stunning, thank you! (https://dl.dropboxusercontent.com/u/4686872/opr0BI86.gif)
I was thinking...
It is possible to have default values if we just click enter?

BTW, ImageMagick 7 beta have HDRI, but will not work for the script.
http://www.imagemagick.org/discourse-server/viewtopic.php?t=22934
Pictus: The command line syntax of 7 is different (but I would think this does not matter here) but most importantly, because results are not clamped to 0,65535 between operations, I would guess that one needs to clamp results before passing things through gamma. (Also, last I checked, IM 7, for some reason, did not work smoothly for me. Maybe there is a bug in the latest beta that was not there before.) I'll look into this when I have a minute...
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 04:31:55 pm
Bart and Nicolas
Thank you for this interesting and informative discussion--some of which is over my head. I've tried the tool and find it does produce great results.
Thanks again.

Hi Les,

You're welcome.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 27, 2014, 04:41:23 pm
The script is now stunning, thank you! (https://dl.dropboxusercontent.com/u/4686872/opr0BI86.gif)
I was thinking...
It is possible to have default values if we just click enter?

Hi,

I'll see what I can do. I'll have to figure out which 'errorlevels' are generated on empty input, after that it should be easy.

EDIT: I think I've solved it, just needs some more testing to make it 'monkey proof'.

Quote
BTW, ImageMagick 7 beta have HDRI, but will not work for the script.
http://www.imagemagick.org/discourse-server/viewtopic.php?t=22934

Because '7' is a Beta version and might not be stable, I have not yet investigated which commands and options have changed. I'd hate to think my own code is faulty and waste time looking for bugs when it may be just fine.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 28, 2014, 07:33:14 am
Wish:
Users: Instead of just giving thumbs up or thumbs down, could you describe what you like or don't like, and specify which of the three main options you use, and possibly the context of your use?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 28, 2014, 09:54:03 am
Okay, attached is version 1.1.4, this time I've only done some cosmetic script changes, like implementing a Default response input that can be accepted by just hitting the <Enter> key. It unfortunately doesn't show those defaults when just hitting the <Enter> key (unless I create a lot more verbose input text), but it  always uses the same ones. I've chosen the default inputs to be: 800x800 as output size 'fit within' maxima, choose the optimized Down-sampling algorithm, and use a sharpening Amount of 100.

These default choices are based on a common requirement for most photographers, to produce web publishing output from large input image files. If one has predominantly different default requirements, they are relatively simple to change in the Batch script file, and of course different keyboard input can always be given to override the default choices for the image at hand.

I plan on working some more on Profile conversions (if applicable) and output compression quality (currently produces uncompromised but large minimum compression amount output, in case one needs to work some more on the image and re-compress with as little loss as possible). But these are more icing on the cake, and I have some other things that I want to address as well (like really prevent clipping when extreme sharpening is required, and optimizing the up-sampling quality). I am already working on those as well.

An interesting tidbit; one can also use a negative sharpening amount with the 'optimized for down-sampling' algorithm, which will obviously does the reverse of sharpening, it will blur the finest detail/edges.

Actually, this could also be a consideration for upsampling where we want to use a sharpness preserving filter method, but want to reduce some edge halo overshoots with a targeted blur. The difficulty is that, in contrast with down-sampling to less than 50% of the original size, the upsampling (blur) radius to use is variable. Also, blurring will not be restricted to edges (unless an edge mask is used), but will also affect smoother regions with the same size detail as the filter. Those pesky trade-offs again ...

Another thing that tends to produce a high level of control with upsampling is, to enlarge to a larger than required size, and sharpen that upon down-sampling to the final size. Of course that will require more memory (larger convolution kernels), and thus takes longer to execute, in addition to the parameter tweaking requirement depending on size (might be a simple function of size, don't know, just thinking aloud).

Some operations, in particular convolutions on large image sizes, could be sped up by performing them as FFT operations in the frequency domain instead of the spatial domain. However, that quickly makes working in floating point precision a requirement rather than an option (and it claims much more application/operational memory, also since image sizes internally scale to integer powers of 2 with padding). A Q16 version of ImageMagick may lose too much precision, although I haven't tested how severe the lost is for our type of operations. Too much stuff to do, can't do it all.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 28, 2014, 10:20:15 am
Wish:
Users: Instead of just giving thumbs up or thumbs down, could you describe what you like or don't like, and specify which of the three main options you use, and possibly the context of your use?

+1

Also would be interesting to know if someone has ported the script to a Mac OS (LinUnixese dialect) version.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on July 28, 2014, 02:09:41 pm
Wish:
Users: Instead of just giving thumbs up or thumbs down, could you describe what you like or don't like, and specify which of the three main options you use, and possibly the context of your use?
my holiday period is about to end and will return home in 2 days. I have been following this with great interest. What version of IM do i need to install?  I am a windows 7 64bit user.
I do have a few images i would love to see upressed.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 28, 2014, 02:39:49 pm
my holiday period is about to end and will return home in 2 days. I have been following this with great interest. What version of IM do i need to install?  I am a windows 7 64bit user.
I do have a few images i would love to see upressed.

Hi Jan,

http://www.imagemagick.org/script/binary-releases.php#windows (http://www.imagemagick.org/script/binary-releases.php#windows) leads to a list of available installers of the current ImageMagick Version 6. I usually install the latest (ImageMagick-6.8.9-6-Q16-x64-dll.exe as of this writing) 16-bit x64 DLL version at the top of that list.

The current upsampling method is quite nice for all sorts of subjects (I just used it to upsample a small graphic chart, which also came out fine), and it may be hard to improve, but we'll try to. It doesn't try to invent detail, like e.g. 'Perfect Resize', but rather stay true to the data that it gets fed (good input quality helps, otherwise it will faithfully enlarge the artifacts that are already there). It manages to maneuver through the minefield of creating new artifacts, carefully avoiding the various traps it encounters.

Enjoy.

Cheers,
Bart

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 28, 2014, 03:28:48 pm
Bart:
There is a possible improvement of the tuning of the EWA Lanczos 3 deblur that I've been thinking of trying out for a long time but never found the time to do carefully.
It goes like this:
Create a synthetic image which is light gray on the left and dark gray on the right, with a perfectly sharp vertical interface between the two.
Now, for all deblurs between the LanczosRadius one and the LanczosSharp one, enlarge this image a lot (128 times, say) with the corresponding EWA Lanczos (with -define filter:blur=value, where value ranges roughtly from .91 to 1).
Crop the result so boundary effects don't matter, and measure the largest undershoot (or overshoot).
Question: Which deblurs are local minimizers? (There may be more than one.)
A similar question concerns the second overshoot (or undershoot).
I expect, without having any solid basis for it, that there is a local minimizer at, roughly, .94 or .95.
-----
There is also a similar optimization that is linked to how I determined that the Keys spline are optimal, among BC-splines, for EWA resampling. It has to do with affine gradients as closely as possible, a very attractive property from a numerical analysis viewpoing. I'll keep that in mind.
The tricky thing is that infinite deblur is most likely the global winner in both cases. It's really local minimizers I'm looking for.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on July 28, 2014, 07:43:16 pm
Okay, attached is version 1.1.4, this time I've only done some cosmetic script changes, like implementing a Default response input that can be accepted by just hitting the <Enter> key. It unfortunately doesn't show those defaults when just hitting the <Enter> key (unless I create a lot more verbose input text), but it  always uses the same ones. I've chosen the default inputs to be: 800x800 as output size 'fit within' maxima, choose the optimized Down-sampling algorithm, and use a sharpening Amount of 100.

You hit the Bull's eye!

Wish:
Users: Instead of just giving thumbs up or thumbs down, could you describe what you like or don't like, and specify which of the three main options you use, and possibly the context of your use?

For any time I need to resize, most will be for web.
This makes sharpened downsample the main thing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 02:33:06 am
Bart:
There is a possible improvement of the tuning of the EWA Lanczos 3 deblur that I've been thinking of trying out for a long time but never found the time to do carefully.
It goes like this:
Create a synthetic image which is light gray on the left and dark gray on the right, with a perfectly sharp vertical interface between the two.
Now, for all deblurs between the LanczosRadius one and the LanczosSharp one, enlarge this image a lot (128 times, say) with the corresponding EWA Lanczos (with -define filter:blur=value, where value ranges roughtly from .91 to 1).
Crop the result so boundary effects don't matter, and measure the largest undershoot (or overshoot).
Question: Which deblurs are local minimizers? (There may be more than one.)
A similar question concerns the second overshoot (or undershoot).
I expect, without having any solid basis for it, that there is a local minimizer at, roughly, .94 or .95.
-----

Hi Nicolas,

I'll see what I can do. Just to be sure, when you talk about 'deblur', you have the IM expert function (like '-define filter:blur=0.95 ') in mind, correct? The deblur expert function does have a nice auto-scaling of the kernel support size property, although it is probably less effective than a (slower) Deconvolution in actually restoring some detail.

The only reservation I have about the type of target you describe is, that in practice such a sharp edge can only be provided by a vector / CGI image source. A digital camera will always create a small amount of blur (I've rarely seen an image with less than 0.7 Gaussian blur with very good lenses, unless already sharpened to death). That is due to the residual lens aberrations, the IR and optional optical Low-pass filters, and the sensor element's area sampling (it's not a point sampler). Then there is usually a Bayer CFA demosaicing required as well, to reconstruct a full RGB image from undersampled colors.

So with that in mind, while a sharp discontinuity makes a nice torture test, I'll probably also do the same test with a, say Gaussian 0.39 (a theoretical 1 pixel wide sharp edge transition from an area sampling device), pre-blurred edge. That's not point-sample Gaussian blur, but one with an adjusted shape, like my PSF generator tool (http://bvdwolf.home.xs4all.nl/main/foto/psf/PSF_generator.html) can produce. That would allow to design an operation that does not attempt to overcompensate for artifacts that are never encountered in real images. Cartoons and other line drawings, are perhaps better vectorized before resampling anyway, although it would be nice if they too can be handled reasonably well with the same resampling method.

Quote
There is also a similar optimization that is linked to how I determined that the Keys spline are optimal, among BC-splines, for EWA resampling. It has to do with affine gradients as closely as possible, a very attractive property from a numerical analysis viewpoint. I'll keep that in mind.
The tricky thing is that infinite deblur is most likely the global winner in both cases. It's really local minimizers I'm looking for.

I'm not sure I fully understand, could you explain? I'm always in for optimizations, so I want to understand exactly what can be done to see if it can be implemented in a practical way ...

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 04:41:01 am
Bart: Forget about the two optimizations I suggested, at least for now. I should not offload such things without being willing to do a very clear write up.
-----
I would like to suggest a change to your script that sort of addresses the same issues:
Right now, you have variable sharpening for downsampling. Could we have variable sharpening, though -define filter:blur=value, for the "generic" (for upsampling, mostly) luminance weighted gamma blended resampling (as opposed to deconvolution) method?
This would mean, for example, is that you'd always use -filter Lanczos instead of -filter LanczosRadius, and that:
sharpening = 0 sets -define filter:blur=1  <- regular EWA lanczos
sharpening = 100 sets -define filter:blur=0.88549061701764 <- deblur for EWA LanczosSharpest 3, slightly different from the LanczosSharpest 4 value
default sharpening would be LanczosRadius obtained, since we use -filter Lanczos across the board, by setting -define filter:blur=.9264075766146068 <- deblur that defines LanczosRadius in terms of plain Lanczos
References: http://web.cs.laurentian.ca/nrobidoux/misc/AdamTurcotteMastersThesis.pdf (http://web.cs.laurentian.ca/nrobidoux/misc/AdamTurcotteMastersThesis.pdf) Section 4.8.6.7 and http://www.imagemagick.org/Usage/filter/nicolas/#upsampling (http://www.imagemagick.org/Usage/filter/nicolas/#upsampling)
Then, hopefully, users would let us know what works for them. But they also could adjust the upsampling to the blurriness of the source material, or taste, and actually could turn up anti-aliasing by setting the sharpening level to a low value (that corresponds, for example, to EWA LanczosSharp, which I quite like).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 05:15:37 am
...
So with that in mind, while a sharp discontinuity makes a nice torture test, I'll probably also do the same test with a, say Gaussian 0.39 (a theoretical 1 pixel sharp edge transition from an area sampling device), pre-blurred edge. That's not point-sample Gaussian blur, but one with an adjusted shape, like my PSF generator tool (http://bvdwolf.home.xs4all.nl/main/foto/psf/PSF_generator.html) can produce. That would allow to design an operation that does not attempt to overcompensate for artifacts that are never encountered in real images. Cartoons and other line drawings, are perhaps better vectorized before resampling anyway, although it would be nice if they too can be handled reasonably well with the same resampling method...
Good point. I do tend to optimize based on absolute worst case scenarios.
On the other hand, put a premium lense on a medium format camera and push the result through a top of the line demosaic method, and you'd be surprised how sharp things can be.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 06:23:14 am
Good point. I do tend to optimize based on absolute worst case scenarios.
On the other hand, put a premium lense on a medium format camera and push the result through a top of the line demosaic method, and you'd be surprised how sharp things can be.

Nicolas, I have done that, and none of them get much below 0.7 sigma Gaussian Blur (with 100% fill-factor assumption), at their best aperture, in the center of the image circle. Of course most medium formats also aliase like hell, due to the absent OLPF. While it is good to keep an eye on the absolute worst case scenario (for robustness of calculations), it can also lead to sub-optimal results for more realistic cases. Attached, as plots, the current upsampling compromise as implemented in the script, a linear light blend of a linear & gamma 2, EWA LanczosRadius upsample of your suggested test target (attached in ZIP) by a factor of 128x.

Here is the blur kernel I used on the second target (after first normalizing to a sum of 1.0):
8.240474063989279e-21, 6.8060100125474815e-15, 1.1327893594730693e-11, 9.077705692513543e-11, 1.1327893594730693e-11, 6.8060100125474815e-15, 8.240474063989279e-21
6.8060100125474815e-15, 5.6212509051295795e-9, 0.0000093559856663739, 0.00007497500186815324, 0.0000093559856663739, 5.6212509051295795e-9, 6.8060100125474815e-15
1.1327893594730693e-11, 0.0000093559856663739, 0.015572062031516103, 0.12478806846616428, 0.015572062031516103, 0.0000093559856663739, 1.1327893594730693e-11
9.077705692513543e-11, 0.00007497500186815324, 0.12478806846616428, 1, 0.12478806846616428, 0.00007497500186815324, 9.077705692513543e-11
1.1327893594730693e-11, 0.0000093559856663739, 0.015572062031516103, 0.12478806846616428, 0.015572062031516103, 0.0000093559856663739, 1.1327893594730693e-11
6.8060100125474815e-15, 5.6212509051295795e-9, 0.0000093559856663739, 0.00007497500186815324, 0.0000093559856663739, 5.6212509051295795e-9, 6.8060100125474815e-15
8.240474063989279e-21, 6.8060100125474815e-15, 1.1327893594730693e-11, 9.077705692513543e-11, 1.1327893594730693e-11, 6.8060100125474815e-15, 8.240474063989279e-21

The no-blur CGI worst case, shows a significant overshoot (53928.6562 / 51199, or 5.33%) in the highlights after upsampling by 128x, but the slightly blurred (IMHO less than actual cameras/lenses wll produce without additional sharpening), is very well behaved (51318.7344 / 51199, or 0.23% overshoot). Anything below 1% over/undershoot can probably be considered as perceptually not significant under normal viewing conditions. It's only the shadow tones undershoot that could be improved a bit, e.g. by raising the gamma from 2.0 to, say, 2.5 (because 3 had you worried about color separation).

So, the worst case scenario would suggest a significant reduction of highlight halos to be needed, while in practice (for real images) it's just fine (with this particular blend), even for the best production cameras/lenses.

I'll do some simulations without blends and different deblurs on both targets later.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 07:10:15 am
Suggestions:
A) Are we really sure that gamma 3 is bad when enlarging? One thing I like about gamma 3 (besides the connection to L*a*b* and perceptual models) is that the C cbrt function can recover easily from negative values without clamping. <- rather pedestrian
B) Instead of using the auto-level luminance as is as weighting function, push it through gamma after conversion to greyscale! We want the weight of linear to grow more slowly than it already does, so something like -gamma .5 (corresponds to Pow 2, or maybe .333333333333333) should work?
P.S. Off the top of my head warning.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 07:18:42 am
Bart:
Well done and points taken.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 07:23:43 am
Suggestions:
A) Are we really sure that gamma 3 is bad when enlarging? One thing I like about gamma 3 (besides the connection to L*a*b* and perceptual models) is that the C cbrt function can recover easily from negative values without clamping. <- rather pedestrian

I personally prefer a slightly higher gamma than the current 2.0 for upsampling. Because I like simple numbers, not really any other reason than that at this time into the testing, a gamma 2.5 / 0.4 roundtrip would seem to produce better Luminance results, even on e.g. the Magick logo and similarly rasterized vector image content.

Quote
B) Instead of using the auto-level luminance as is as weighting function, push it through gamma after conversion to greyscale! We want the weight of linear to grow more slowly than it already does, so something like -gamma .5 (corresponds to Pow 2, or maybe .333333333333333) should work?

Sure, the code is currently easy enough to adapt. So a -gamma 0.5 (or whatever) on the luminance based blending function would be worth a try (with or without -auto-level).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 07:29:10 am
TO DO:
At some point, check if -evaluate Pow 2 gives similar results to -gamma .5.
Most likely, one is done through a LUT (how dense? have not checked recently) and the other through actual function evaluation.
-evaluate Pow is probably more accurate at high/low gamma. And possibly not much slower (memory/computation trade off does not favor LUTs as much as it used to be).
Warning: I don't know if -evaluate carefully matches ranges. Maybe -evaluate Pow requires more plumbing.
In HDRI...
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 08:07:13 am
How about we go back to gamma 3 instead of gamma 2 for upsampling and use that until problems occur?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 08:59:31 am
How about we go back to gamma 3 instead of gamma 2 for upsampling and use that until problems occur?

Fine with me. I'll change the (Windowese with batch variable 'Msize') code line
( -clone 0 -gamma 2 -filter LanczosRadius -distort Resize %Msize% -gamma 0.5 ) ^
into
( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize %Msize% -gamma 0.3333333333333333 ) ^

In Unixese that would probably become (although I'm not sure how variables are formatted in that dialect):
\( -clone 0 -gamma 3 -filter LanczosRadius -distort Resize \%Msize\% -gamma 0.3333333333333333 \) \

If nothing else transpires before the end  of the day, UTC+2 here, I'll upload a new full script version later.

Cheers,
Bart

P.S. I've attached the resulting plots of a sharp edge step, and an ever so slightly blurred one, Gamma 3, upsampled 128x. At least the halos are much more balanced. Highlight overshoot halo has increased a bit (51341.3438 / 51199 or 0.28% which was 0.23% on the blurred target version, but still well below significant), but the main improvement is in the shadow halo reduction. There is a bit more low amplitude ringing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 09:02:07 am
BTW, these plots really make clear that this can be a very effective halo suppression approach.
The other thing is that halo suppression has generally been performed with "anti ring" limiters. They can't give results as smooth as those, at least not as cheaply.
There are two things: Suppress halos. But don't introduce aliasing.
...
The no-blur CGI worst case, shows a significant overshoot (53928.6562 / 51199, or 5.33%) in the highlights after upsampling by 128x, but the slightly blurred (IMHO less than actual cameras/lenses wll produce without additional sharpening), is very well behaved (51318.7344 / 51199, or 0.23% overshoot). Anything below 1% over/undershoot can probably be considered as perceptually not significant under normal viewing conditions. It's only the shadow tones undershoot that could be improved a bit, e.g. by raising the gamma from 2.0 to, say, 2.5 (because 3 had you worried about color separation).

So, the worst case scenario would suggest a significant reduction of highlight halos to be needed, while in practice (for real images) it's just fine (with this particular blend), even for the best production cameras/lenses.

I'll do some simulations without blends and different deblurs on both targets later.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 09:05:40 am
Bart:
Don't worry about conversion back to unixese. Easy enough.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 10:07:54 am
Bart:
The right scale to measure overshoot and undershoot over is the difference between the value on the "high side" and the "low side" (instead of the value on the "high side", or the "low side", separately).
In other words, compare the overshoot to the size of the "jump".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 10:36:56 am
Bart:
The right scale to measure overshoot and undershoot over is the difference between the value on the "high side" and the "low side" (instead of the value on the "high side", or the "low side", separately).
In other words, compare the overshoot to the size of the "jump".

Nicolas, is there such a thing as a right scale?

In my non-academic experience, a reduction in the shadows + an increase in the highlights might (partially) cancel (or increase) each other, which to me could potentially hide the real effect on a perceptual level (the Human Visual System is more sensitive to 'detail' the bright tonal ranges, the highlight ripples at this scale of magnification). The total amplitude also changes with local contrast, so experiments need to be calibrated to the same contrast range, which can be tricky if local shadow and highlight contrast differs. That's why I prefer to differentiate between the two tone levels.

I do understand that a total amplitude approach makes more sense at a denser/higher spatial resolution (smaller magnification scale), because the level of detail is closer to the peak of the Human Contrast Transfer Function (which peaks around 6-8 cycles/degree for Luminance, and more like a 'plateau' from 1 to 6 cycles/degree for chroma). So the 'right scale', varies with viewing distance and magnification factor.

Maybe doing both would satisfy all?

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 11:12:39 am
Bart:
Indeed, I'm asking for a presentation of information that makes most sense in a linear context, when we are dealing with a nonlinear context.
Nonetheless, characterizing an overshoot of 5000 (in 16-bit) when the "flat values" are 15000 and 50000 as 100*5000/(50000-15000) = 14.3% is informative to me, even though it says nothing relative to JND. So if you could call this "relative measure of the overshoot" or something like this, and report it, I'd be thankful. But this is certainly not necessary. My apologies for putting "right" into this.
P.S. Maybe we misunderstood each other. Preferably, I'd like an overshoot to 55000 when the "flat values" are 15000 and 50000 to be reported as 14.3%, and an undershoot to 10000 with the same "flat values" to be reported likewise. But this is a clumsily stated wish, not a "right".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 12:29:57 pm
I would like to suggest a change to your script that sort of addresses the same issues:
Right now, you have variable sharpening for downsampling. Could we have variable sharpening, though -define filter:blur=value, for the "generic" (for upsampling, mostly) luminance weighted gamma blended resampling (as opposed to deconvolution) method?

Let me see If I got the gist of that, all referring to the EWA -distort Resize:
LanczosRadius (the current generic/upsampling filter) is the same as Lanczos and LanczosSharpest 3, they all are 3-lobe support filters (Jinc windowed Jinc types?) that only differ in the amount of (de)blur?

What is LanczosSharp? Does it materially differ from the above scheme?

Another thing, and I know I should go look up the details, but maybe you can get me going faster ahead with a direct response. In what way are the Blur defines special for the above named filters? Are they just optimized for different levels of sharpening, or is there a specific relationship to how the blur radius relates to the lobes (e.g. aligned with orthogonal, or diagonal pixels)?

The reason I'm asking is because I'm thinking of a simple/flexible way to set this up within the limitations of a basic script file. Something like a fine grained lookup such as, in pseudo 'code' , with simple to add intermediate levels:
If 'SharpAmount' is less than or equal to 0, then set a 'BlurVal' variable to '1' and go to 'convert'
If 'SharpAmount' is less than or equal to 64,  then set a 'BlurVal' variable to '0.9264075766146068' and go to 'convert'
If 'SharpAmount' is less than or equal to 100,  then set a 'BlurVal' variable to '0.88549061701764' and go to 'convert'

With some levels in between for finer control.

All converts would then use '-define filter:blur=%BlurVal% -filter Lanczos -distort Resize'

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 12:49:55 pm
Bart:
EWA LanczosSharp 3 can be obtained with
... -filter LanczosSharp -distort Resize ...
or with
... -filter Lanczos -define filter:blur=0.9812505644269356 -distort Resize ...
(Note that there is another LanczosSharp with a slightly different deblur floating around. It's not the ImageMagick one.)
EWA LanczosRadius 3 can be obtained with
... -filter LanczosRadius -distort Resize ...
or with
... -filter Lanczos -define filter:blur=0.9264075766146068 -distort Resize ...
EWA LanczosSharpest 3 can be obtained with
... -filter Lanczos -define filter:blur=0.88549061701764 -distort Resize ...
All of them are Jinc-windowed Jinc 3 lobe, different from each other only in the chosen deblur (the value, smaller than 1, set with -define filter:blur=...). Nothing more, nothing less.
Just like you can "sweep" through Keys splines by setting the b parameter in the range 1 to a smallest usable value which is slightly below 0, this sweep going from very blurry to very sharp, you can sweep through EWA Jinc-windowed Jinc 3-lobe filters going from 1 down to 0.88549061701764 in the blur value.
What I was suggesting is that you use a percent sharpening to set the deblur, as follows:
Let SharpAmount be the parameter, from 0 (little) to 100 (a lot) that controls the sharpening amount. Then,
DEBLUR = 0.88549061701764 + (1 - .01*SharpAmount)*(1-0.88549061701764)
with default SharpAmount set, when using the generic (upsampling) method, to
100*(1-(0.9264075766146068-0.88549061701764)/(1-0.88549061701764))
Passing DEBLUR to -define filter:blur=DEBLUR then makes sharpening "tighten" the radius of the Jinc-windowed Jinc 3-lobe, and the default value reproduces EWA LanczosRadius.
No need for cases.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 01:03:15 pm
To give an idea of how these values are arrived at. (There are other characterizations in terms of operator and matrix norms which I can't track down just now.)
http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=19636&p=89068&hilit=Axiom+code#p89068 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=19636&p=89068&hilit=Axiom+code#p89068)
http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=19636&start=30#p78347 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=19636&start=30#p78347)
EWA Lanczos Radius 3 does not come from optimization. It a pragmatic choice simply obtained by setting the deblur to the ratio of 3 and the location of the third root of the Bessel Jinc function. This fixes things so that the EWA disc used when enlarging is exactly 3, which means it is the largest deblur such that EWA Lanczos 3's "zone of influence" is fully contained inside tensor (orthogonal) Lanczos 3's "zone of influence" (which is the image of the result of "infinite ratio" upsampling of an impulse with the filter, that is, the continuous support of the impulse response).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 01:48:50 pm
Basically, EWA LanczosSharp is obtained with the same optimization principle that gives EWA Robidoux (the two variants of EWA LanczosSharp have to do with two variants of the optimization principle that give the same result in the case of Keys cubic splines), EWA LanczosSharpest is obtained with the same optimization principle that gives EWA RobidouxSharp, and EWA LanczosRadius is obtained through a reasonable but nonetheless ad hoc specification that the radius of the EWA disc should match its number of lobes.
EWA LanczosSharp basically minimizes the no-op modification of an image that has constant columns (or rows). Same with Robidoux.
EWA LanczosSharpest basically minimizes the no-op modification of an arbitrary image. Same with RobidouxSharp.
Optimization is done in a minimax sense (minimize the worst case). It's actually "the other" EWA LanczosSharp that is the minimizer in this sense. The ImageMagick EWA LanczosSharp satisfies a sligthly different criterion: A vertical (or horizontal) line (with constant pixel value) does not modify its immediate neighbour under no-op. (It modifies the neighbour of its neighbour, but very little.) Robidoux satisfies both criterions.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 29, 2014, 03:21:00 pm
Bart:
Just like I've mentioned before that I am not sure that the Keys cubics RobidouxSoft, Robidoux and RobidouxSharp are absolutely the best one can do, among Keys cubics, or more generally among BC-splines, I am not sure that the EWA Jinc-windowed Jinc 3 lobe methods LanczosSharpest, LanczosRadius, and LanczosSharp (either of the two variants) are necessarily the best among all deblurred EWA Jinc-windowed Jinc 3-lobe methods.
They seem to produce very good results. But I'm not sure they are necessarily the best. (The optimized ones are the best w.r.t. some criterions, but being the fastest 100m runner does not make you a good cook.)
-----
By letting users choose the amount of deblur (in the guise of sharpening), we can learn something. And we give them flexibility.
I am pretty sure that the criterion used to optimize EWA LanczosSharpest (.88...) implies that one should not deblur more than that, and I see no reason to go more blurry than "classical" EWA Lanczos (blur = 1).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 29, 2014, 06:35:46 pm
Nicolas, I can't find out how to calculate an expression in ImageMagick. Do you have any idea?

I've tried all sorts of things, also with variations on -fx, like
SET Deblur=(convert -format "%%[fx:1-3290*%Deblur%/2873127]" info:)
, but in-line evaluation of e.g.
-define filter:blur=(1-3290*%Deblur%/2873127) -filter Lanczos
is what I'm looking for.

EDIT: I have got it calculating a result (the Blur parameter) on the command line console with the following code:
>SET Deblur=100

>SET Deblur=convert xc: -format '%[fx:1-3290*%Deblur%/2873127]' info:

>%deblur%
'0.885491'

But I'm probably doing something wrong (some silly Escape character requirement or such) to get it doing the right thing when the variable is used in the convert script section .... Also the precision is a bit low, but that is probably a -format issue that needs some work.

Maybe something like '%[option: ...]' will allow to directly set an expert option like -define , don't know yet.

Suggestions are (still) welcome.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 02:48:32 am
Bart:
You are producing a string that contains the numerical value you want in decimal representation, which is not the same as a number.

-----

Question:
My understanding is that Fred Weinhaus, in his scripts, always has this kind of numerical preprocessing done by the shell (the scripting language), not by ImageMagick itself.
Are you able to have your scripting language do basic arithmetic and push the result over to IM?

-----

I'll post something on the Users Forum. This, really, is more of a scripting language question than an IM one, I think, but hopefully someone will know.

P.S http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 03:39:29 am
Bart:
Unrelated suggestion (untested): I think that if you put the -delete 0 before the previous pair of parenthesis (so that it is just after the parenthesis in which there is the last reference to list image 0) you will reduce the memory footprint of the script, and probably make it faster. That is, use
...
-delete 0 ^
( -clone 2 -colorspace gray -auto-level ) ^
...
Make sure this does not change the results first. I'm making assumptions about IM list syntax and internals.
-----
I also think that a skilled IM command line programmer would know how to avoid to clone a second time and then deleting, but I don't know off the top of my head. Maybe just skip the last -clone 0 and then the -delete 0? Or the first one?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 04:17:36 am
Bart:
See http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006#p113878 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006#p113878). Please thank Alan Gibson (a.k.a. snibgo, so putting a link to im.snibgo.com would be nice, just like giving a link to Fred Weinhaus' scripts page) in your script's header. He's helped twice now (also helped with AdobeRGB).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 30, 2014, 04:18:28 am
Bart:
You are producing a string that contains the numerical value you want in decimal representation, which is not the same as a number.

Yes, probably something like that won't work. I'm also exploring the option to set one's own global option, like:
-set option:mydeblur '%%[fx:1-3290*%Deblur%/2873127]'
after which one should be able to use it as
'%%[mydeblur]'

The double %% are supposed to be a Windows batch escaped single %.

Quote
Question:
My understanding is that Fred Weinhaus, in his scripts, always has this kind of numerical preprocessing done by the shell (the scripting language), not by ImageMagick itself.
Are you able to have your scripting language do basic arithmetic and push the result over to IM?

For some reason it doesn't work yet, which is strange because a batch variable like %deblur% is correctly parsed. So probably something to do with text versus numerical.

Quote
I'll post something on the Users Forum. This, really, is more of a scripting language question than an IM one, I think, but hopefully someone will know.

Maybe, but I do see code examples that do exactly what I'm trying, it only doesn't work in a batch script (assuming correct porting to windowese). For example this should correctly feed a calculation of a pixel value to a label (in unixese command line code):
convert rose:  -set option:mylabel '%[pixel:u.p{12,26}]' -delete 0 \
          label:'%[mylabel]'    label_fx_indirect.gif

Quote
P.S http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006)

I'll be watching that as well. Thanks for thinking along, I really like the suggestion for a variable blur, so I'd like to get it working, either via script precalculation or (preferably) an in-line "Percent escape" convert calculation.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 30, 2014, 05:08:34 am
Bart:
See http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006#p113878 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=26006#p113878). Please thank Alan Gibson (a.k.a. snibgo, so putting a link to im.snibgo.com would be nice, just like giving a link to Fred Weinhaus' scripts page) in your script's header. He's helped twice now.

Nicolas (and Alan) thanks. That works like a charm!

EDIT: This issue was solved and corrected (by adding "-precision 16" without the quotes) in the re-updated Version 1.2.0.
The only concern I have is that the precision is limited to only a few significant digits. This is what an amount of 100 is parsed to when it is finally used in the convert command:
-define filter:blur=0.885491 -filter Lanczos

Maybe something inside the convert command will use a higher precision than what the batch precision allows ...


The current corrected code parses as:
-define filter:blur=0.88549061701764
as intended.

I've added Alan Gibson to the credits, and will upload the new script version (1.2.0) shortly (just need to do some more code clean-up, an have a coffee).

BTW. As you may have noticed, I've modified the formula to a shorter version (1-3290*%Deblur%/2873127) that should give exactly the same result. I just prefer 'Rational number' calculations to preserve precision in floating-point environments as long as possible and delay going into floating-point, because 'Real number' decimal fractions will internally not be exactly the same as typed (depends on machine precision). This also often allows to substantially shorten formulas.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 05:16:11 am
...
BTW. As you may have noticed, I've modified the formula to a shorter version (1-3290*%Deblur%/2873127) that should give exactly the same result.
...
It's done in floating point whether you like it or not from the get go. (And, actually, this is a good thing, B/C 199/200 = 1 in integer arithmetic.)
Please use
1-0.0011450938298236*%Deblur%
Irrelevantly faster and probably more accurate. (I've not checked how the formulas are parsed by IM.)
P.S. Hair splitting apologies. Feel free to ignore.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 30, 2014, 06:19:34 am
New version (1.2.0) attached.

Added was the possibility for users to also adjust the amount of sharpening when upsampling (currently based on EWA Lanczos filtered resampling and a Gaussian deblur). Negative amounts will blur the image, positive amounts will sharpen.

Changed was the blending gamma (was 2.0, now it's 3.0) for better reduction of dark edge undershoots.

Alan Gibson's name was added to the credits for code optimization. Thank you Alan!

Some minor tweaks to clean up the code were done as well, but there are more significant fundamental changes/improvements that have yet to be made to further improve the output quality even further. It's a work in progress!

Cheers,
Bart

P.S. With only 1 download, I've updated Version 1.2.0 without adding a new version number. The internal calculation of the Deblur value was increased to full 16-bit significant digit precision.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 09:02:06 am
Bart:
Two quick comments, that may or may not be correct:
1) I think that the DoG parameter should be the same for both linear light and gamma light. The reason is that, roughly, we want the key "crossings" to match so that the heuristic that suggests picking one or the other based on luminance have a slightly stronger footing. I understand that in principle "crossings" depend on the colorspace. But not much, I think.
2) Maybe suggesting that normal sharpening is between 50 and 100 across the board is a good thing, since I suspect that the very best all purpose EWA Lanczos may actually be a bit softer than LanczosRadius, and a bit sharper than LanczosSharp.
P.S. Maybe this was a bit opaque. Basically, I want the halos produced in linear light and gamma to be aligned as much as possible so that the selection process implied by the luminance weighted blending does the right thing most of the time.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on July 30, 2014, 10:08:17 am
Hi,

Bart: Thank you very much for your effort.
I have started experimenting with it, just downloaded the last version (1.2.0)

Started with an png 16 bit file with embedded sRGB
Downsample, specifying x800; yes for optimized and 50 for sharpening

Results in two warnings:

1) "environmental variable deblurAmount not set"

I checked the script and it looks that it is missing an "=" in the finish part, so I added it and now it does not show the warning (apparently that variable is not used when selecting optimized down-sampling method

Quote
:finish
REM Clear environment variables
SET deblurAmount=

2) Even if the original file has sRGB embedded, I receive the following warning, which does not prevent the program to complete. The resulting file does not have a profile but opens correctly assigning sRGB in PS.

Quote
convert.exe: no color profile is available `C:\Users\Francisco\Pictures\Test res
ize\IMsource.icc' @ error/meta.c/WriteMETAImage/2326.
convert.exe: UnableToTransformColorspace `C:\Users\Francisco\Pictures\Test resiz
e\IMsource.icc' @ warning/profile.c/LCMSExceptionHandler/459.
convert.exe: ColorspaceColorProfileMismatch `icc' @ error/profile.c/ProfileImage
/578.

I don't know if I'm doing something wrong.

Regards

P.S. still too early for me to comment about the results, I need to play with it more.

Regards
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 30, 2014, 10:28:08 am
Bart:
Two quick comments, that may or may not be correct:
1) I think that the DoG parameter should be the same for both linear light and gamma light. The reason is that, roughly, we want the key "crossings" to match so that the heuristic that suggests picking one or the other based on luminance have a slightly stronger footing. I understand that in principle "crossings" depend on the colorspace. But not much, I think.

Nicolas, I d have to have a look at that. The reason that they are currently different is that while the gamma is still applied, the slope of an edge gradient has also changed, also in the mid-tonal region where the current blend will later cross when blending in linear light. That would suggest that also the detail that was spread over the neighboring pixels has a changed relative weight, and would require more/or less weight to deconvolve.

But I will review that again later anyway, because now that we have established a workable solution to employ user variable parameters, I might tweak the deconvolution radii to be a bit more adaptive for images that were resized to 50-100% of their original size.

It may also be interesting to be able and occasionally use the deconvolution for upsampling by selecting the down-sampling optimized filters with deconvolution. Currently the radius of the DoGs is fixed to a 7x7 support with a fixed High-pass filter radius that is used for deconvolution. That makes it under-dimensioned for enlargements, but boosting the amount can still produce interesting results I remember that the Jigsaw piece looked rather clean when I tried it a few versions ago, but the algorithms have change in the mean time so I'd have to do that again to see what it looks like now. With a more appropriate support and radius it would be more effective, but also slower. As far as I remember, the (de)blur function just mis-matches the number of lobes and produces less or more halo as a compensating mechanism, unlike properly dimensioned deconvolution (probably better done in the frequency domain, so IM7, due to the larger radii and kernel supports needed).

Quote
2) Maybe suggesting that normal sharpening is between 50 and 100 across the board is a good thing, since I suspect that the best EWA Lanczos may actually be a bit softer than LanczosRadius, and a bit sharper than LanczosSharp.

Done, will show up being changed in the next update. I just put 64 there as a memory jogger to signal the approximate correlation with one of the 'named' filters. There's no absolute need to keep that connection, it's more important how the output looks, whatever setting is needed for the particular image content.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 10:29:37 am
Bart: See the P.S. to my last post.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 30, 2014, 10:50:14 am
Hi,

Bart: Thank you very much for your effort.
I have started experimenting with it, just downloaded the last version (1.2.0)

Started with an png 16 bit file with embedded sRGB
Downsample, specifying x800; yes for optimized and 50 for sharpening

Results in two warnings:

1) "environmental variable deblurAmount not set"

I checked the script and it looks that it is missing an "=" in the finish part, so I added it and now it does not show the warning (apparently that variable is not used when selecting optimized down-sampling method

Hi Frank,

Thanks for the good bug analysis, there was indeed an '=' missing. Not that it really mattered (all resampling was already done at that point), it seems like these variables are being discarded from the environment anyway since the script is ending all processing at that point in time, but it was something I missed. I've just corrected that for the next update. I did want to leave that bit of housekeeping cleanup in place, as a reminder, and maybe for future subroutine calls.

Quote
2) Even if the original file has sRGB embedded, I receive the following warning, which does not prevent the program to complete. The resulting file does not have a profile but opens correctly assigning sRGB in PS.

I don't know if I'm doing something wrong.

I don't think it's something you/we can do a lot about, it has to do with how ImageMagick deals with assigning color Profiles to PNGs, which were originally without profile capability, or with an sRGB profile that's not standard. I could capture the attempt to reassign the original profile to the PNG (or assign an sRGB if none is present), but maybe I can use an available switch that triggers IM to just do as told. The warning is just that, a warning that something didn't go as instructed, and it only happens because I try to re-assign the original profile. Having to do it by hand is annoying though, so I'll review the details (I had a switch in one of the earlier versions, which I removed, that might do it).

Thanks for pointing these issues out, luckily minor ones, but they still went to the todo list.

Quote
P.S. still too early for me to comment about the results, I need to play with it more.

Please do. Likes as well as dislikes, they are all useful feedback. I haven't posted any direct comparisons with e.g. Lightroom or Photoshop myself yet, because the tool's output quality is still improving, but it might be nice do post an occasional interim comparison.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 11:54:50 am
I believe that if you enlarge more than a little, EWA BC-splines lose to EWA Lanczoses (3-lobes by default) unless you really really don't want a second halo.
I would love to be proven wrong. (EWA with a soft Keys (c small) + deconvolution?)
P.S. I also believe that in general one is better off resampling with a suitably sharp filter so that one not have to sharpen much after that. But I would not be surprised if actually it is better to push things through a strongly anti-aliasing filter first, then restoring perceptual sharpness.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on July 30, 2014, 01:11:55 pm
I believe that if you enlarge more than a little, EWA BC-splines lose to EWA Lanczoses (3-lobes by default) unless you really really don't want a second halo.
I would love to be proven wrong. (EWA with a soft Keys (c small) + deconvolution?)
P.S. I also believe that in general one is better off resampling with a suitably sharp filter so that one not have to sharpen much after that. But I would be surprised if actually it is better to push things through a strongly anti-aliasing filter first, then restoring perceptual sharpness.

I do not have all the answers either. I do know that sharpening will usually also sharpen or (with deconvolution) even restore artifacts if enough evidence is left, so having few artifacts does help either way. However, there are other ways of dealing with artifacts like over/under-shoots than blurring or filtering.

That's why I was investigating blending of Clipping prevention layer differences. That would allow to use a relatively sharp filter, but adaptively dampen the halos especially those that come close to the clipping boundaries when a further contrast boost is not really needed.

These kind folks (http://pixinsight.com/doc/docs/InterpolationAlgorithms/InterpolationAlgorithms.html) also use (amongst other choices) Lanczos 3 based resampling, but with interpolation clamping.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on July 30, 2014, 01:59:34 pm
Opinion: Although a number of people have had a somewhat successful time using limiters to minimize undershoots and overshoots (including me with the NoHalo interpolation method), I believe that it is extremely difficult to put together a strong enough limiter that does not introduce aliasing.
I have little interest in following this direction at this point.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 01:29:48 am
Bart:
Opinion: Sooner or later there will be a need for versions that take in Adobe RGB and deal with it correctly (one that keeps things in Adobe RGB, others that convert to sRGB), and versions that produce 8 bit RGB (or Adobe RGB), it this tool is truly meant for "web publication".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 01, 2014, 03:23:25 am
Bart:
Opinion: Sooner or later there will be a need for versions that take in Adobe RGB and deal with it correctly (one that keeps things in Adobe RGB, others that convert to sRGB), and versions that produce 8 bit RGB (or Adobe RGB), it this tool is truly meant for "web publication".

Fully agree, although I have yet to test how much improvement this will make compared to a 'round-trip' in 'assumed sRGB', where the original profile is reassigned to the image afterwards. After all, we are creating loads of totally new pixels, but they are (mostly, with some damage control) interpolated, and should stay close to the original real data. But you are correct that a fully color-managed (assuming there is an embedded profile in the image ) processing is preferred.

It is high on my ToDo list. Currently I'm not too happy with how PNG embedded profiles are treated by IM, JPG/JPEG seems okay, as is TIFF although they may still generate TAG warnings (probably to do with the version of the LIBTIFF library). I saw that Alan Gibson (snigbo) also has color management on his long ToDo list, perhaps because it's not all that simple to do (or just less urgent for him), given all possible situations one can encounter.

In photographic everyday practice we can be confronted with all sorts of profiles, such as device independent profiles (e.g. sRGB, ProPhoto RGB, Beta RGB), and device dependent profiles (output modality profiles, e.g. display, printer/paper), and even missing or incorrect profiles. And different operating systems may store the installed versions at different locations, as do some applications in yet other locations.

Currently, I save the originally embedded profile (if embedded) and reassign it afterwards, but some user interaction would be nice, as would the optional conversion to a different profile be. It will create a much larger script file though, due to the various scenarios one may need to tackle, but we/I could start with the basics.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 01, 2014, 04:04:03 am
I have attached an updated version (1.2.1) .

Some code cleanup was done. The user prompts were somewhat re-worded, in an attempt to better reflect the fact that both types (Up/Down) of resampling are optimized with user adjustable sharpening. I also tried to suppress some non-fatal warning messages that IM would generate, but which have not influence on the result. Also the User defaults are identified a little better (I hope) in most prompts.

The more important addition is the option to use user definable deconvolution for all methods. When the user defines a sharpening amount of '0' or less (which essentially blurs the image), the option to add deconvolution sharpening is automatically presented after the resampled result is produced and saved. This allows to grab a copy of the file before additional sharpening, while the script waits for user input.

I'm trying to improve the human interface as to which values one should enter for the 'best' results (perhaps by supplying calculated defaults), but for the moment one could start with the resampling factor as radius, and 50 or more for the amount. So a 3x (300%) upsampled image would probably best use a Blur radius input of 3 (will be converted to the actual DoG value to use). When boosting the amount above 50, some halo may become visible (which would not necessarily hurt the image if printed), so then a smaller radius would also reduce that while still attempting to sharpen. Larger amounts than 50 are probably needed for very large upsampling anyway, and it's subject matter dependent, so some experimentation is in order. For downsampling a radius setting of 1 to 2  gives good results, and the amount will control the level of crunchiness.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 04:04:20 am
Bart:
(Possibly mistaken) opinion:
For incoming images that are Adobe RGB it is good enough to "import" with the gamma trick and export back with the gamma trick, attaching whatever profiles things came with.
For incoming images that are sRGB, good enough to use -set colorspace sRGB -colorspace RGB ... -colorspace sRGB.
Profiles matter more when you create Adobe RGB or sRGB from a larger color space than once the damage is done.
I care more about smoothness of the transformations, and I think the above two ways are "smoother".
I'd love to find out if I'm wrong.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 04:07:31 am
If I was to do a full job, I'd actually let nip2 do the color management, having it call its own private copy of ImageMagick. This works on all platforms. nip2, which can be called in batch mode even when the workspace has been created interactively, has a really powerful, accurate and clear handling of colorspace management. Originally designed by museum image processing experts. They care. It chews up monster images without breaking a sweat.
P.S. I'm not saying that ImageMagick can't be used to produce a fully color managed tool. I just happen to know nip2 better in this context.
P.S. For Adobe RGB and sRGB, I think the above ImageMagick tricks are enough unless possibly if one changes the bit depth of the output compared to the input. Probably fine too.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 12:26:13 pm
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25629&start=15#p113972 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25629&start=15#p113972)
P.S. It's looking more and more like Q32 or HDRI may be required.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 01, 2014, 01:32:10 pm
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25629&start=15#p113972 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=25629&start=15#p113972)
P.S. It's looking more and more like Q32 or HDRI may be required.

I notice that OP used:
convert infile.jpg -colorspace RGB
instead of
convert infile.jpg -set colorspace sRGB -colorspace RGB
Maybe that has something to do with his results?

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 01:47:42 pm
TTBOMK JPEGs are assumed to be sRGB in ImageMagick. Not pngs.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 02:16:27 pm
What may make a difference is -depth 16 from the get go?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 01, 2014, 02:30:06 pm
TTBOMK JPEGs are assumed to be sRGB in ImageMagick. Not pngs.

Assumptions are dangerous, it's better to define (-set ). ImageMagick has changed behavior a few versions ago which e.g. also influences how Grayscale images are interpreted (as linear gamma by default), if I'm not mistaken, and IM tries to simplify files to their lowest number of channels if RGB gray. Dangerous stuff, easy to lose track, which I also may have.

Changing to at least 16-bit/ch is mandatory for any attempt to survive serious gamma changes. Both of these, '-set colorspace sRGB' and forcing to 16-bit/channel with '-depth 16', is what we do in our resampling script at hand. In addition there is an attempt to avoid halos (and a risk of clipping) through blending. As I have demonstrated with the slightly (~0.39) blurred input, it also makes a difference how well behaved instead of synthetic the input is.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 01, 2014, 02:51:09 pm
Bart:
Have you had a chance to check if -evaluate Pow .3333333333333333 gives a similar result to -gamma 3 and vice versa?
P.S. My sincere apologies for not having the time to look in the source code and performing experiments myself.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 01, 2014, 03:08:44 pm
Bart:
Have you had a chance to check if -evaluate Pow .3333333333333333 gives a similar result to -gamma 3 and vice versa?
P.S. My sincere apologies for not having the time to look in the source code and performing experiments myself.

Not checked yet, I have been spending some time checking the differences between the current convolution with a DoG, and an
-unsharp 0x%blurSigma%+%unsharpAmount%+0
operation, and they produce 'almost'(!) the same results. The simpler code may be tempting, and maybe faster, but it also robs us of another control possibility (not simply deblurring, but targeting certain (edge) spatial frequencies).

Have also been investigating Profile conversions instead of colorspace assignments/conversions, to bypass the sRGB linear part of the gamma curve. Also a direct gamma conversion without respecting colorspace or profile would be possible, but may have unintended effects. Lots of things to verify..., but I'll eventually have a look at Pow versus Gamma.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 02, 2014, 01:57:03 am
There is a new ImageMagick 7 beta for Windows: http://www.imagemagick.org/download/beta/ImageMagick-7.0.0-0~beta20140731-windows.zip (http://www.imagemagick.org/download/beta/ImageMagick-7.0.0-0~beta20140731-windows.zip).
IM7 is HDRI by default (which does not mean that it does not use LUTs internally; for some operations it's probably no more precise).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 02, 2014, 03:09:06 am
There is a new ImageMagick 7 beta for Windows: http://www.imagemagick.org/download/beta/ImageMagick-7.0.0-0~beta20140731-windows.zip (http://www.imagemagick.org/download/beta/ImageMagick-7.0.0-0~beta20140731-windows.zip).
IM7 is HDRI by default (which does not mean that it does not use LUTs internally; for some operations it's probably no more precise).

Nicolas:
Thanks for the link. However, I don't think we should fall into the trap of getting sloppy with procedures "because floating point will be our safety net to prevent clipping while processing". Processing should be so robust that e.g. clipping errors are caught or prevented, also in a 16-b/ch processing. Only in rare, e.g. integer round-off situations (think about FFT divide by near zero deconvolution), should 32-bit/ch or floating point allow the required additional precision. Again, not as a safety net for sloppy procedures, but for when no other solution offers the absolutely required precision.

We should also note that it's still a Beta release, so there may be more bugs than in a stable release. So some options may change behavior, or parameters may have different meaning between releases, and there may be bugs. I hate to waste time searching for my own errors when there are none.

Having said that, I'm looking out for the stable IM7 releases, even though it might mean having to rewrite some of the scripted commands. For the moment I'll stick to optimizing the script for the stable IM6 environment, and have an occasional look at IM7 to see if we can prepare for that by maybe doing things a little different. I'm not going to maintain 2 versions, so when the switch is made, that's it. Backwards compatibility may play some role but will not drain my resources. I've done enough programming to know that that is to be avoided.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 02, 2014, 03:19:12 am
IM6 will be maintained and most likely developed "forever".
I suspect that the IM7 effort may have diminished given that Anthony Thyssen is not as involved in ImageMagick as he once was.
You should not worry about being "trapped" in IM6.
-----
Let's hope that -gamma or Pow, with intermediate storage in 16 bit, is accurate enough. Let's face it, ICC profiles are not that particularly precise themselves, esp. the older (pre sRGB v2, for example) ones.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 02, 2014, 06:39:39 am
Not checked yet, I have been spending some time checking the differences between the current convolution with a DoG, and an
-unsharp 0x%blurSigma%+%unsharpAmount%+0
operation, and they produce 'almost'(!) the same results. The simpler code may be tempting, and maybe faster, but it also robs us of another control possibility (not simply deblurring, but targeting certain (edge) spatial frequencies).
Good idea to check whether the complication brings benefit. Thank you.
Have also been investigating Profile conversions instead of colorspace assignments/conversions, to bypass the sRGB linear part of the gamma curve.
As far as sRGB goes, I trust the IM conversions performed by -colorspace sRGB and -colorspace RGB (with proper -set colorspace).
Also a direct gamma conversion without respecting colorspace or profile would be possible, but may have unintended effects. Lots of things to verify..., but I'll eventually have a look at Pow versus Gamma.
I am really really distrustful of conversions to gamma spaces without going through a linear light space (unless we already are in a gamma space, like Adobe RGB, or the profile has a mode that allows direct conversion to a gamma space, which I doubt). I would not even bother trying.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 03, 2014, 05:45:32 am
Bart:
This is with really quick testing, but the "generic" scheme that corresponds to sharpening at 50% (with a deblur that falls halfway between EWA LanczosSharpest 3 and Lanczos 3, which happens to be about halfway between LanczosRadius 3 and LanczosSharp 3) looks quite good to me when used to enlarge.
Haloing is mild, the result is fairly sharp, it's not too aliased, and it actually sort of looks like an image taken slightly out of focus, which is pretty good for an enlargement. (There definitely are things to argue here...)
The usual artifacts are all there, but none of them jumps at you. Good in a not bad way, in other words.
Let's see if this opinion holds up with further testing etc.
-----
P.S. I've also tried the scheme that corresponds to maximal sharpening/deblur, a.k.a. LanczosSharpest 3, and given how sharp it is, it produces quite good looking results. It corresponds to
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
 \( -clone 0 -gamma 3 -define filter:lobes=4 -define filter:blur=0.88549061701764 -filter Lanczos -distort Resize 6400% -gamma 0.3333333333333333 \) \
 \( -clone 0 -define filter:lobes=4 -define filter:blur=0.88549061701764 -filter Lanczos -distort Resize 6400% \) \
    -delete 0 \
 \( -clone 1 -colorspace gray -auto-level \) \
 -compose over -composite \
 -set colorspace RGB -colorspace sRGB \
output.png
(I've moved the -delete to, hopefully, free memory halfway through the pipeline. The syntax being that of a stack, what used to be -clone 2 is now -clone 1.)
Here are the results of a contrived test, which nonetheless I find quite telling, a 64x enlargement. The original is at the bottom (8x8 image).
P.S. I made a mistake and what is shown above is almost EWA LanczosSharpest 4 instead of EWA LanczosSharpest 3. See the following posts.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 03, 2014, 01:30:30 pm
Bart:
This is with really quick testing, but the "generic" scheme that corresponds to sharpening at 50% (with a deblur that falls halfway between EWA LanczosSharpest 3 and Lanczos 3, which happens to be about halfway between LanczosRadius 3 and LanczosSharp 3) looks quite good to me when used to enlarge.

Yes, in general this looks like a good compromise. If you want, I can change the default sharpening from 0 to 50, just let me know if the setting keeps looking pleasing on different images. Others are invited to judge their images and share their preferred settings for upsampling, but also if there are things they do not like about their upsampled results.
 
Quote
Haloing is mild, the result is fairly sharp, it's not too aliased, and it actually sort of looks like an image taken slightly out of focus, which is pretty good for an enlargement. (There definitely are things to argue here...)
The usual artifacts are all there, but none of them jumps at you. Good in a not bad way, in other words.
Let's see if this opinion holds up with further testing etc.

Yes, some more testing with different image content will need to be done.

Quote
P.S. I've also tried the scheme that corresponds to maximal sharpening/deblur, a.k.a. LanczosSharpest 3, and given how sharp it is, it produces quite good looking results.

Maybe we can consider where both alternatives excel, and device a gradual transition between the two, or simply offer a choice between two methods.

Quote
(I've moved the -delete to, hopefully, free memory halfway through the pipeline. The syntax being that of a stack, what used to be -clone 2 is now -clone 1.)

It won't hurt and it may help, it's up to the memory management of IM and the OS I guess, whether it immediately frees memory. As far as the image content is concerned it produces identical results, so I'll implement it for the next update.

Quote
Here are the results of a contrived test, which nonetheless I find quite telling, a 64x enlargement. The original is at the bottom (8x8 image).

Yes, it's not bad, but what we cannot see is how much the first halo ring over/undershoots the goal value (which is already at the clipping level to start with). The first lobe of a 4 lobe support has a higher amplitude, and the remainder has lower amplitudes. I assume that a reduced contrast checker will be more revealing for continuous tone images, but for line drawings this does give a useful impression.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 03, 2014, 01:35:18 pm
Bart:
Given that the two schemes I just tested correspond, respectively, to deblur=50 in the case of the "halfway scheme" and 100 for LanczosSharpest 3, and that I know that deblur=0, namely plain EWA Lanczos, works well with JPEG compression, offering the range [0,100] with default at 50 would seem like a pretty good thing.
Cheers
P.S. In case it was not clear: All the methods I've discussed very recently are 3-lobe EWA Jinc-windowed Jinc. No 4-lobe here. I'll be really clear if I bring back EWA LanczosSharpest 4.
As you may guess, there is a LanczosSharp at every number of lobes, a LanczosSharpest at every number of lobes, etc. But they are not implemented like this in ImageMagick: The deblur is fixed, instead of depending on the number of lobes like it does with EWA LanczosRadius. Programming expediency.
P.S. Bart: My apologies. Indeed I tested the four lobe LanczosSharpest. Not quite, actually, since the deblur is the one for 3 lobes. It's right in the code. :(
Here is the result of
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
 \( -clone 0 -gamma 3 -define filter:blur=0.88549061701764 -filter Lanczos -distort Resize 6400% -gamma 0.3333333333333333 \) \
 \( -clone 0 -define filter:blur=0.88549061701764 -filter Lanczos -distort Resize 6400% \) \
    -delete 0 \
 \( -clone 1 -colorspace gray -auto-level \) \
 -compose over -composite \
 -set colorspace RGB -colorspace sRGB \
output.png
which is what I meant to do and which, this time, is achievable with your script.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 03, 2014, 06:21:30 pm
This is "the other end", namely sharpening=0 in the "generic" scheme:
convert \( input.png -depth 16 -set colorspace sRGB -colorspace RGB \) \
 \( -clone 0 -gamma 3 -filter Lanczos -distort Resize 6400% -gamma 0.3333333333333333 \) \
 \( -clone 0 -filter Lanczos -distort Resize 6400% \) \
 -delete 0 \
 \( -clone 1 -colorspace gray -auto-level \) \
 -compose over -composite \
 -set colorspace RGB -colorspace sRGB \
output.png
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 08, 2014, 03:48:27 am
Bart:
I am starting to suspect that going through gamma 2 instead of gamma 3 may strike a better balance between "faithfulness" and halo reduction.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 08, 2014, 05:01:24 am
Bart:
I am starting to suspect that going through gamma 2 instead of gamma 3 may strike a better balance between "faithfulness" and halo reduction.

Hi Nicolas,

Perhaps, although the dark side undershoots will become more prominent. We could also go for an intermediate Gamma 2.5/0.4 (slightly brighter than the presumed original gamma 2.2 precompensation), and see how pleasing the results from that are. I suppose it also depends on image content. But maybe you have a specific example that makes you wonder if 3 is too much.

Line-art (which lends itself better for vectorization than raster interpolation) would require different settings (or maybe an ever so slight preblur) than continuous tone images. And the latter would require tuning to the degree of sharpening that the image already received. As my earlier example of the 128x upsampled ramp showed, a very small radius Gaussian preblur (maybe even with an reduced amount) totally changes the over/undershoot behavior of a given method/filter.

I'll try to squeeze in an evaluation (precision and timing) of  '-evaluate Pow' versus '-gamma', later today. I think I've found a useful command execution timer batch file to do the execution time part of that evaluation.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 08, 2014, 05:04:58 am
Quick suggestion: Don't time subcomponents in isolation. So much time is spent in memory management in typical systems that you should time the entire command, including read and save to file. If you can't see a difference, you have your answer. (Of course, you need to try to have the disk be in a similar state, or take the minimum time over a reasonable number of runs (5 is probably enough), which is less of a hassle than trying to prevent other processes to access the disk or setting the disk to a fixed state.)
If I was timing things in Unix, I'd just put time before the command and call it a day.
P.S. That was a bit terse. What I really mean is this: If an instruction, in isolation, runs faster, but its insertion in a toolchain somehow interferes with the smooth flow of data through the pipeline, the fact that it runs faster is irrelevant. I/O and memory management dominate computation time, almost always. Timing individual links is less informative than the whole.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 08, 2014, 05:10:32 am
The "wondering" comes from that, with pixel art, I'd like black and white features to be perceptually thickened/thinned out about the same amount. There is just a tiny bit too much of a difference, that I can see, with gamma 3. (On the other hand, I've not calibrated my MacBook's screen, so who knows, really?)
Also, I totally agree that if you don't get this close to Nyquist, it may be better to stick to gamma 3.
Basically, I am wondering if >>you<< can see a difference, leaving aside the amount of haloing for a minute, just focusing on the handling of thin features and sharp boundaries.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 08, 2014, 06:37:09 am
Basically, I am wondering if >>you<< can see a difference, leaving aside the amount of haloing for a minute, just focusing on the handling of thin features and sharp boundaries.

Yes, I see it on the extreme checker example, but less so on continuous tone images, but then we're talking about huge upsampling factors on 'impossible' edge contrast. We're also 'exploiting' the white clipping which hides the first lobe halo there, and some of the first dark lobe halo as well. The clipping can in such a case be used to our benefit on bi-tonal images, by cranking up the contrast of the original, and if necessary reduce it again afterwards.

I also see the differences in the Chip-crop image, mostly on the highlight edge which is smoother with a higher gamma.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 08, 2014, 06:49:27 am
The chip test image (that I've used because others have) has been pushed some rather drastic processing, I am fairly sure. The ways the colours mix and vary appears to have little connection to what one would expect from an unmanipulated digital photograph. Maybe I am wrong on this, but something that can only be seen on the "chip" test image I would tend to dismiss.
It's a fun image to enlarge with, but definitely not a litmus test.
(Same with Lena, mandrill, the photographer, etc. Nothing to do with what digital photographs are about now.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 08, 2014, 06:52:14 am
OK: Let's stick to gamma 3.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 08, 2014, 08:01:25 am
Quick suggestion: Don't time subcomponents in isolation. So much time is spent in memory management in typical systems that you should time the entire command, including read and save to file. If you can't see a difference, you have your answer. (Of course, you need to try to have the disk be in a similar state, or take the minimum time over a reasonable number of runs (5 is probably enough), which is less of a hassle than trying to prevent other processes to access the disk or setting the disk to a fixed state.)
If I was timing things in Unix, I'd just put time before the command and call it a day.
P.S. That was a bit terse. What I really mean is this: If an instruction, in isolation, runs faster, but its insertion in a toolchain somehow interferes with the smooth flow of data through the pipeline, the fact that it runs faster is irrelevant. I/O and memory management dominate computation time, almost always. Timing individual links is less informative than the whole.

Understood, and that was what I intended to do; a full upsample (16-b/ch RGB image) only changing the -gamma 'x' to -evaluate '1/x' and back.

Well, the result is in (Q16 version of ImageMagick): with '-evaluate' it takes some 4-5% longer to execute a full upsample process to 800% than with '-gamma', and the image data results are identical (zero difference). I've tried multiple runs and averaged, and small 0.1sec differences were seen but nothing all that significant between runs, and -evaluate was consistently 'slower'.

Of course it's only a sample of one specific 64-bit computer running on Win 7, but based on this I'll stick to '-gamma'.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 08, 2014, 08:06:01 am
Case closed.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 09, 2014, 02:12:11 pm
If I am not mistaken, the Unix version of the downsampling scheme with maximum (=default) sharpening is basically
convert \( input.jpg -set colorspace sRGB -colorspace RGB \
  -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 512x512 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
  -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -define convolve:scale=100^,100% \
  -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  -delete 0 \
  \( -clone 1 -colorspace gray -auto-level \) \
  -compose over -composite \
  -set colorspace RGB -colorspace sRGB \
 output.png
P.S. Here is what it gives with the (rather unforgiving) fly http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg (http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg).
P.S. 2 I also quite like the result with half the amount of sharpening (50^,100% instead of 100^,100%). Attached below as well. (Bart: You did well! The "generic" scheme is no match when downsampling.)
P.S. 3 Looked more carefully on a variety of monitors, including a large color balanced one (Eizo), and I think that 67^ instead of 100^is a good, safe but still punchy, value. There is just a hint too much moire at 100. (Of course this test image is a torture chamber.) I also looked at the result of resizing to 25% so that I could align results with the original, to get a sense of colour preservation. Should do that every time, actually.
P.S. 4 The original shows a good amount of sharpening halo. Maybe 100^,100% is just fine when the image being downsampled was not pre-sharpened.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 11, 2014, 03:17:42 am
Bart:
I think your script should use the downsample scheme when downsampling, and the generic scheme when upsampling, automatically switching between the two.
This leaves the issue of what is done near the identity in a somewhat undesirable state. But it's reasonable.
Near the identity (say, between 50% and 100%) it could blend the two results in linear light, in such a way that the current generic is all of it at 100% or above, and at 50% it's all the current "downsample".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 11, 2014, 04:04:39 am
Bart:
I think your script should use the downsample scheme when downsampling, and the generic scheme when upsampling, automatically switching between the two.
This leaves the issue of what is done near the identity in a somewhat undesirable state. But it's reasonable.
Near the identity (say, between 50% and 100%) it could blend the two results in linear light, in such a way that the current generic is all of it at 100% or above, and at 50% it's all the current "downsample".

Hi Nicolas,

I agree. However, during the stage of searching for optimal approaches and filters, I did want to allow the possibility to use either type of algorithm for both up- and down-sampling, for comparisons (without having to change the script itself).

Now that things seem to stabilize (although I'm thinking of a Keys Cubic variable input if that beats Lanczos), the remaining practical difficulty (with a batch script file) is to determine the fact whether one is effectively going to do upsampling or down-sampling, without asking the user for that.

If we were to restrict the input to percentage inputs, then it would be relatively simple, but I did also want to allow some of the ImageMagick special resizing options (http://www.imagemagick.org/Usage/resize/), such as fitting to maximum width (or height) to achieve an exact number of pixels (without running the risk of a percentage rounding to 1 pixel more of less than intended). That makes it harder to parse the input and determine if the result will be larger or smaller than the original.

I'm pretty sure it is possible to parse the input and find out what to do, but it could require quite a bit of code (and time to compact and optimize) to do so reliably. As a minimum, it should distinguish between e.g. 800% (obviously upsampling), and 800x800 (could be either upsampling or down-sampling, depending on the  input size), or 800x (for maximum width, could also be either up/down), or x800 (for maximum height, could also be either up/down). Ideally it would also allow to parse the other size limiting resizing options (!, >, <, ^, @) that IM recognizes, and determine which direction to take the resampling.

Again, I agree (and I have been looking into it already), but it may take a lot more time to really figure it out than I can currently invest. I suppose I could throw it over the fence of the IM discourse server and let someone else solve it ..., but not before doing an initial attempt myself (may be a character flaw on my side ;) ).

But feel free to call in the troops for their collective knowledge, which is what I do like about fora. The question to ask would be how to parse the IM resize options to determine whether up- or downsampling is going to occur (ideally expressed as a percentage, only to take further decisions on like with 50-100% identity resizes).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on August 11, 2014, 04:10:01 am
Then split the script into two:
Upsample (which can be used to downsample if this is what the command states, even though it's tuned for upsampling).
Downsample (which can be used to enlarge...).

Users usually know which one they want.
P.S. I also agree that EWA with Keys cubics gives an awful lot of mileage for the money. As far as linear schemes go, they are the best I know with only one halo. But unless you enlarge more than a little, they hold their own against (deblurred) EWA Lanczos 3, all things considered, which is mind blowing.
I am actually stunned. (There are theoretical reasons why they perform so well: They are almost second order accurate, the tensor Keys cubic being exactly second order accurate at the expense of not being rotationally invariant. But it does not appear to me that this "strength" should be sufficient.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: kirkt on August 11, 2014, 09:48:22 am
I have very little shell script programming experience, but I think I got the downsampling commands to work with the following *nix bash script, I called it "resizer.sh":

#!/bin/bash
#
# set default values for command line arguments
convolveScale=100 # -c scale of CONVOLVE operation
longEdge=512 # -s SIZE of long edge for resample
infile="input.jpg" # -i path to INPUT file
outfile="output.tif"        # -o path to OUTPUT file
       # -h help

while getopts "hc:s:i:o:" VALUE "$@" ; do
 
    echo "GOT FLAG $VALUE"
 
    if [ "$VALUE" = "h" ] ; then
        echo "USAGE: resizer.sh [options] ... -i infile -o outfile"
        echo "-c: convolve scale (default = 100)"
        echo "-s: size of output long edge in pixels (default = 512)"
        exit
    fi
    if [ "$VALUE" = "c" ] ; then
     convolveScale="$OPTARG"
        echo "convolveScale=$OPTARG"
    fi
    if [ "$VALUE" = "s" ] ; then
        longEdge="$OPTARG"
        echo "longEdge=$OPTARG"
    fi
    if [ "$VALUE" = "i" ] ; then
        infile="$OPTARG"
        echo "infile=$OPTARG"
    fi
      if [ "$VALUE" = "o" ] ; then
        outfile="$OPTARG"
        echo "outfile=$OPTARG"
    fi
 done
 
convert \( $infile -set colorspace sRGB -colorspace RGB \
  -define filter:c=0.1601886205085204 -filter Cubic -distort Resize "$longEdgex$longEdge" \) \
  \( -clone 0 -gamma 3 -define convolve:scale="$convolveScale^",100% \
  -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -define convolve:scale=100^,100% \
  -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  -delete 0 \
  \( -clone 1 -colorspace gray -auto-level \) \
  -compose over -composite \
  -set colorspace RGB -colorspace sRGB \
$outfile

The script permits the user to specify the convolve scale (so you can specify, for example, 50^ instead of 100^ and the long edge pixel dimension for the resize operation, as well as the input and output file, all as arguments on the command line.  Type -h for help.

You can comment out the "GOT FLAG $VALUE" and the echo of the flag arguments - this was for debugging.  Works on my Mac (Mavericks) and ImageMagick 6.8.9.  When I open the resulting output file in PS, there is no embedded color profile tag recognized by PS.

Thanks for all of your work on this!

kirk
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on August 11, 2014, 09:59:15 am
Thanks for all of your work on this!

Hi Kirk,

Thanks for chipping in, the more the merrier.

I'll have a look later at how that would work out in an interactive Windows environment.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 14, 2014, 07:06:54 am

Now that things seem to stabilize (although I'm thinking of a Keys Cubic variable input if that beats Lanczos)
Opinion: EWA with Keys cubics beat deblurred EWA Lanczoses if you don't want a second halo (for example if you want to post-sharpen). Otherwise, deblurred Jinc-windowed Jinc 3-lobe (EWA Lanczos 3) is the winner, unquestionably so if you are fighting moire (when downsampling) or you are enlarging more than a bit.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on September 18, 2014, 03:44:04 am
Hi,

I performed some tests using Bart's Script + IM with a converted B&W image and compared the results with PS and LR. I am using version 1.2.1 of Bart's script (I don't know if there is a newer one). One thing I found out is that if the original image is monochrome (all values R=G=B) and has an embedded RGB profile (such as sRGB) then the output file is in an invalid mode (as shown in the screen capture from Bridge in Fig 1) where the color mode is B&W and the color profile is sRGB. It seems that Image Magik detects the image is in B&W and leaves it that way. Photoshop would give a warning and ignore the profile.

This was solved by converting the original image to Gray Gamma 2.2 (you can use your preferred B&W profile) and then downsampling the image with Bart's script.

The downsizings were performed with the following parameters
Original image: JPEG, size 6148 x 3458
Output image size 1200 x 675

PS: Resample: automatic
LR: Resize to 1200 long edge, normal sharpen for screen output
IM+Script: Downsizing optimized, 1200 Long edge, 75% sharpening

The second image contains the center crop from each output file. The order from left to right: IM + Bart's Script; LR; PS.

The output from PS (right) shows some artifacts (rose windows) and is the worse of the three. LR performed a much better job (center) than PS but the image has a darker appearance than the original (similar to PS).

I'm attaching also (third image) a 100% crop of the original image to get the sense of the original details and brigthness in the rose window and bricks.

The output from IM + Bart's script (left) is IMO the better one. What surprised me was not the lack of artifacts (where LR also did a decent job), but how the texture of the bricks looks much better, closer to the original image while the LR and PS version look darker.

I have yet to explore the optimal settings with the script and also trying the deconvolution options, but overall I'm very satisfied with this method, which I would use for most of my images.

Thanks Bart, Nicolas and all who contributed to this tool.

P.D. I have posted the whole image (downsized) in the user critique forum here (http://www.luminous-landscape.com/forum/index.php?topic=93538.msg762532#msg762532) (CxC welcome)

Regards,


Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 18, 2014, 05:10:39 am
The scripts really are set up assuming sRGB as input and the same as output.
Because ImageMagick is OK at figuring out automatically what to do, it may work in other situations. For best performance, however, the scripts need to be modified when one or both of these assumptions does not hold.
The "core" of the method works from linear RGB and converts out of linear RGB. In addition, the script, as it is, assumes sRGB primaries. Which primaries are used is not important; what is important is that the "input" primaries and "output" primaries match. Additional steps that perform colorspace conversion may be required if this is not the case.
The most immediately useful sections of the ImageMagick documentation are
http://www.imagemagick.org/Usage/color_mods (http://www.imagemagick.org/Usage/color_mods)
http://www.imagemagick.org/Usage/formats/#color_profile (http://www.imagemagick.org/Usage/formats/#color_profile)
Generally, you'll want to convert from your input colorspace to some linear RGB, resample as usual with the core of the script, then convert out from the same linear RGB (same = same primaries) to whatever colorspace you want for the final result (this includes modifying the bit depth, choice of compression etc).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on September 18, 2014, 06:04:40 am
The scripts really are set up assuming sRGB as input and the same as output.
Because ImageMagick is OK at figuring out automatically what to do, it may work in other situations. For best performance, however, the scripts need to be modified when one or both of these assumptions does not hold.
The "core" of the method works from linear RGB and converts out of linear RGB. In addition, the script, as it is, assumes sRGB primaries. Which primaries are used is not important; what is important is that the "input" primaries and "output" primaries match. Additional steps that perform colorspace conversion may be required if this is not the case.
The most immediately useful sections of the ImageMagick documentation are
http://www.imagemagick.org/Usage/color_mods (http://www.imagemagick.org/Usage/color_mods)
http://www.imagemagick.org/Usage/formats/#color_profile (http://www.imagemagick.org/Usage/formats/#color_profile)
Generally, you'll want to convert from your input colorspace to some linear RGB, resample as usual with the core of the script, then convert out from the same linear RGB (same = same primaries) to whatever colorspace you want for the final result (this includes modifying the bit depth, choice of compression etc).

Thanks for your response. I actually started with an RGB file with the embedded profile sRGB. When I run the script I ended with a file in color mode B&W (which I assume was assigned by Image Magik) and color space sRGB, which was not accepted by Photoshop.

This is the process:

1) Start with a color image in sRGB
2) convert to B&W in Photoshop by Image -> Adjustments -> Black & White
3) Save

Technically, this is still an RGB image with profile sRGB, even if all values are R=G=B

4) Run the script

The output image has:
- Color mode: B&W
- Color space: sRGB

Which is considered as non-valid by Photoshop

Am I doing something wrong? How can I keep the image in Color mode:RGB?

That's why I converted the image to GrayGamma 2.2 in PS before saving. Surprisingly the downsizing worked well.

Regards
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 18, 2014, 03:53:52 pm
Francisco:
You did not do anything wrong. You found a quick and dirty workaround.
I don't have time to assemble and test a "most accurate and efficient" toolchain for your particular situation (no matter how common). Colorspace transformations/image format/compression in ImageMagick are not a trivial thing to put together "just right" (so much so that I usually use other tools when this is a key concern, assembling a hybrid pipeline). Which is why my response was basically RTFM.
So, let's call it a win. And hope that I have more time to RTFM myself some time before 2015.
-----
If I have a little bit of time, I may make a suggestion and hope it hits the nail without testing.
It is possible that this is relevant: http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=21031 (http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=21031)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 18, 2014, 04:18:52 pm
Francisco:
(Untested) I believe that the quick but less dirty answer has to do with inserting -type TrueColor just before the final conversion to JPEG.
http://www.imagemagick.org/script/command-line-options.php?#type (http://www.imagemagick.org/script/command-line-options.php?#type)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on September 18, 2014, 05:26:45 pm
Francisco:
(Untested) I believe that the quick but less dirty answer has to do with inserting -type TrueColor just before the final conversion to JPEG.
http://www.imagemagick.org/script/command-line-options.php?#type (http://www.imagemagick.org/script/command-line-options.php?#type)

Thanks Nicolas, that did the trick!  Now the resulting image shows Color Mode: RGB

Regards,
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 19, 2014, 07:31:20 am
Frank (a.k.a. Francisco):
I agree that with your example Bart's "sharpened downsampling scheme" is clearly superior to both LR and PS.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 19, 2014, 08:12:39 am
Francisco:
(Untested) I believe that the quick but less dirty answer has to do with inserting -type TrueColor just before the final conversion to JPEG.
http://www.imagemagick.org/script/command-line-options.php?#type (http://www.imagemagick.org/script/command-line-options.php?#type)

Hi Nicolas,

Indeed, which is what I had already implemented in the unpublished version 1.2.2 of the tool I was working on, before I got side-tracked by some urgent matters.

However, I implement it at the beginning of the IM convert command, in order to make all intermediate operations before writing the output aware of the requirement to maintain 3-channel RGB data, instead of simplifying to single channel gray scale.

Implementing the 'type TrueColor' parameter near the end may be more efficient, but there are also some potential gamma linearization issues caused by how ImageMagick interprets image gamma when it encounters single channel versus multichannel data. If I recall correctly, single channel images are assumed to already be in linear gamma, and multi-channel images are assumed to be in sRGB gamma space, unless specified otherwise first with a '-set colorspace' option. Maybe that behavior has changed in the mean time, I'm not sure.

Attached is the Version 1.2.2 implementation including the '-type TrueColor' addition, sorry for not posting it earlier.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 19, 2014, 08:27:53 am
The downsizings were performed with the following parameters
Original image: JPEG, size 6148 x 3458
Output image size 1200 x 675

PS: Resample: automatic
LR: Resize to 1200 long edge, normal sharpen for screen output
IM+Script: Downsizing optimized, 1200 Long edge, 75% sharpening

The second image contains the center crop from each output file. The order from left to right: IM + Bart's Script; LR; PS.

The output from PS (right) shows some artifacts (rose windows) and is the worse of the three. LR performed a much better job (center) than PS but the image has a darker appearance than the original (similar to PS).

I'm attaching also (third image) a 100% crop of the original image to get the sense of the original details and brigthness in the rose window and bricks.

The output from IM + Bart's script (left) is IMO the better one. What surprised me was not the lack of artifacts (where LR also did a decent job), but how the texture of the bricks looks much better, closer to the original image while the LR and PS version look darker.

Hi Frank,

Yes, this clearly shows the benefits of down-sampling in linear gamma space. It is superior in maintaining tiny luminance differences and will also prevent near single pixel color errors when calculating the weigthed average of multiple pixels. It also shows that the Photoshop recommended (by Adobe) BiCubic Sharper method produces lots of aliasing artifacts, on the fine details of the window details, but even on slanted (e.g. roof) edges which produce clearly visible stair-stepping artifacts.

Thanks for your feedback. As I said before, the more the merrier.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on September 19, 2014, 04:57:06 pm
Attached is the Version 1.2.2 implementation including the '-type TrueColor' addition

Hi Bart, excellent.  Can you summarize the state of the art (what functions are being used for downsampling, sharpening and deconvolving - and why)?

'preciate this a lot!

Jack

PS I also agree that yours looks the best out of the three that Francisco showed
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 20, 2014, 03:31:25 am
I agree with Bart that the main differentiator in Francisco's example is almost certainly doing the "local averaging" component of the downsampling in linear light.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 20, 2014, 05:58:30 am
Hi Bart, excellent.  Can you summarize the state of the art (what functions are being used for downsampling, sharpening and deconvolving - and why)?

Hi Jack,

Sure, this is the executive summary:

All resampling methods that are used in the script, are based on Elliptical Weighted Averaging or EWA (therefore in two dimensions) instead of a two pass orthogonal tensor approach. This has the benefit of producing more 'organic looking' resizing because the resulting resolution is circularly symmetric, rather than having a higher diagonal resolution that is possible in a square pixel grid.
BTW, EWA resampling also has benefits for other types of (asymmetrical or variable) distortion.

A drawback of EWA resampling, besides the higher cost of having to process more samples, is that a (nearly) No-Op (scale close to 1.0) resampling will be slightly blurry, instead of having no effect on sharpness. Therefore, and for adaptability to different subject matter and viewing conditions, a sharpening parameter is added which allows to modify the behavior of the resampling and windowing filter used.

There are different filter methods available for upsampling and for down-sampling, and they are chosen because they perform better (less artifacting) for the intended scaling. The script is set up to allow choosing either filter method regardless of the fact of upsampling or down-sampling, but that's more for experimentation purposes.

The actual resampling results are a linear gamma space blend between two resampling operations. The resampling operations are performed in two different gamma spaces in order to cope with the different halo under-/overshoot amplitudes. The blending of those in linear gamma space is luminance driven which allows to address the different halo tendencies in darker and lighter tones, and it reduces the risk of clipping.

This is all based on a proposal by Nicolas Robidoux, after experimenting with other temporary contrast adjustment functions based on Sigmoidal tone curves. The approach with resampling in separate gamma spaces allows to better target the suppression of halo under/overshoots in different luminance ranges.

The upsampling operation (labeled as 'generic', because it also does pretty decent down-sampling) is implemented by using the EWA version of 'Lanczos' filtering (http://www.imagemagick.org/Usage/filter/#lanczos_cylindrical), also known as 'Jinc' filtering, to make it circularly symmetric when simply scaling both dimensions by the same factor. Some 'deblur (http://www.imagemagick.org/Usage/filter/#blur)' is used, the amount is user adjustable. The deblur is controlled by modification of the filter support and window size.

The down-sampling operation is implemented by using the EWA version of 'Cubic' filtering (http://www.imagemagick.org/Usage/filter/#cubics), with an adjusted parameter choice to use a relatively soft filter version (from the family of Keys filter parameters), which is achieved with the following '-define filter:c=0.1601886205085204 -filter Cubic'. That defines a somewhat blurry filter, but also relatively halo and ringing free, which helps because down-sampling needs to actively avoid aliasing and ringing artifacts. It also blends the results of two resampling operations that were done in different gamma spaces, to allow and control halo generation.

To compensate for the softness/blur of the Cubic filter version, deconvolution sharpening is added. The deconvolution uses a Gaussian blur based weighting, and is implemented as a Dirac Delta function subtracted by a Gaussian blur, with the ImageMagick DoG (difference of Gaussians) function, which produces a 2-D convolution filter kernel.

There is a different down-sampling operation that is used when the choice is made to eliminate all sharpening, e.g. because one wants to use a separate utility for that. That operation only uses the EWA version of the 'Quadratic' filter (http://www.imagemagick.org/Usage/filter/#quadratic) in linear gamma space, without blending with other gamma space results. That filter produces very clean, essentially halo free, results (thus the possibility to skip blending with non-linear gamma space resampling) with very little aliasing but it's a bit blurry, and will therefore allow significant (e.g. local) sharpening without risk of enhancing existing halos.

As a bonus, an experimental option is added to the script to add a simple separate deconvolution sharpening operation, when zero or 'negative sharpening' (i.e. blur) was used.

All operations are executed in the spatial domain, to circumvent potential issues with limited 16-bit precision calculations that will create artifacts in the (Fourier converted) frequency domain. The future versions of ImageMagick will apparently also be available as already complied binaries that allow floating point Fourier transforms, which may help with faster and more precise calculations. It may also generate some other resampling options.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 20, 2014, 09:52:48 am
Bart:
It is possible to construct EWA methods that are interpolatory. One example is -filter Triangle -distort Resize (EWA with a "cone" of radius 1, which is also the basis of the downsampling component of the NoHalo method built into GEGL, the new library core of GIMP).
Other examples are obtained by restricting any filter nonzero at the origin to the disc of radius 1, or shrinking a BC-cubic's radius to sqrt(2) instead of 2, fixing things so the zero crossing is at 1. (I've experimented with the unique Keys cubic that satisfies these conditions, and was not impressed with the result.)
One way or another, I have not managed to find/construct a "pure" EWA method that is both interpolatory and strongly antialiasing (at least, more strongly antialiasing than the "cone"). In other words, I don't know a non-contrived high quality EWA filter that reproduces the identity map when the geometrical transformation is "no-op".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 20, 2014, 10:44:11 am
Bart:
It is possible to construct EWA methods that are interpolatory. One example is -filter Triangle -distort Resize (EWA with a "cone" of radius 1, which is also the basis of the downsampling component of the NoHalo method built into GEGL, the new library core of GIMP).
Other examples are obtained by restricting any filter nonzero at the origin to the disc of radius 1, or shrinking a BC-cubic's radius to sqrt(2) instead of 2, fixing things so the zero crossing is at 1. (I've experimented with the unique Keys cubic that satisfies these conditions, and was not impressed with the result.)
One way or another, I have not managed to find/construct an EWA method that is both interpolatory and strongly antialiasing (at least, more strongly antialiasing than the "cone").

Nicolas:
To make sure, I'm not criticizing the near No-Op blurring of EWA methods, it may even improve the look of certain image content that is almost aligned with the pixel grid and would fade in and out of perfect alignment (e.g. slightly rotated or tilted image content with a regular structure, like the noise example here (http://pixinsight.com/doc/docs/InterpolationAlgorithms/InterpolationAlgorithms.html#__section004__)).

Trade-offs are inevitable, and if one needs to fine tune for a very specific scenario, it's usually possible to find some optimization that does slightly better than a generic solution, but it's not so easy to find good compromises that do not cause unwanted side effects like aliasing. That's why I think that some sort of sharpening operation will allow to improve results that try to avoid artifacts.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 20, 2014, 12:01:58 pm
...
This is all based on a proposal by Nicolas Robidoux, after experimenting with other temporary contrast adjustment functions based on Sigmoidal tone curves.
...
Quick comment: "Sigmoidization", the ancestor of "luminance weighted gamma resampling/sharpening", is basically dead. (It probably is useful for single channel, e.g. grayscale, images, but otherwise it is not recommended.) Main theoretical reason: http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796 (http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on September 21, 2014, 04:55:07 am
Hi Jack,

Sure, this is the executive summary: ...

The down-sampling operation is implemented by using the EWA version of 'Cubic' filtering (http://www.imagemagick.org/Usage/filter/#cubics), with an adjusted parameter choice to use a relatively soft filter version (from the family of Keys filter parameters), which is achieved with the following '-define filter:c=0.1601886205085204 -filter Cubic'. That defines a somewhat blurry filter, but also relatively halo and ringing free, which helps because down-sampling needs to actively avoid aliasing and ringing artifacts. It also blends the results of two resampling operations that were done in different gamma spaces, to allow and control halo generation.

To compensate for the softness/blur of the Cubic filter version, deconvolution sharpening is added. The deconvolution uses a Gaussian blur based weighting, and is implemented as a Dirac Delta function subtracted by a Gaussian blur, with the ImageMagick DoG (difference of Gaussians) function, which produces a 2-D convolution filter kernel.

There is a different down-sampling operation that is used when the choice is made to eliminate all sharpening, e.g. because one wants to use a separate utility for that. That operation only uses the EWA version of the 'Quadratic' filter (http://www.imagemagick.org/Usage/filter/#quadratic) in linear gamma space, without blending with other gamma space results. That filter produces very clean, essentially halo free, results (thus the possibility to skip blending with non-linear gamma space resampling) with very little aliasing but it's a bit blurry, and will therefore allow significant (e.g. local) sharpening without risk of enhancing existing halos.

As a bonus, an experimental option is added to the script to add a simple separate deconvolution sharpening operation, when zero or 'negative sharpening' (i.e. blur) was used.

All operations are executed in the spatial domain, to circumvent potential issues with limited 16-bit precision calculations that will create artifacts in the (Fourier converted) frequency domain. The future versions of ImageMagick will apparently also be available as already complied binaries that allow floating point Fourier transforms, which may help with faster and more precise calculations. It may also generate some other resampling options.

Cheers,
Bart

Wow Bart, excellent effort and explanation!  What happened to Lanczos for downsampling?  And is b=1 in the cubic -filter with the specified c?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 21, 2014, 07:18:51 am

Wow Bart, excellent effort and explanation!  What happened to Lanczos for downsampling?

Thanks Jack,

Lanczos (or its EWA circular equivalent Jinc implementation) is still a pretty good filter for discrete (quantized) image source data. That's why it is the basis for upsampling. However, it also creates a bit of ringing (depending on edge/detail contrast, which is lower for upsampling but potentially higher for down-sampling). While that may not be really noticeable on most down-sampled images that are intended to be displayed relatively small, I also anticipated a requirement for post down-sample sharpening which would make it more visible. Also, linear gamma space down-sampling will preserve those ringing artifacts very well.

Therefore I picked up on the observations made by Nicolas Robidoux about the usability of the Keys Cubic family of filters that satisfy the 2C+B=1 relationship. Given the 'need' (IMHO) for sharpening after resampling, I opted for a relatively softer variant of the filter family than those near the Robidoux/Mitchell region (near B=C=1/3), because they exhibit more blocking/ringing risk. I verified that the relatively soft 'filter:c=0.1601886205085204' parameter, which was nominated for becoming a named filter (simpler and more understandable to encode) would produce a useful deconvolution sharpening setting.

Quote
And is b=1 in the cubic -filter with the specified c?

According to the ImageMagick documentation (http://www.imagemagick.org/Usage/filter/#cubic_bc):  If one of the 'b' or 'c' settings has not been defined or set, its value will be calculated from the other value on the assumption that you are wanting a 'Keys' family filter (along the dotted line in the Mitchell-Netravali Survey diagram).

This still allows to specify any other 'b' value in addition to that 'c' value if that benefits specific image content. That's another benefit of using Keys Cubics instead of Lanczos, it's highly tweakable to image content, and it's also relatively efficient to code at a lower level (it's also used in some OpenGL / GPU oriented software implementations, see example 24-7 (http://http.developer.nvidia.com/GPUGems/gpugems_ch24.html)).

Changing the 'c' (or 'b') parameter, does require to re-evaluate the currently used deconvolution settings because they are specifically optimized for the current 'RobidouxSoft' setting.

Cheers,
Bart

P.S. I may seem that I'm overly concerned about ringing and blocking in down-sampled image content, but I've seen and shot enough images that created problems for down-sampling to want to avoid that.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on September 21, 2014, 04:17:14 pm
P.S. I may seem that I'm overly concerned about ringing and blocking in down-sampled image content, but I've seen and shot enough images that created problems for down-sampling to want to avoid that.

Thank you Bart, I am 100% with you there.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 03:07:01 am
I'd be curious to see the result of downsampling Franks' church image with the "generic" scheme. It would give a hint as to whether Frank got better results mostly because of downsampling through linear light is to perform the same reduction with the "generic" scheme instead of the one targeted at downsampling. (Of course, the "generic" scheme does "half" of the downsampling through linear light, so this only sort of hints at what is going on...)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 03:29:03 am
Jim: I am certain that you are not the first to want to interface ImageMagick and Matlab.
If google does not settle the question, my guess is that if you post a query at http://imagemagick.org/discourse-server/viewforum.php?f=1 (http://imagemagick.org/discourse-server/viewforum.php?f=1), you'll get bites.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on September 23, 2014, 05:37:12 am
I'd be curious to see the result of downsampling Franks' church image with the "generic" scheme. It would give a hint as to whether Frank got better results mostly because of downsampling through linear light is to perform the same reduction with the "generic" scheme instead of the one targeted at downsampling. (Of course, the "generic" scheme does "half" of the downsampling through linear light, so this only sort of hints at what is going on...)

Hi Nicolas,

The attached image shows the comparison between the downsampling-optimized (left) vs the generic scheme (right). The generic scheme gives similar results to LR. The optimized scheme does make a difference.

Regards,
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 23, 2014, 05:54:18 am
I'd be curious to see the result of downsampling Franks' church image with the "generic" scheme.

If Frank is reading this, what Nicolas would like to see is a comparison with the script:
1. Choose the same output size as before and Down-sample as you did.
2. Choose the same output size as before and 'Upsample' down like before.

By selecting 'Up' instead of 'Down', the 'generic' EWA Lanczos filter is used, instead of the EWA Keys Cubic filter.

The optimized Down-sampling method with a sharpening amount of 50 will probably produce slightly sharper results than the generic method with sharpening 'amount' of 50. That can also produce a slightly higher risk of micro-stair-stepping jaggies on very fine lines that are smaller than 1 pixel wide, but also slightly crisper results.

Both methods use linear gamma resizing/blending, so I do not expect too much of a difference from that, it's more to do with the difference in filter weights.   

Cheers,
Bart

P.S. I see that Frank already responded.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 06:10:08 am
There is a big difference between "generic" and "downsample" in the way they use nonlinear light (namely gamma 3):
"Downsample" prefilters/resamples (only) in linear light and uses a pair of gamma spaces to deconvolve the downsampled result.
"Generic" prefilters/resamples with a pair of gamma spaces (and, if deconvolution is added to the result of the dowmsamping, again uses a pair of gamma spaces).
IMHO this is a more significant difference than using different EWA weights (and support): It is a "structural" difference, as opposed to "parametric".
(Whether MHO is correct could of course be settled using the same filter in both "downsample" and "generic", comparing the result of only using linear light in the resample stage with the result of blending two resampling results.)
-----
In summary: "generic" resamples using luminance-weighted gamma blending, and "downsample" sharpens a linear light result using luminance-weighted gamma blending.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 23, 2014, 06:19:04 am
There is a big difference between "generic" and "downsample" in the way they use nonlinear light (namely gamma 3):
"Downsample" prefilters/resamples (only) in linear light and uses a pair of gamma spaces to deconvolve the downsampled result.
"Generic" prefilters/resamples with a pair of gamma spaces (and, if deconvolution is added to the result of the dowmsamping, again uses a pair of gamma spaces).
IMHO this is a more significant difference than using different EWA weights (and support): It is a "structural" difference, as opposed to "parametric".
(Whether MHO is correct could of course be settled using the same filter in both "downsample" and "generic", comparing the result of only using linear light in the resample stage with the result of blending two resampling results.)
-----
In summary: "generic" resamples using luminance-weighted gamma blending, and "downsample" sharpens a linear light result using luminance-weighted gamma blending.

Nicolas, you are correct. It's the suppression of Lanczos/Jinc ringing (under-shoot) with a Gamma blend that seems to produce most of the difference for downsampling. The Down-sampled Sharpening gamma blend just produces more symmetrical edge sharpening.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 06:58:17 am
Of course, none of this hair splitting would matter if the results were not as good as they appear to be.
(Bart: My apologies for being in a "pedantic mood". Has to do with sleep deprivation, I think. Much celebration lately.)
P.S. Come to think of it, I'm generally in a pedantic mood. Ah well.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 07:11:33 am
Thank you Frank. Very interesting comparison.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on September 23, 2014, 08:26:24 am
Hi, I have another image for comparison between the optimized-downsampling (Bart's Script) method and Photoshop Automatic resize mode. The first attachment is the complete image from the Melbourne convention centre downsampled (optimized scheme) to 1200 pixels wide.

The second image contains the comparison in the following order:

- The top section is the output from Photoshop resizing, shown at 300%
- The middle section is the output from the optimized-downsampling script, also at 300%
- The bottom section is the full size image, shown at 50% (which is not exactly the same size as the other two, but close for comparison purposes)

A few areas for comparison:

- Area "A", top of the handrails for the stairs, looks darker in the PS resized image.
- Area "B", in the PS resize, detail has been lost completely with the PS resizing.
- Area "C", shows a pronounced halo in the PS resized image.

Following Bart's instructions to have the script available as an option in the "send to" submenu (windows) makes this option something really easy to use. I'll have to study a little bit of the scripting language to be able to send an entire folder or group of images at once.

Regards,
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 09:33:21 am
Opinion:
The Melbourne convention center is an example image in which EWA with a deblurred Jinc-windowed Jinc with 3 (maybe 4) lobes may be preferable to using EWA Keys cubic (which are 2-lobe methods) with post-sharpening, to eliminate the moire in the top right red wall without losing detail.
(Note that all my comments are off the top of my head: Based on experience, not experiment.)
P.S. Does the moire noticeably go down if the deconvolution sharpening amount is reduced?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: fdisilvestro on September 23, 2014, 10:23:03 am
Hi Nicolas,

I performed another test (I'm not sure if I addressed correctly your comments)

I have attached two images, one with comparisons made at 300% and the second one at 100%

- The top image is the same as the previous post, using the downsampling-optimized scheme with 100% sharpening
- The middle image is using the "generic" scheme without sharpening
- The bottom image is again using the generic scheme with deconvolution sharpening, Blur radius 2 px and 50% amount

I cannot tell if there is less moire with the generic version, maybe just a slight difference.  What I'm not sure is if I should try different parameters

Regards,

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 01:37:50 pm
Looking at the last results posted by Frank, I think that EWA LanczosSharp (say) through linear light (skipping the gamma blending in the "generic" scheme) may do well with the moire, but I have to admit that the "downsampling" scheme does a pretty good job given how sharp it is. 3-lobe EWA Lanczoses, deblurred or not, will add some haloing/ringing if kept sharp :(
Maybe, in the "dowsampling" scheme, pushing the Keys C (a.k.a. alpha) down toward 0 and going light on the deconvolution sharpening?
No free hot lunch, most likely. But it would be nice to have something concrete to recommend when moire shows its ugly head.
-----
Idea:
Separately compute the result of 3-lobe EWA Lanczos through linear light. (I don't think LanczosSharp, either version, would be better for this.) Provide a "moire reduction" slider: 100% = pure EWA Lanczos through linear light. 0% = Pure "current downsampling scheme".
This way we are not messing with the tuned luminance weighted gamma blended deconvolution.
Will add ringing, sure, but that's less offensive than moire.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 23, 2014, 02:46:56 pm
Looking at the last results posted by Frank, I think that EWA LanczosSharp (say) through linear light (no dual blending, like is done in the "generic" scheme) may do well with the moire, but have to admit that the "downsampling" scheme does a pretty good job given how sharp it is. 3-lobe EWA Lanczoses will add some haloing/ringing if kept sharp :(
Maybe, in the "downsampling" scheme, pushing the Keys C (a.k.a. alpha) down toward 0 and going light on the deconvolution sharpening?
No free hot lunch, most likely. But it would be nice to have something concrete to recommend when moire shows its ugly head.

Letting the Keys 'C' go to 0.0 (and 'B' to 1.0) will produce more blurry output. By also lowering/eliminating sharpening the result will get quite soft, but aliasing, blocking, and ringing will also be minimal. Doing that as a simple linear light EWA Spline filter, without blending, works better than with blending.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 23, 2014, 02:56:03 pm
Bart:
Indeed, that's the question: Is it possible to get rid of (most of) the moire without making things soft (or messing up the nice properties of downsampling through linear light)? My idea above hopes that one can trade moire for some ringing without losing too much sharpness.
Come to think of it, it one is willing to trade moire for not downsampling through linear light, blending the "downsampling" result with the "generic" scheme with sharpening set at 0 could work nicely, and would not add much ringing.
In any case, I like the original "blend (in linear light) the result of the "downsampling" scheme (before the conversion to sRGB) with the result of plain EWA Lanczos through linear light" idea more.
What do you think?
P.S. Maybe blending with the result of EWA Quadratic in linear light? (Can such a cheap filter be moire suppressing enough?)
Basically, there are two components to this "idea" of using a slider to let users reduce moire:
1) Find a good moire fighting filter that's not too blurry or ringy.
2) Make sure it jives, when blended, with the "downsampling" scheme.
P.S. 2 Maybe the moire is tolerable given the sharpness. (Using 67% sharpening instead of 100% makes it unnoticeable?) Any opinions? On this topic, see http://www.luminous-landscape.com/forum/index.php?topic=91754.msg751649#msg751649 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg751649#msg751649).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 24, 2014, 12:58:25 am
Resample_by_Percentage_V122.bat contains a zero-consequence bug. Each of the four occurrences of:
-define convolve:scale=^%sharpAmount%%%,100
should read:
-define convolve:scale=%sharpAmount%%%^^,100
Explanation: the caret ^ is the Windows escape character. Windows says, okay, no need to do anything with that, and strips it before passing the command to convert.exe. But we want convert.exe to see the caret, meaning "normalize to make the kernel sum to zero", so the caret needs doubling in the command. I've moved it to the end because that's how the documentation http://www.imagemagick.org/Usage/convolve/#identity_addition has it. Perhaps the position doesn't matter.

This seems to be zero-consequence. Perhaps DoGs are zero-sum by design.

#3655:
Quote from: Bart
... the remaining practical difficulty (with a batch script file) is to determine the fact whether one is effectively going to do upsampling or down-sampling, without asking the user for that.
I think the most sensible way is to tell IM to resize the image, and see what it comes up with. The result could be upsampled horizontally but downsampled vertically, eg a 1000x1000 image might be resized to 2000x500. As upsampling is the "generic" method, perhaps the rule is to use the downsampling method only if both dimensions shrink.

I have some more comments, but this margin is too narrow to contain them.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 24, 2014, 02:58:19 am
In the interest of making as quick progress as possible, let's agree that when downsampling (more than a little), nonlinear light (gamma different than 1) will only be used in the deconvolution step.
There may be some advantage to explore the alternative ("generic"-type luminance weighted gamma blending in the "prefiltering/resampling" step, as opposed to "downsampling"=type LWGB in the post-filtering step), but I think that the path of least resistance is to stick to linear light when downsampling (but not when sharpening).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 24, 2014, 05:10:45 am
...
This seems to be zero-consequence. Perhaps DoGs are zero-sum by design.
...
Alan: My guess is that if coefficients are computed accurately enough, things will turn out to be unnoticeably close to zero-sum.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 24, 2014, 05:18:46 am
Hi Alan,

Thanks for joining us here. I appreciate your insights of the IM code specifics a lot, especially because of the Windows batch script dialect translations.

Resample_by_Percentage_V122.bat contains a zero-consequence bug. Each of the four occurrences of:
-define convolve:scale=^%sharpAmount%%%,100
should read:
-define convolve:scale=%sharpAmount%%%^^,100
Explanation: the caret ^ is the Windows escape character. Windows says, okay, no need to do anything with that, and strips it before passing the command to convert.exe. But we want convert.exe to see the caret, meaning "normalize to make the kernel sum to zero", so the caret needs doubling in the command. I've moved it to the end because that's how the documentation http://www.imagemagick.org/Usage/convolve/#identity_addition has it. Perhaps the position doesn't matter.

This seems to be zero-consequence. Perhaps DoGs are zero-sum by design.

Correct, according to the usage web page "All the edge detection kernels have one feature in common. They are all zero-summing." This can be verified by adding "-define showkernel=1", which will display the actual kernel values (if echo is on).

I remember struggling with the correct syntax when I started using the convolution option (and the convolve operator has changed over time), also due to the fact that the IM web page commands are UNIX oriented, and Windows batch files have slightly different syntax (such as the caret escape character instead of backslash). I stopped trying when it worked as intended.

In fact, the caret character by itself does not serve any function in the convolve operator, it was probably left over from an escaped exclamation point normalization in the past, and can now be removed all together because the DoG is zero-summing (unlike some other kernels I tried in the past), and normalizes automatically. As you said, the error is zero-consequence, but I've completely removed the caret for the updated script version 1.2.3. Should we decide it is useful for a future change (e.g. a hand-crafted kernel), it can also be replaced by an exclamation point to force a normalization (! may be more telling than ^^, and currently serves the same purpose in the -convolve:scale operator).

Quote
#3655:I think the most sensible way is to tell IM to resize the image, and see what it comes up with. The result could be upsampled horizontally but downsampled vertically, eg a 1000x1000 image might be resized to 2000x500. As upsampling is the "generic" method, perhaps the rule is to use the downsampling method only if both dimensions shrink.

The goal would be to predict which direction to take, instead of determining it afterwards.

Quote
I have some more comments, but this margin is too narrow to contain them.

Looking forward to more helpful comments.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 24, 2014, 05:34:41 am
Alan: My guess is that if coefficients are computed accurately enough, things will turn out to be unnoticeably close to zero-sum.

Correct. The zero-summing and normalization are automatic, and I specified to create a slightly larger (7x7) kernel than the default (5x5) that IM calculated. That results in a normalized kernel sum that gives less than a maximum of 0.11 of an RGB value difference in 16-bit precision.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on September 24, 2014, 04:04:43 pm
Correct. The zero-summing and normalization are automatic, and I specified to create a slightly larger (7x7) kernel than the default (5x5) that IM calculated. That results in a normalized kernel sum that's gives less than a maximum of 0.11 of an RGB value difference in 16-bit precision.

Hi Bart,

I am a big fan of your efforts here, thank you very much for doing this, I think we need a tool like this.  My constructive feedback on 1.2.2: it seems to me that the downsampling versions, with all those clones, gammas and blending are losing some local contrast along the way.  I thought it was the DoGs, but it looks like I was barking up the wrong tree :)

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 24, 2014, 06:44:30 pm
Thanks for the good work.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 25, 2014, 12:49:28 am
Suggestion:
Since these ideas and scripts are starting to spread and hopefully people will start experimenting with variants (although they are awesome as is), I would like to suggest some terminology:
The "generic" scheme currently is LWGB3 deblurred EWA Lanczos (at least in the default version, without deconvolution).
The "downsampling" scheme currently is EWA RobidouxSoft with tuned LWGB3 Gaussian deconvolutions. (The plural comes from that the deconvolution parameters are slightly different for gamma 1 and gamma 3.)
LWGB3 = Luminance Weighted Gamma Blending (with gamma) 3.
EWA = Elliptical Weighted Averaging (the "clamped" version, advocated originally in Andreas Gustaffson's Masters thesis AFAIK, and the default in ImageMagick for a few years now, where it's called Clamped EWA. Nobody in her right mind should use the original Heckbert, non-clamped, version so I think we can safely drop the "clamped" in "Clamped EWA". On the other hand, the original version still occasionally shows up in academic papers so maybe Clamped EWA is better?)
RobidouxSoft if the Keys cubic with alpha=C=0.1601886205085204.
As components get swapped in and out, the above are easy to modify so that people (or at least I) know exactly what's going on. And then it's easier for everybody to search for the components and go directly where they are discussed. For example, someone blending with a gamma 2 result could use LWGB2 and then we'd all know what's going at a glance.
-----
Bart: These work for you? I suppose even shorter versions could be LWGB EWA Lanczos and EWA RobidouxSoft with LWGB deconvolution (or even EWA Keys with LWGB deconvolution, since the exact value of the Keys alpha is a detail). Still mouthfuls, but nonetheless pretty specific.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 25, 2014, 01:02:50 am
As Bart no doubt knows, more local contrast could most likely be obtained by using a larger Keys alpha (=C). But then he needs to retune the deconvolution, so this is not a one minute fix. And we'd need to see which artifacts this increases. Zero sum is hard to escape.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 25, 2014, 02:55:09 am
Hi Bart,

I am a big fan of your efforts here, thank you very much for doing this, I think we need a tool like this.  My constructive feedback on 1.2.2: it seems to me that the downsampling versions, with all those clones, gammas and blending are losing some local contrast along the way.  I thought it was the DoGs, but it looks like I was barking up the wrong tree :)

LOL. As people say; A dog is man's best friend ...

Jack, I'm not sure where your perceived loss of local contrast comes from, but maybe it is the absence of artifacts (seeing that many people like Luminance aliased Sigma Foveon conversions)? All my tests show me that there is a very good correspondence between source and result although, due to the choice for a relatively soft interpolation filter, the need for post-resample sharpening is a given.

Of course, it is possible to process the down-sampled result (optionally with a sharpening amount of 0) with halo free tools like Topaz 'Clarity' or 'Detail'. That's why I maximize the 'Lossless' nature of the output, even for JPEGs. However, it is still on my To-Do list to find an automatic clamping function that allows to boost sharpening which will automatically throttle back locally when artifacts (like halo and clipping) begin to show-up. It will probably be something along the lines of a Blend-if functionality that I have mentioned before.

Thanks for your feedback.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 25, 2014, 03:06:32 am
Bart: These work for you? I suppose even shorter versions could be LWGB EWA Lanczos and EWA RobidouxSoft with LWGB deconvolution (or even EWA Keys with LWGB deconvolution, since the exact value of the Keys alpha is a detail). Still mouthfuls, but nonetheless pretty specific.

Nicolas: Yes, sounds usable although cryptic for the uninitiated, but such is the nature of jargon.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 25, 2014, 03:16:38 am
As Bart no doubt knows, more local contrast could most likely be obtained by using a larger Keys alpha (=C). But then he needs to retune the deconvolution, so this is not a one minute fix. And we'd need to see which artifacts this increases. Zero sum is hard to escape.

Indeed, but the trade-off between more artifact prone filters, or softer ones with added sharpening is always present. It is still on my radar to find an automatic Blend-if functionality that allows to boost the sharpening amount without reintroducing artifacts like jaggies/aliasing.

Of course, if I would have more time to spare, I could probably also develop an automatic re-tuning of the deconvolution settings, based on a variable Keys alpha input (something that a modern commercial image processing tool could use). Lack of resources is unfortunately slowing me down ...

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 25, 2014, 04:42:40 am
I have adapted the 1.2.2 script to suit my own working methods, and published resampHM.bat with a number of trials at http://im.snibgo.com/resamphm.htm

My script takes parameters instead of prompting for values. The other major change is that there is only a single convert command. Thus any deconvolution doesn't spend time (and lose precision) converting to sRGB, writing a file, reading the file, and converting to RGB.

My script expects to find identify.exe in directory %IM%, and convert.exe in %IM32f%. For my web page, %IM32f%convert.exe is Q32 HDRI.

The script can auto-select the method from downsampling or upsampling. See the page for details. I don't think the IM utilites can find resized dimensions without also creating the image in memory. But a trivial utility could be written to take a width, height and resize, call ParseMetaGeometry() in geometry.c, and return the new dimensions.

I shouldn't judge the results from a single image, but I gotta say: I love the downsampling with sharpen. It's much better than I get with "-resize -unsharp".

A note on colorspaces and profiles: Resample_by_Percentage_V122.bat writes any embedded profile to a file, then flags the image as sRGB with "-set colorspace sRGB". This will give inaccurate linearization. After saving the profile, it would be better to convert the image to sRGB with "-profile sRGB.icc", perhaps providing a path to that file. (The IM installation directory contains sRGB.icc.) At the end of the convert command, the "-profile" operation will then convert the image back to the saved ICC profile.

My page describes how to adapt my script for non-sRGB colorspaces. As almost all my work is in sRGB, I haven't automated this.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 25, 2014, 06:20:40 am
I have adapted the 1.2.2 script to suit my own working methods, and published resampHM.bat with a number of trials at http://im.snibgo.com/resamphm.htm
...
Alan: This is a very nice and informative comparison.
Comment about your "u" option examples: Among deblurred EWA Lanczoses, those that correspond, roughly, to low sharpening (<50) are more artifact free. For example, LanczosSharp corresponds to something like 10.
Anything above 100 is guaranteed unusable: With EWA Lanczos, (de)blur=0.88549061701764 a.k.a. EWA LanczosSharpest 3, is the smallest reasonable value that can be used (based on a heuristic), and this is (should be) what 100 corresponds to. So the range of sharpening values shown in your examples probably should be narrower. 0 to absolutely no more than 50. (A wider range was provided for experimentation and allowing people to push the envelope. It's not necessarily recommended.)
Note: These are preliminary comments. I'll have a closer look later today.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 25, 2014, 07:23:21 am
Quote from: NicolasRobidoux
So the range of sharpening values shown probably should be narrower.
Fair comment, thanks. My work on this isn't over; I'll be using it for downsample-with-sharpening on a variety of images. If I discover anything interesting, I'll publish it. I do very little upsampling.

Meanwhile, I'll keep an eye on what you folks are doing. You know vastly more about filters and lobes and stuff than I do.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 25, 2014, 09:30:14 am
Alan:
Another thing: With sharpening = 0, the "downsampling" scheme uses EWA Quadratic instead of RobidouxSoft. So, sharpening = 1 (or .0001) should give a signficiantly different result.
On the other hand, note that the Keys Cubic RobidouxSoft is not very far from the cubic B-spline, which in some sense is not "very" far from the Quadratic B-spline.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on September 25, 2014, 09:44:33 am
LOL. As people say; A dog is man's best friend ...

Jack, I'm not sure where your perceived loss of local contrast comes from, but maybe it is the absence of artifacts (seeing that many people like Luminance aliased Sigma Foveon conversions)?

Can't quite put it into words, Bart.  I can't tell whether my perception of loss of local contrast is due to the shadow-end of transitions being raised or whether it's the highlight-end that, having been boosted, overwhelms the shadow-end.  Keep in mind that my meter of comparison is PS bicubic, as that's what my monitor shows when viewing the full sized image at less than 100% (close to 25% in this case).

Perhaps the forum could help me with the right words.  This is a typical situation: a landscape downsized about 3.5:1 to 1000 pixel height.  The following animated GIF (with all its limitations) shows alternating 1 second views of the same capture downsized with simple bicubic in PS and with v1.2.2 using the downsample routine and 100% sharpening.  It's pretty clear which is which.

(http://i.imgur.com/G3zQbtp.gif)

Click this link (http://i.imgur.com/G3zQbtp.gif) to download or see the image at full size in your browser ( make sure it doesn't resize it: ctrl-zero in Chrome).

First comment is that I find 100% too crispy (see for instance grass bottom right) - not a big deal, I could just as easily use 50% sharpening.  The second is that to me it looks as if saturation and local contrast have been lost by v1.2.2.  See for instance the grass to the left of the blue boot and shadows around the DoG.  Similar effect at 50%.  It's enough to make me prefer bicubic, which can't be right :)

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 25, 2014, 11:24:53 am
Can't quite put it into words, Bart.  I can't tell whether my perception of loss of local contrast is due to the shadow-end of transitions being raised or whether it's the highlight-end that, having been boosted, overwhelms the shadow-end.  Keep in mind that my meter of comparison is PS bicubic, as that's what my monitor shows when viewing the full sized image at less than 100% (close to 25% in this case).

Hi Jack,

That's a very good example of some of the differences between resampling algorithms. In fact, the shadows are as dark as they were at the original size, and the highlights are also preserved better. However, our eyes play a trick on our perception due to the smaller scale (we are more sensitive to the lighter tones when our eyes can't resolve detail, and we imagine luminance differences based on direction of light and we invent edge enhancement, AKA Mach effect). One remedy would indeed be to reduce sharpening to 50, which restores original sharpness/detail without boosting it. That still leaves some of the perceptual effect due to scale.

You could also try reducing sharpening to 0 or less, which will allow to manually dial in deconvolution radius and amount. By choosing a larger radius, say 1 - 2, instead of approx. 0.5, the sharpening will turn a bit more into a local contrast enhancement.

It would be interesting to see a crop of the grass at the original size, and compare it with a zoomed in version of the down-sampled result with sharpening set to 50. I would expect the mean/median value of the histograms to be close to each other, while a bicubic would be darker and with low contrast and lacking subtle color differentiation.

When the reduced sharpening doesn't help enough, there would be of course also be other means to address our over-zealous perception (e.g. Topaz Detail with a Magenta-Green luminance shift for darker Greens), but it would be nice if we can keep the workflow simple.

Quote
Perhaps the forum could help me with the right words.  This is a typical situation: a landscape downsized about 3.5:1 to 1000 pixel height.  The following animated GIF (with all its limitations) shows alternating 1 second views of the same capture downsized with simple bicubic in PS and with v1.2.2 using the downsample routine and 100% sharpening.  It's pretty clear which is which.

I'd say human perception is playing tricks on us, but maybe we can find a way to address that as well. It is actually tying in with my 'Blend-if' desire  ...

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 25, 2014, 12:29:47 pm
Maybe the "downsample" scheme should have default sharpening set to 50 instead of 100?
P.S. Because we are using EWA lanczoses to upsample, not downsample, the default sharpening for the "generic" scheme should be very low. If I have time, I'll figure what corresponds to the LanczosSharp found in Adam Turcottte's thesis. Pretty close to zero. Alan Gibson's tests http://im.snibgo.com/resamphm.htm (http://im.snibgo.com/resamphm.htm)) support this choice.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on September 25, 2014, 01:16:25 pm

It would be interesting to see a crop of the grass at the original size, and compare it with a zoomed in version of the down-sampled result with sharpening set to 50. I would expect the mean/median value of the histograms to be close to each other, while a bicubic would be darker and with low contrast and lacking subtle color differentiation.

Here are the histograms in an animated GIF (http://i.imgur.com/VpdRonH.gif) (http://i.imgur.com/VpdRonH.gif)

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 25, 2014, 01:53:54 pm
RE: Jack Hogan's excellent comparison:
With sharpening at 50 or so, I would like to know if making the two deconvolution parameters the same better preserves the color histograms. (Set them both to the linear light value.)
I would also like the answer to the same question with sharpening = 1 (0, really, but this gives EWA Quadratic through linear light, a completely different scheme.)
P.S. I wonder if we have to be more careful in our colorspace conversions. (No time...)
P.S.2 I also wonder whether I need to have another go at blending approaches that are symmetrical w.r.t. black<->white interchange.
P.S.3 I bet symmetric breaking is what's what's going on. Back to the drawing board.
P.S.4 But then, the HVS is asymmetrical. :|
P.S.5 Not obvious how to improve things. In this example, the very dark shadows are actually slightly fuller with LWGB: some form of contrast is increased, just not "symmetrically". Bad Idea: Instead of weighting gamma 1 with the luminance, weight it with luma? (In which case the question of which gamma to use becomes more important: Which gamma is best?)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 12:50:11 am
At this point the best suggestion I have (off the top of my head) to fix the "things get lighter near high amplitude high frequency" is likely to have only a small effect:
1) Use the same deconvolution filter params for both gammas.
2) Instead of using gamma 1 and gamma 3, use gamma 1.5 and gamma 3. (So luminance becomes luma 1.5.)
Hopefully Bart will think of something more effective.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 03:46:14 am
Possibly a red herring, but here is:
Looking at http://blog.kasson.com/?p=7225 (http://blog.kasson.com/?p=7225), I am a bit surprised to see how much dark haloing is seen with the EWA Keys with LWGB deconvolution method.
Are the results blended correctly? (I vaguely recall that it's quite hard to completely get rid of the dark halos, so maybe this is expected.)
-----
Wish I had time to do my own tests...
P.S. Jack Hogan: Thank you for bringing the "lightening effect" to our attention.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 03:56:02 am
Here is another idea:
Set the parameters of the two deconvolutions so that they are the same.
First, check that the "lightening effect" is not there if the two gammas are the same (=1).
Then, push the second gamma up until there is noticeable lightening.
Hopefully, both the light and dark halos are still reasonably small.
This is a really pragmatic solution that takes into account that escaping zero sums is hard.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 05:53:24 am
If this has not been done already:
First, it needs to be ascertained that the issue is not with the colour conversion.
Then, it needs to be ascertained whether there is only lightening. Comparing with the downsampling results of PS or LR is not a reliable indicator B/C I'm pretty sure they don't use linear light (like, for example, nip2, when proper import and colorspace conversion has been performed).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 06:22:02 am
Bart:
(I think we discussed this before.)
Are you sure that -auto-level really helps?
P.S. What I am trying to do is simplify the method as much as possible so that we figure out whether "tonality" is preserved, and how to make things better if not. Also, complex ImageMagick scripts sometimes reveal quirks in the methods' implementations, and simple -> fewer surprises.
P.S.2 Also see Alan Gibson's comment on proper use of color profiles with ImageMagick in http://www.luminous-landscape.com/forum/index.php?topic=91754.msg764826#msg764826 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg764826#msg764826) and his script for how to cleanly handle Adobe RGB. I find it very plausible that the LWGB changes the lightness of high amplitude high frequency patches, but we need to be careful not to mix such artifacts (if they are significant) with shortcomings of the toolchain w.r.t. color space handling. FWIW, this is why http://www.imagemagick.org/Usage/filter/nicolas/ (http://www.imagemagick.org/Usage/filter/nicolas/) warns that the suggested ImageMagick calls assume sRGB input and output. The color space handling should be tackled separately.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 11:12:36 am
For downsampling at the least, I think that the only way to get this to work "perfectly" will be to have the blending be locally determined so as to better preserve local averages. This is likely not to be a trivial thing to put together.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 26, 2014, 11:58:26 am
...
P.S. I wonder if we have to be more careful in our colorspace conversions. (No time...)

Is a color space conversion needed or is it "just" linearisation that's needed?  If it's just a gamma correction does it matter if it's the 100% correct gamma that's used.
I know of gamma sRGB (close to 2.2, but a little bit special in the low values) ,1.8 (prophoto) 2.2 (adobe and most others).  It could be that gamma 2.2 is good enough for all resamplings...

PS. color space conversions do more that just a gamma correction.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 12:20:53 pm
Is a color space conversion needed or is it "just" linearisation that's needed?  If it's just a gamma correction does it matter if it's the 100% correct gamma that's used.
Ideally, the input image should be converted to linear light before being pushed through the resampling pipeline, and the final result converted out of linear light into the desired output color space. If both the input image and output images are sRGB, this is trivial with ImageMagick. As Alan Gibson pointed out in his version of the script, if what's coming is a gamma space (like Adobe RGB), and the output is a gamma space with the same primaries, it's again easy. Things start being more complicated when one deals with color profiles. Making one single tool that automatically, accurately and correctly deals with all possibilities is tricky.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 26, 2014, 12:28:26 pm
Possibly a red herring, but here is:
Looking at http://blog.kasson.com/?p=7225 (http://blog.kasson.com/?p=7225), I am a bit surprised to see how much dark haloing is seen with the EWA Keys with LWGB deconvolution method.

As in the earlier test in this thread (reply 164 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg748825#msg748825)) on your suggested CGI vertical edge, it is virtually impossible to avoid halos. With a tiny amount of blur, less than a typical lens produces, the halos are almost gone. Real life camera images are more blurry (Sigma Foveon aliased raw conversions aside), due to lens aberrations, defocus, diffraction, and demosaicing. Also, not all edges are perfectly aligned with the pixel grid, some edges land dead in the middle of the pixel itself, reducing its intensity to halfway between the luminance on both sides.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 12:30:04 pm
Bart:
(Again off the top of my head. No experimenting.)
I am pretty sure that -auto-level is a mistake.
The "lightening" of high frequency patterns will be minimized if the blending alpha varies slowly.
Using -auto-level normalizes it in a way that makes it vary more rapidly.
I realize the attraction of "normalizing" in a "scaling independent" way.
However, in the present case, I would put money down that most of the time it makes things worse.
Nice images are not scaling invariant.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 12:33:14 pm
As in the earlier test in this thread (reply 164 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg748825#msg748825)) on your suggested CGI vertical edge, it is virtually impossible to avoid halos. With a tiny amount of blur, less than a typical lens produces, the halos are almost gone. Real life camera images are more blurry (Sigma Foveon aliased raw conversions aside), due to lens aberrations, defocus, diffraction, and demosaicing. Also, not all edges are perfectly aligned with the pixel grid, some edges land dead in the middle of the pixel itself, reducing its intensity to halfway between the luminance on both sides.
Point taken. The goal is to resample real, off the camera, digital photographs. Results obtained with anything else are backs of the envelope.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 12:45:42 pm
Bart:
In the KISS dept, I think my best suggestion so far to minimize the "lightening" effect is to use gamma 1.5 or 2 together with gamma 3 in the deconvolution step. That is: raise the lower gamma so as to "balance" the dark (which currently are visible) and light (which currently are invisible) halos. (And get rid of -auto-level and use the same deconvolution with both gammas.)
Note: I am not talking about the "generic" scheme here. I am talking about the "downsampling" scheme, for which I think that EWA should be done in linear light but the deconvolution should be done in a pair of perceptual spaces chosen to balance and minimize the dark and light halos. There is no reason to stick to gamma 1 except possibly to compute the luminance, although I suspect that using the luma derived from the lowest gamma (whether it is 1 or higher), when deconvolving, is just as good.
P.S. Grrr. Do we lose something perceptually significant by deconvolving out of linear light?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 26, 2014, 01:49:06 pm
If this has not been done already:
First, it needs to be ascertained that the issue is not with the colour conversion.
Then, it needs to be ascertained whether there is only lightening. Comparing with the downsampling results of PS or LR is not a reliable indicator B/C I'm pretty sure they don't use linear light (like, for example, nip2, when proper import and colorspace conversion has been performed).

I agree, and that's why I asked for a crop of the original and of the down-sampled image (to be blown up with nearest neighbor to the original size for comparison) and histogram mean or median of the histogram compared. We may be comparing a darker duller Lightroom conversion with a better luminance preserving LWGB one. There may also be a difference between profiles (e.g. AdobeRGB working space versus sRGB destination space).

For that purpose, I did a comparison (see attachment) of a crop of a larger file, in Adobe RGB space, downsampled that to 25% with LWGB with sharpening (amount 50, and 100), and upsampled that with Nearest Neighbor (pixel replication). The Histogram Mean / Median of the original is 106.97 / 104, of the amount 50 sharpened it is 108.55 / 106, and of the amount 100 sharpened it is 108.45 / 106.

That isn't to say that there is no lightening due to the linear gamma resampling (there is some), but it's not necessarily as strong as Jack's example shows.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 02:00:42 pm
What would not surprise me at all is that LWGB slightly darkens dark textures and slightly lightens light textures. In other words, it may also have built-in frequency dependent contrast enhancement.
I'm not particularly concerned about it screwing up mid-tones. (Famous last words?)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 26, 2014, 02:30:32 pm
Far out idea: Choose the "lower gamma" so as to best fit the input image's linear light histograms.
(Yes, I know, my recent suggestions are quite ad hoc.)
P.S. Attempting to grasp the big picture, I have managed to convince myself that anchoring the "dual gamma" blending on gamma 1 was a mistake: Use two different gammas larger than 1.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 26, 2014, 03:42:03 pm
Quote from: NicolasRobidoux
Making one single tool that automatically, accurately and correctly deals with all possibilities is tricky.
Yes.

As a rule, I wouldn't. I think it's better to stay within one colourspace (sRGB, AdobeRGB1998, WidestGamut, whatever you want) throughout the workflow. Take offshoots from the mainstream workflow as desired, eg sRGB for display on a screen, but I think batting around between profiles within the workflow is bad practice.

My own workflow is sRGB. My tools work in sRGB. If I used something else, such as AdobeRGB1998, I would adjust each tool to work in that space. I would not bracket each tool to convert to sRGB and back.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 26, 2014, 04:36:45 pm
Yes.

As a rule, I wouldn't. I think it's better to stay within one colourspace (sRGB, AdobeRGB1998, WidestGamut, whatever you want) throughout the workflow. Take offshoots from the mainstream workflow as desired, eg sRGB for display on a screen, but I think batting around between profiles within the workflow is bad practice.

My own workflow is sRGB. My tools work in sRGB. If I used something else, such as AdobeRGB1998, I would adjust each tool to work in that space. I would not bracket each tool to convert to sRGB and back.
Hi

If the end result is sRGB (web)  and originals are prophoto (tif) and adobeRGB (jpg) (and from time to time sRGB), would you then use 2 or (3) tools or do a convertion to sRGB first?
(I'm now lucky that I can select on extention.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 26, 2014, 05:21:45 pm
Round trips between colorspaces lose both precision and accuracy, so the main thing is to avoid this. Profiles are necessary evils, but profile conversions really are horrible and should be kept to a minimum.

Then we have considerations of processing speed, user convenience, familiarity with the tools, and tool maintenance. I would rather convert all inputs to a common space and push them through an identical workflow than have to use different tools for different spaces.

If outputs are needed in different spaces, as well as inputs being in different spaces, then decisions become harder. Then it may become a trade-off between convenience (funnel everything through a single colour space) and precision (minimise colour space conversion).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jim Kasson on September 26, 2014, 06:01:36 pm
Round trips between colorspaces lose both precision and accuracy, so the main thing is to avoid this.

I'm not sure what you mean by "precision" in this context. Precision  (http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Double_precision.html)as I use the term in the context of computational accuracy describes the number of bits used in encoding the values and how they are allocated. Round trip conversion of an image with 16-bit precision usually results in an image with 16-bit precision. Round trip conversion of an image with 32-bit floating point representation (aka single precision) usually results in an image with 32-bit floating point precision.

As to accuracy, while 3D lookup table conversions usually "walk" after round trips, it is possible to do conversions between model-based color spaces, such as display spaces, with no loss in accuracy save rounding to the original precision, if the intermediate calculations are performed with sufficient precision. For performance reasons, that is not usually the case.

However, current tools are quite robust. As a test, I took this 16-bit, sRBG ray-traced image of Bruce Lindbloom's desk (http://www.brucelindbloom.com/) that I'd res'd down to 20% using Bart's script version 1.2.2:

(http://www.kasson.com/ll/RTorig.jpg)

I brought it into Photoshop CC 2014.1.0 converted it to Adobe RGB, then to Prophoto RGB, then to Adobe RGB again, then to sRGB using the ACE Engine and Absolute rendering intent, with black compensation turned off. Note that this set of conversions involves one change of gamma. There is also a change of white point, but Photoshop blithly ignores that.

I subtracted the original image from the one with the four color space conversions, and got this:

(http://www.kasson.com/ll/RTdiff.jpg)

Then I applied a 10-stop exposure increase. This resulted:

(http://www.kasson.com/ll/RTdiffPlus10ev.jpg)

Jim

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 26, 2014, 08:34:00 pm
We hope for both precision (a decent number of bits) and accuracy (the bits we have, are correct). Your 8-bit jpg diff file has a maximum value of 4 (out of 255). After 4 conversions, the worst pixels have only 6 accurate bits. Not wonderful, really.

On the other hand, the diff file is jpeg, and that compression may have created noise that raised the error to 4 out of 255.

Of course, the image matters far more than the numbers. (I don't want to gain a reputation as a bean-counter. Umm, bit-counter.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 27, 2014, 02:21:03 am
RE: Alan Gibson's (snibgo)'s warning about using color profiles.
The ImageMagick colorspace and color profile system has seen many improvements in the last few years and I am not quite up to speed on the details.
In the FLOSS world, I personally trust the libvips/nip2 system more (http://libvips.blogspot.dk/2012/11/new-colour-package.html). The main reason is that when importing with a color profile, a "large" reference container colorspace (like L*a*b* or XYZ) is used as an anchor. (At least, that's more or less how it used to work. The system is even smarter now.) A consequence is that when I request conversion into and out of linear light, I have full trust that I am getting what I am asking for (modulo the accuracy of the profile itself).
Maybe ImageMagick is now up to that level of trustworthiness w.r.t. the entire color space/profile business. But I'm not sure. So I prefer to take profiles out of the equation if I can. And the whole thing is a can of worms. For example, my understanding is that older sRGB profiles are often somewhat inaccurate on round trips. (That's one of the benefits of using sRGB v4.) I'm not only concerned with round trip accuracy: I want linear light to be as close as possible to actually being linear light. Returning to Earth after a trip to Phobos does me little good if I wanted to visit Mars.
P.S. Really, at this point, I want to know that if there is color drift it is because of a shortcoming of LWGB, not some color toolchain quirk. By design, LWGB will not make flat patches drift. But I find it quite likely that high frequency patches drift with LWGB, and it is on my TODO list to figure out how much of an issue this drift is.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 27, 2014, 03:40:52 am
I have adapted the 1.2.2 script to suit my own working methods, and published resampHM.bat with a number of trials at http://im.snibgo.com/resamphm.htm
In the first "Basic Upsampling" results, I do see a difference between #3 (LWGB EWA Lanczos) and the others (tensor Mitchell-Netravali through sRGB (#1) and linear RGB (#2)): LWGB is sharper yet just a bit less jaggy, and has a little more haloing. LWGB also shows something that can be vaguely described as more color texture or color separation (the "cheap gummidruck" look). (Hard to put my finger on it. Looks sort of like chromatic aberration or color noise.) This last property could be a side effect of the additional sharpness: the LWGB is a bit more "vivid" in the color department.
Certainly no big bang for the buck, though.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 27, 2014, 11:01:34 am
Bart (and Alan and Jim):
I think I may have a solution to the "lightening of high frequency patches" issue.
Here is the off the top of my head version that is closest to what we are doing now:
After downsampling in linear light using EWA RobidouxSoft, compute two deconvolved versions (I would prefer with the same deconvolution parameters but this is not necessary) with two different gamma, making sure to convert the two results to linear light.
Now, compute three luminance images:
1) Luminance of the non-deconvolved image
2) Luminance of the result of deconvolving through the first gamma (gamma 1, say)
3) Same as 2) with the second gamma (gamma 3, say)
At every pixel, choose the result with the median luminance (averaging ties, say) out of 1), 2) and 3).
That's it.
-----
I'll see if I can come up with something more likely to be better.
P.S. Let me explain the heuristic behind this.
What we want to do is improve LWGB deconvolution sharpening.
We don't want to do things on a per channel basis because don't want to lose monochromaticity preservation (that is, if all three channels are proportional in linear light before sharpening, it's also true after sharpening, ignoring clipping issues). See http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796 (http://www.luminous-landscape.com/forum/index.php?topic=77949.msg745796#msg745796). Performing median filtering of the three results on a per-channel basis would break this property. Averaging would not.
So, let's talk luminance.
Recall that the lower the gamma, the more noticeable dark halos, and the higher the gamma, the more noticeable light halos (of same "numerical" size).
If the pixel is part of a light halo in at least one of the two sharpened results, its sharpened luminance is higher than the unsharpened luminance. So, don't use the pixel value that corresponds to the highest luminance.
If the pixel is part of a dark halo in at least one of the two sharpened results, its sharpened luminance is lower than the unsharpened one. So, don't use the lowest one.
The key component of the heuristic consists of reversing the implications:
If you pick the median of the three results, you'll never choose the gamma creating the worst halo. Provided the other gamma creates a moderate halo in the same situation, you have successfully tamed halos.
Now, you may ask what happens when the two gammas produce luminance values that bracket the original's.
This is likely to happen on high frequency patches. Selecting the non-sharpened result in this situation will help the preservation of local averages. We want that.
-----
The above, as usual, is off the top of my head. Let's see if there is some proof in the pudding.
P.S. I just realized that there is a clear path to resolving ties: 1) (no deconvolution) is best, 2) (lowest gamma) is second best, 3) (highest gamma) is last.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 27, 2014, 01:19:14 pm
Quote from: NicolasRobidoux
After downsampling in linear light using EWA RobidouxSoft, compute two deconvolved versions (I would prefer with the same deconvolution parameters but this is not necessary) with two different gamma, making sure to convert the two results to linear light.
I'm unsure about this, so I'll leave it to someone else.

Quote from: NicolasRobidoux
Now, compute three luminance images:
1) Luminance of the non-deconvolved image
2) Luminance of the result of deconvolving through the first gamma (gamma 1, say)
3) Same as 2) with the second gamma (gamma 3, say)
At every pixel, choose the result with the median luminance (averaging ties, say) out of 1), 2) and 3).
I think the following almost delivers what I think you want. From three source images, each output pixel comes from one of the three sources, determined by which luminance is equal to the median luminance. Windows BAT syntax; adjust for other shells.

If two or three sources have luminances equal to the median, it won't average the pixels from the sources. Every output pixel comes from only one input source.

%IM%convert ^
  %SRC0% ^
  %SRC1% ^
  %SRC2% ^
  ( -clone 0 -colorspace gray +write x0.png ) ^
  ( -clone 1 -colorspace gray +write x1.png ) ^
  ( -clone 2 -colorspace gray +write x2.png ) ^
  ( -clone 3-5 -evaluate-sequence median +write x4.png ) ^
  -compose Difference -fill White ^
  ( -clone 3,6 -composite +opaque Black +write x5.png ) ^
  ( -clone 5,6 -composite +opaque Black +write x6.png ) ^
  -delete 3-6 ^
  -compose Over ^
  ( -clone 2 -clone 1 -clone 4 -composite +write x7.png ) ^
  ( -clone 0 -clone 5 -clone 3 -composite +write x8.png ) ^
  -delete 0-5 ^
  %OUTFILE%
It isn't optimal; I optimise only when I am reassured the logic is correct. Every "+write x?.png" is only for debugging; they can all be removed.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jim Kasson on September 27, 2014, 03:36:13 pm
We hope for both precision (a decent number of bits) and accuracy (the bits we have, are correct). Your 8-bit jpg diff file has a maximum value of 4 (out of 255). After 4 conversions, the worst pixels have only 6 accurate bits. Not wonderful, really.

On the other hand, the diff file is jpeg, and that compression may have created noise that raised the error to 4 out of 255.

This is a post about mission creep.

I thought of responding to you that it wasn't appropriate to subtract errors in a nonlinear color space. Then it occurred to me that It wasn't appropriate for me to subtract the images in Ps, which not only does the subtraction in gamma-corrected space, but throws away negative values. Then it occured to me that if I performed the subtraction in linear space and kept the negative numbers, I still wouldn't have any reasonable way to make a scalar from them.

So I bit the bullet and wrote some Matlab code to convert both images to CIEL*a*b*, and compute delta-E at each pixel.

(http://www.kasson.com/ll/PsColorDiffCode.PNG)

A few notes. I'm using Jerker Wagberg's OptProp  (http://apachepersonal.miun.se/~magneu/publications/R-07-77.pdf)for the color calcs. When I go to Lab I'm using the D65 spectrum and the 1931 2 degree observer for the white point.

When I run the program, I get a mean DeltaE of 1.4515, a standard deviation of 1.2533, and a worst cae DeltaE of 6.3707.  

That seems like a lot. So I wrote some more code to do 100 round trip conversions srgb>argb>srgb, with quantizing to 16 bit unsigned integer precision after each conversion:

(http://www.kasson.com/ll/rtCode.PNG)

Then I got an average error of 6.004 * 10^-4 DeltaE, a standard deviation of 0.0011 DeltaE, and a worst case error of 0.0485 DeltaE.

The take home for me is that color conversion between display-based color spaces don't have to be a significant source of error, even with 16-bit working spaces, but that the Ps (ACE) implementation is not as good as it could be.

The way the errors walk is interesting:

(http://www.kasson.com/ll/rtsigmavsiter.PNG)

(http://www.kasson.com/ll/rtwcvsiter.PNG)

Thanks for prompting me to do this work.

[Added 10/10/2014: The above CIELab DeltaE errors are actually much worse than is accurate. The reason is a confusion on my part wrt sRGB color spaces. The original Brue Lindbloom image was in a space with the sRGB primaries and a gamma of 2.2, while I thought it was in the IEC 61966-2-1:1999 color space. When corrected the errors are quite small even for repetitive 16 bit conversions. See here for some results with a tougher target image: http://blog.kasson.com]/?p=7517

Jim
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 27, 2014, 03:58:21 pm
...
My oh my Alan! You're awesome.
Anyway, all I did was hook things up and check that nothing blows up. No clear opinion yet on the quality of the resulted compared with the "old" downsample. (Manana.) But this looks promising.
I used 100 deconvolution to emphasize tone drift, if any.
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -colorspace gray \) \
  \( -clone 1 -colorspace gray \) \
  \( -clone 2 -colorspace gray \) \
  \( -clone 3-5 -evaluate-sequence median \) \
  -compose Difference -fill White \
  \( -clone 3,6 -composite +opaque Black \) \
  \( -clone 5,6 -composite +opaque Black \) \
  -delete 3-6 \
  -compose Over \
  \( -clone 2 -clone 1 -clone 4 -composite \) \
  \( -clone 0 -clone 5 -clone 3 -composite \) \
  -delete 0-5 \
  -set colorspace RGB -colorspace sRGB -quality 98 output.jpg
(The -set colorspace sRGB should not be needed because we're dealing with JPEGs, but are needed if, say, we're using PNGs, so I left them there.)
Question: If there is a median tie, does this resolve it by chosing 0 if possible, then 1, and never 2 (never 2 since one needs a tie to resolve)? This is what I would like.
Here is the result on the fly http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg (http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg)
P.S. As a comparison, I also attached the result of plain EWA RobidouxSoft through linear RGB, without the LWGB deconvolution:
convert \
  input.jpg -set colorspace sRGB -colorspace RGB \
  -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \
  -set colorspace RGB -colorspace sRGB -quality 98 plain.jpg
P.S.2 Also attached the result with 50^,100% instead of 100^,100%.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 27, 2014, 05:55:50 pm
Good work, Jim. It's good to know where errors are, or that there aren't any. I'm spending a happy weekend hunting bugs in ImageMagick's YCC colorspace.

Quote from: NicolasRobidoux
Question: If there is a median tie, does this resolve it by chosing 0 if possible, then 1, and never 2 (never 2 since one needs a tie to resolve)? This is what I would like.
Your wish is my, umm, headache. If there is a tie, the code above would use 0 if possible, otherwise 2, never 1.

The revised script below resolves ties in order 0 then 1, never 2. The two changed lines are marked ***.
%IM%convert ^
  %SRC0% ^
  %SRC1% ^
  %SRC2% ^
  ( -clone 0 -colorspace gray +write x0.png ) ^
  ( -clone 1 -colorspace gray +write x1.png ) ^
  ( -clone 2 -colorspace gray +write x2.png ) ^
  ( -clone 3-5 -evaluate-sequence median +write x4.png ) ^
  -compose Difference -fill White ^
  ( -clone 3,6 -composite +opaque Black +write x5.png ) ^
  ( -clone 4,6 -composite +opaque Black +write x6.png ) ^  ***
  -delete 3-6 ^
  -compose Over ^
  ( -clone 1 -clone 2 -clone 4 -composite +write x7.png ) ^  ***
  ( -clone 0 -clone 5 -clone 3 -composite +write x8.png ) ^
  -delete 0-5 ^
  %OUTFILE%
Resolving ties by taking averages would need the 4 average colour images to be made (01, 02, 12, 123), one more median difference, then further complexities to decide which of the seven sources to use. I feel a migraine coming on.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 02:26:00 am
Alan:
Thank you.
-----
Resolving ties by preferring no-deconvolution over some, and low gamma over high, should be better than with averages. Keep the aspirin in the cupboard (although I can't do anything about YCC bugs).
P.S. Here is the full enchilada with favored median tie resolution (and hardcoded options):
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -colorspace gray \) \
  \( -clone 1 -colorspace gray \) \
  \( -clone 2 -colorspace gray \) \
  \( -clone 3-5 -evaluate-sequence median \) \
  -compose Difference -fill White \
  \( -clone 3,6 -composite +opaque Black \) \
  \( -clone 4,6 -composite +opaque Black \) \
  -delete 3-6 \
  -compose Over \
  \( -clone 1 -clone 2 -clone 4 -composite \) \
  \( -clone 0 -clone 5 -clone 3 -composite \) \
  -delete 0-5 \
  -set colorspace RGB -colorspace sRGB -quality 98 output.jpg
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 06:24:24 am
The "new downsample" is promising, and I like its heuristic basis a lot more than the "old downsample".
Here is a quick comparison with the usual fly, with hardwired options:
# "Old downsample" (blending alpha proportional to the luminance of the gamma 1 result)
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
    -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
    -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -define convolve:scale=100^,100% \
    -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  -delete 0 \
  \( -clone 1 -colorspace gray -auto-level \) \
  -compose over -composite \
  -set colorspace RGB -colorspace sRGB -quality 98 olddownsample100.jpg

# "New downsample" (among the unsharpened result and sharpened results computed with two different gammas, select result with median luminance)
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -colorspace gray \) \
  \( -clone 1 -colorspace gray \) \
  \( -clone 2 -colorspace gray \) \
  \( -clone 3-5 -evaluate-sequence median \) \
  -compose Difference -fill White \
  \( -clone 3,6 -composite +opaque Black \) \
  \( -clone 5,6 -composite +opaque Black \) \
  -delete 3-6 \
  -compose Over \
  \( -clone 2 -clone 1 -clone 4 -composite \) \
  \( -clone 0 -clone 5 -clone 3 -composite \) \
  -delete 0-5 \
  -set colorspace RGB -colorspace sRGB -quality 98 newdownsample100.jpg
It looks like I'll have to come up with a new acronym.
P.S. I've also attached the result with the new scheme using a higher strength for the DoG: 125^ instead of 100^. The new scheme does not sharpen as much given the same strength. 125 "new" roughly matches 100 "old" (as measured by JPEG file size :) ). The differences are subtle, but they are there.
Just because "why not", I also added the result with 150^.
P.S.2 Quick and dirty check of luminance drift. With "neutral" strenght of the DoG, namely 50, I saved the results into pngs, loaded into nip2, converted to XYZ, and compared the Y channel image mean with that of the input. The new scheme is a lot closer. Not conclusive. But encouraging.
P.S.3 This seems to hold at higher strengths of the DoG: Same strength -> "new" preserves mean luminance better than "old". This comparison, however, is not quite fair, because the "new" scheme is perceptually less sharp at equal DoG strength. And this is one image etc etc etc.
The jury is still out. But I'd put money on "new" being a better method than "old".
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 07:41:30 am
Pngs of the results with DoG strength = 50.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 10:17:47 am
With the "new downsample", there are isolated single or pairs of pixels that surprise me. Hung jury for now.
P.S. Typo in test code. (Unwittingly used the other way of resolving ties.) Redoing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 28, 2014, 11:49:00 am
With the "new downsample", there are isolated single or pairs of pixels that surprise me. Hung jury for now.

Nicolas,

Allow me to make a few quick remarks that might save some time when looking for differences.

First of all, I really appreciate Alan's coding skills being put to good use for improving the algorithms we consider. It may also have given me some better idea how to tackle my Blend-if ideas that allow to reduce the risk of clipping, and that a add a few other positive side effects.

The deconvolution radius should IMHO be different for the different gamma versions of restoration. I understand your concerns about the potential effects on color by using different radii, but the simple reason for different radii is that restoration of resolution with the 'wrong radius' will either under-achieve with a smaller radius, or over-achieve (with halo) with a larger radius than optimal. The optimal radii were determined by fitting a Gaussian blur kernel to the 10x oversampled PSF blur as observed. Because the radii are almost the same, I didn't mention it yet as I estimate the sub-optimal restoration will have a hardly noticeable effect, but I do want to mention it now that we start looking at how individual pixels turn out.

When comparing single pixels, I think it is very important to eliminate the potential influence of JPEG compression (quality), and Chroma sub-sampling. That's why I set those parameters to the highest possible quality level in the 'old' version.

Also, in the 'old' version I force the processing to 16-bit precision from the start of the conversion with the '-depth 16' parameter, because ImageMagick tends to use the lowest amount of memory if possible (e.g. by using single channel images if 3-channel monochrome images are input), and may (or may not) do some of the processing in 8-bit/channel precision. I'd rather be over-cautious than sorry, and prefer to force bit-depth to 16, even for 8-b/ch input. When experimentation stabilizes, we can always test if it makes any difference for a JPEG workflow, but I want to keep the options for dealing with other input sources open for now.

The Median weighting of the various luminances that result from deconvolution restoration, tends to choose conservatively, towards un-sharpened luminance domination. This results in a reduced sharpness, although on the positive side it seems to also reduce aliasing a little, even with a boosted sharpening amount. Alternatively one might also consider a different Keys 'C' value as filter input, but I do not want to change too many things at the same time, because we'd lose track of what exactly caused the observed changes in image detail.

I didn't want to nip the creative thinking process in the bud, because that's how progress can be made, by stumbling on unanticipated side-effects that we may like. However, the lightening of high amplitude high frequency image content, is the direct result of resampling in linear gamma, not necessarily the other factors that are being looked at. For example averaging 51 and 204 in 8-bit gamma 2.2 space will produce 127.5, but it will produce 152 when first linearized, then averaged, and converted back to the original gamma space. It has way more impact that the otherr things that are being looked at.

Blending in linear light is essential if we want to preserve accurate color (e.g. as in the Dalai Lama image test) but it does lighten the high amplitude and high (beyond Nyquist) spatial frequencies as they are low-pass filtered. One might test how a modest low-pass filter run in gamma space before down-sampling in linear light would work. Maybe it does a better job of achieving both objectives in RGB space, reduced lightening and preservation of accurate color blends.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 28, 2014, 11:58:22 am
It looks like I'll have to come up with a new acronym.

Down-sampling with Median Blended Gamma Deconvolution (MBGD down-sampling)?

Cheers,
Bart
Title: Round trip color accuracy with double precision FP
Post by: Jim Kasson on September 28, 2014, 12:27:27 pm
If we leave out the quantizing to 16 bit after every conversion, and leave the image in double-precision floating point representation all the time, we can see that the round trip color space conversion does not materially affect the accuracy of the color encoding.


(http://www.kasson.com/ll/meanFPRT.PNG)


(http://www.kasson.com/ll/sigmaFPRT.PNG)


(http://www.kasson.com/ll/wcFPRT.PNG)

Jim
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 12:28:12 pm
...
Bart:
Thank you for this detailed write up. I take note.
-----
Not sure, but I think the weird pixels or pairs of pixels usually have a clipped channel (blue = 0, for example).
Come to think of it, clipping should be expected to "confuse the heuristic". (I'm running a Q16 version of IM, hence clipping, within the toolchain, is possible.)
I gather that this does not surprise you either.
When sharpening, clipping is really evil.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 02:00:19 pm
One thing I have not fully figured out is that the gamma 3 result is most likely lighter than the other two even when nothing's happening. Taking the median thus generally returns one either the unsharpened (linear light) result or the gamma 1 result. This may, or may not, be the best thing.
-----
P.S. Here is a very very simple scheme that gets rid of (P.S. most of) the "odd pixels". (Bart: Note that for the specific purpose of downsampling JPEGs or 8-bit sRGB PNGs, I've checked that my shortcuts are harmless).
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=50^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=50^,100% \
     -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  -evaluate-sequence median \
  -set colorspace RGB -colorspace sRGB altdownsample50.png
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 28, 2014, 04:18:58 pm
One thing I have not fully figured out is that the gamma 3 result is most likely lighter than the other two even when nothing's happening. Taking the median thus generally returns one either the unsharpened (linear light) result or the gamma 1 result. This may, or may not, be the best thing.
-----
P.S. Here is a very very simple scheme that gets rid of the "odd pixels". (Bart: Note that for the specific purpose of downsampling JPEGs or 8-bit sRGB PNGs, I've checked that my shortcuts are harmless).
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=50^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=50^,100% \
     -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  -evaluate-sequence median \
  -set colorspace RGB -colorspace sRGB altdownsample50.png

Thanks, would you convert to sRGB before down sampling (aka running above commands) or after?  If doing the conversion after the commands will result in sRGB RGB not completely correct as the gamma would be slightly different.
I supposse running everything in 16-bit will be more than sufficiënt?


Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 28, 2014, 05:19:45 pm
Alain:
My latest script, or the one with identical DoG parameters which I suspect, despite Bart van der Wolf's misgivings, may be slightly better and that I put below, assumes sRGB input. How to best deal with other types of input image depends on the specifics. See Alan Gibson and Bart van der Wolf's scripts for hints of how to handle other situations. Or use your favorite software to produce a 8 or 16 bit sRGB PNG or TIFF (I recommend Perceptual rendering intent over the others) and just replace input.jpg by input.png in what follows. Or ask for help at the ImageMagick User forums.
Here is the "same DoG parameters" version:
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.3333333333333333333 \) \
  -evaluate-sequence median \
  -set colorspace RGB -colorspace sRGB -quality 98 output.jpg
Warning: You realize that I'm using this Forum as a sandbox for new methods, right? Definitely no warranty. However feedback, esp. negative, is welcome.
Note: Recent ImageMagick turns off chroma downsampling if the quality is high enough (threshold is -quality 90). If you are using an old version, add the usual -sampling-factor 4:4:4.
P.S. At some point, I'll start adding credits to the source code. Everyone who contributed to Bart's version is of course "in it".
P.S. Bart: I strongly suspect taking the per channel median is better than the "old downsampling". Yes, I'm violating one of my earlier pronouncements. But the trade-off is worth it, it appears, in my limited testing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 28, 2014, 05:36:17 pm
I'm happy to help, where I can. You folks know vastly more about IM filters, convolution etc than I do. You are showing me a whole new world.

Quote from: BartvanderWolf
Also, in the 'old' version I force the processing to 16-bit precision from the start of the conversion with the '-depth 16' parameter, because ImageMagick tends to use the lowest amount of memory if possible (e.g. by using single channel images if 3-channel monochrome images are input), and may (or may not) do some of the processing in 8-bit/channel precision. I'd rather be over-cautious than sorry, and prefer to force bit-depth to 16, even for 8-b/ch input. When experimentation stabilizes, we can always test if it makes any difference for a JPEG workflow, but I want to keep the options for dealing with other input sources open for now.
IM V6 stores images in memory according to the Q-number and HDRI choice. "-depth N" has no effect on this. "-depth N" affects only output to files. Using "-depth N" earlier than required in the command should make no difference, positive or negative.

To reduce clipping when doing gamma operations etc, it is best to use a Q-number greater than the bit depth of input files. For 8-bit inputs, use Q16. For 16-bit inputs, use Q32. When using Q32, "-depth 16" will force the output bit-depth to be 16.

Quote from: NicolasRobidoux
P.S.2 Quick and dirty check of luminance drift. With "neutral" strenght of the DoG, namely 50, I saved the results into pngs, loaded into nip2, converted to XYZ, and compared the Y channel image mean with that of the input. The new scheme is a lot closer. Not conclusive. But encouraging.
The old scheme blended pixels, thus it could generate colours that weren't in any version of the down-sampled input. The new scheme (currently) picks each output pixel from one of the versions of the input, so can't invent new colours.

Another possibility for blending/tie-breaking would be to take, not a (weighted) average of the RGB values, but a (weighted) average of L*a*b* values. Where's that Aspirin?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 28, 2014, 06:09:28 pm
Alain:
My latest script, or the one with identical DoG parameters which I suspect, despite Bart van der Wolf's misgivings, may be slightly better and that I put below, assumes sRGB input. How to best deal with other types of input image depends on the specifics. See Alan Gibson and Bart van der Wolf's scripts for hints of how to handle other situations. Or use your favorite software to produce a 8 or 16 bit sRGB PNG or TIFF (I recommend Perceptual rendering intent over the others) and just replace input.jpg by input.png in what follows. Or ask for help at the ImageMagick User forums.
Here is the "same DoG parameters" version:
convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.3333333333333333333 \) \
  -evaluate-sequence median \
  -set colorspace RGB -colorspace sRGB -quality 98 output.jpg
Warning: You realize that I'm using this Forum as a sandbox for new methods, right? Definitely no warranty. However feedback, esp. negative, is welcome.
Note: Recent ImageMagick turns off chroma downsampling if the quality is high enough (threshold is -quality 90). If you are using an old version, add the usual -sampling-factor 4:4:4.
P.S. At some point, I'll start adding credits to the source code. Everyone who contributed to Bart's version is of course "in it".
P.S. Bart: I strongly suspect taking the per channel median is better than the "old downsampling". Yes, I'm violating one of my earlier pronouncements. But the trade-off is worth it, it appears, in my limited testing.

Thanks and I'm fully aware that it's a sandbox.  For what I'm doing is also no problem, mostly sandbox work to.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 28, 2014, 06:11:49 pm
I'm happy to help, where I can. You folks know vastly more about IM filters, convolution etc than I do. You are showing me a whole new world.
IM V6 stores images in memory according to the Q-number and HDRI choice. "-depth N" has no effect on this. "-depth N" affects only output to files. Using "-depth N" earlier than required in the command should make no difference, positive or negative.

To reduce clipping when doing gamma operations etc, it is best to use a Q-number greater than the bit depth of input files. For 8-bit inputs, use Q16. For 16-bit inputs, use Q32. When using Q32, "-depth 16" will force the output bit-depth to be 16.
The old scheme blended pixels, thus it could generate colours that weren't in any version of the down-sampled input. The new scheme (currently) picks each output pixel from one of the versions of the input, so can't invent new colours.

Another possibility for blending/tie-breaking would be to take, not a (weighted) average of the RGB values, but a (weighted) average of L*a*b* values. Where's that Aspirin?

Thanks for the clarification.  I'm I right that there's no Q32 binary for windows?  Not a real need if the output is 8-bit jpg ;-)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 28, 2014, 06:38:35 pm
The developers don't release a prebuilt Q32. But it is easy to build yourself, using Cygwin. See my page http://im.snibgo.com/compim.htm
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 03:42:39 am
...
The old scheme blended pixels, thus it could generate colours that weren't in any version of the down-sampled input. The new scheme (currently) picks each output pixel from one of the versions of the input, so can't invent new colours.
...
Alan:
Clarification: The new scheme (currently) picks each output pixel from one of the resized versions of the input, so can invent new colours.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 03:50:43 am
Bart:
I understand, I believe, the heuristic that justifies using different DoGs for different gammas. I am more concerned, however, about how the two results interact than I am about each of them being "as good as possible" in isolation. Roughly speaking, I see the gamma 3 deconvolution result as a correction of the gamma 1 deconvolution result (and now, as a corrections of the non-deconvolved one).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 04:03:46 am
Side comment: The libvips GUI Nip2 now contains ImageMagick. Installs on "everything". http://libvips.blogspot.dk/2011/06/using-imagemagick-from-nip2.html (http://libvips.blogspot.dk/2011/06/using-imagemagick-from-nip2.html)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 04:25:25 am
Here is a minimally modified version of Bart's script that differs as follows:
In the "downsample" method, the same DoG is used for both gammas, and the per channel median of the unsharpened, gamma 1 sharpened and gamma 3 sharpened is used to "blend".
I see some evidence that this may be the best method so far, at least in terms of taming haloing without introducing occasional outliers.
(No windows machine: Feel free to fix my direct *n*x translation.)
P.S. Apparently I changed the encoding or something like this. Hopefully I got the Windowese right this time. Just in case, I am cutting and pasting the alt bat file here (I think I'm still missing the CTRL-Ms):
@ECHO OFF

REM Edit the codeline below to point to the location of the most recent version of
REM ImageMagick on your computer if the correct convert command is not recognized.
REM SET PATH=C:\Program Files\ImageMagick-6.8.9-Q16;%PATH%

ECHO *****************************************
ECHO **          IMAGE RESAMPLING           **
ECHO *****************************************
ECHO * Halo minimization by Gamma blending,  *
ECHO *     as suggested by Nicolas Robidoux. *
ECHO *                                       *
ECHO * Author : Bart van der Wolf            *
ECHO *                                       *
ECHO * Suggestions for code optimization:    *
ECHO *   Alan Gibson, Nicolas Robidoux,      *
ECHO *   Fred Weinhaus                       *
ECHO *                                       *
ECHO * Version: 1.2.2                        *
ECHO * Date   : 2014.08.12                   *
ECHO *                                       *
ECHO * The Author(s) of this free tool shall *
ECHO * not be liable for any damages that    *
ECHO * might result from the use of this     *
ECHO * tool. Feel free to not use it.        *
ECHO *****************************************

REM Ignore this for the moment, it's unfinished, for future support when dragging/dropping multiple files
REM FOR %%I IN (*.GIF *.JPG *.JPEG *.PNG *.TIF *.TIFF)
REM

:imsize
ECHO.
SET mSize="?"
ECHO Input the magnification percentage (e.g. 50%%), or fit
ECHO inside the requested pixel dimensions (e.g. 800x800,
SET /p mSize= or a max width 800x, or a max height x800):
IF %mSize% EQU "?" SET mSize=800x800
IF %mSize% EQU "" GOTO imsize

:method
ECHO.
SET UpDwn=d
SET /p UpDwn= Use an optimized Up- or Down-sampling method? [U]p, [D]own:
IF /i %UpDwn% GEQ d IF /i %UpDwn% LEQ down GOTO downsample
IF /i %UpDwn% GEQ u IF /i %UpDwn% LEQ up GOTO generic
GOTO method

:generic
SET deblurAmount=50
SET /p deblurAmount= Sharpening amount? (none=0, default/normal=50, high=100+):
for /f "usebackq" %%L in (`identify ^
 -precision 16 -format "deblurValue=%%[fx:1-0.0011450938298236*%deblurAmount%]" ^
 xc:`) do set %%L
REM IF /i %deblurAmount% EQU -1 GOTO generic

ECHO.
ECHO Creating the resampled image, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 3 -define filter:blur=%deblurValue% -filter Lanczos -distort Resize %mSize% -gamma 0.3333333333333333 ) ^
 ( -clone 0 -define filter:blur=%deblurValue% -filter Lanczos -distort Resize %mSize% ) ^
 -delete 0 ^
 ( -clone 1 -colorspace gray -auto-level ) ^
 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

REM Experimental
IF /i %deblurAmount% LEQ 0 GOTO addDeconv

GOTO finish

:downsample
SET sharpAmount="?"
SET /p sharpAmount= Sharpening amount? (none=0, normal=50+, default=100):
IF /i %sharpAmount% == 0 GOTO nodownsharp
IF %sharpAmount% EQU "?" SET sharpAmount=100
ECHO.
ECHO Creating the Down-sampled image with output sharpening, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ^
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize %mSize% ) ^
 ( -clone 0 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:3,0,0.4806768770037563 ) ^
 ( -clone 0 -gamma 3 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.3333333333333333333 ) ^
 -evaluate-sequence median ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -define png:preserve-iCCP ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

REM Experimental
IF /i %sharpAmount% LEQ 0 GOTO addDeconv

GOTO finish
 
:nodownsharp
ECHO.
ECHO Creating the Down-sampled image without additional sharpening, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ^
 -filter Quadratic -distort Resize %mSize% ) ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"
 
REM Experimental
GOTO addDeconv

GOTO finish

REM *** DECONVOLUTION ***
:addDeconv
ECHO.
SET Deconv=n
SET /p Deconv= Apply additional deconvolution sharpening? [Y]es, or default [N]o:
IF /i %Deconv% GEQ y IF /i %Deconv% LEQ yes GOTO deconvolve
IF /i %Deconv% GEQ n IF /i %Deconv% LEQ no GOTO finish
GOTO addDeconv
:deconvolve
SET detailSize=0
SET /p detailSize= Blur size in pixels (e.g. blurred edge width, radius of blur):
IF /i %detailSize% LEQ 0 GOTO deconvolve
SET sharpAmount=50
SET /p sharpAmount= Sharpening amount? (none=0, default/normal=50, high=100+):
:noDeconvDialog
for /f "usebackq" %%L in (`identify ^
 -precision 16 -format "blurSigma=%%[fx:max(0.4,0.3902*%detailSize%)]" ^
 xc:`) do set %%L
ECHO.
ECHO Deconvolving the image, please wait ...
convert ( -quiet "%~dpn1_%mSize%%~x1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 3 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:0,0,%blurSigma% -gamma 0.3333333333333333 ) ^
 ( -clone 0 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:0,0,%blurSigma% ) ^
 -delete 0 ^
 ( -clone 1 -colorspace gray -auto-level ) ^
 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

GOTO finish
 
:finish
REM Clear environment variables
SET blurSigma=
SET deblurAmount=
SET deblurValue=
SET Deconv=
SET detailSize=
SET mSize=
SET sharpAmount=
SET UpDwn=
REM Delete temporary profile copy
IF EXIST "%~dp1IMsource.icc" DEL "%~dp1IMsource.icc"

ECHO.
ECHO *************************************
ECHO *** Your resampled file is Ready! ***
ECHO *************************************
ECHO.

PAUSE
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: snibgo on September 29, 2014, 05:02:35 am
Quote from: NicolasRobidoux
Clarification: The new scheme (currently) picks each output pixel from one of the resized versions of the input, so can invent new colours.
Yes. Sorry I wasn't clear.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 05:03:56 am
Bart: The last graph of http://blog.kasson.com/?p=7296 (http://blog.kasson.com/?p=7296), obtained with EWA RobidouxSoft, LWGB and your chosen pair of DoG parameters, blows my mind.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 07:20:00 am
Bart: I think it would be a good idea to add "tensor Triangle" downsampling (-filter Triangle -resize) through linear light to your script, so that people easily see what a fairly neutral (as far as color preservation) scheme does (at least when the input is sRGB).
-----
I am immensely thankful that you picked up on this project. I'm not sure I would have thought of tweaking the post-sharpening so it "fixes" the blurring introduced by an antialiasing EWA method. (Even though it's kind of obvious, at least in hindsight, if you look at things through the right lens.) It was off my radar.
And you brought an immense amount of know how and momentum, without which the project would have moved at a snail's pace.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 09:04:21 am
It will take some new ideas to eliminate the lightening of high frequency structures.
Here is why, as far as I can tell:
DoG through gamma light lightens high frequency structures.
Now, if you combine DoG through gamma > 1 with two gamma 1 results, the lightening introduced by the high gamma DoG will "bleed through".
At this point this bleed through can be reduced. But I don't know how to completely get rid of it without increasing halos.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 29, 2014, 11:50:44 am
I've compared the average of the Y channel of the fly downsampled with bilinear through linear light to the "per channel median" variant at sharpening = 50, and 50 is not too bad in the drift department and keeps moire under wraps. Increasing the sharpening adds moire, lightens the eyes and raises the average of the Y channel.
"Sharp" has a price. (What a surprise.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 30, 2014, 05:36:42 am
Here is what is hopefully a sufficient fix of the "high frequency detail lightening": use gamma 1.25 instead of gamma 3.
This version does not suppress dark halos as much as earlier ones, but I think that, when one does not pixel peep, it may suppress them enough. No free hot lunch: If you want less halos, push the gamma up from 1.25 (and fix its reciprocal). If you want more "lightness preservation" in high frequency areas, push the gamma down toward 1 (and fix its reciprocal).
This runs considerably more slowly than Bart's original, because it uses a rank filter, and rank filters are slow.
Hopefully the zip file has the right line endings for Windows. Just in case, here is the code:
@ECHO OFF

REM Edit the codeline below to point to the location of the most recent version of
REM ImageMagick on your computer if the correct convert command is not recognized.
REM SET PATH=C:\Program Files\ImageMagick-6.8.9-Q16;%PATH%

ECHO *****************************************
ECHO **          IMAGE RESAMPLING           **
ECHO *****************************************
ECHO * Halo minimization by Gamma selection  *
ECHO *     as suggested by Nicolas Robidoux. *
ECHO *                                       *
ECHO * Authors: Bart van der Wolf,           *
ECHO *          Nicolas Robidoux             *
ECHO *                                       *
ECHO * Suggestions for code optimization:    *
ECHO *   Alan Gibson, Fred Weinhaus          *
ECHO *                                       *
ECHO * Version: alt1                         *
ECHO * Date   : 2014.09.30                   *
ECHO *                                       *
ECHO * The Author(s) of this free tool shall *
ECHO * not be liable for any damages that    *
ECHO * might result from the use of this     *
ECHO * tool. Feel free to not use it.        *
ECHO *****************************************

REM Ignore this for the moment, it's unfinished, for future support when dragging/dropping multiple files
REM FOR %%I IN (*.GIF *.JPG *.JPEG *.PNG *.TIF *.TIFF)
REM

:imsize
ECHO.
SET mSize="?"
ECHO Input the magnification percentage (e.g. 50%%), or fit
ECHO inside the requested pixel dimensions (e.g. 800x800,
SET /p mSize= or a max width 800x, or a max height x800):
IF %mSize% EQU "?" SET mSize=800x800
IF %mSize% EQU "" GOTO imsize

:method
ECHO.
SET UpDwn=d
SET /p UpDwn= Use an optimized Up- or Down-sampling method? [U]p, [D]own:
IF /i %UpDwn% GEQ d IF /i %UpDwn% LEQ down GOTO downsample
IF /i %UpDwn% GEQ u IF /i %UpDwn% LEQ up GOTO generic
GOTO method

:generic
SET deblurAmount=50
SET /p deblurAmount= Sharpening amount? (none=0, default/normal=50, high=100+):
for /f "usebackq" %%L in (`identify ^
 -precision 16 -format "deblurValue=%%[fx:1-0.0011450938298236*%deblurAmount%]" ^
 xc:`) do set %%L
REM IF /i %deblurAmount% EQU -1 GOTO generic

ECHO.
ECHO Creating the resampled image, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 3 -define filter:blur=%deblurValue% -filter Lanczos -distort Resize %mSize% -gamma 0.3333333333333333 ) ^
 ( -clone 0 -define filter:blur=%deblurValue% -filter Lanczos -distort Resize %mSize% ) ^
 -delete 0 ^
 ( -clone 1 -colorspace gray -auto-level ) ^
 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

REM Experimental
IF /i %deblurAmount% LEQ 0 GOTO addDeconv

GOTO finish

:downsample
SET sharpAmount="?"
SET /p sharpAmount= Sharpening amount? (none=0, normal=50+, default=100):
IF /i %sharpAmount% == 0 GOTO nodownsharp
IF %sharpAmount% EQU "?" SET sharpAmount=100
ECHO.
ECHO Creating the Down-sampled image with output sharpening, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ^
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize %mSize% ) ^
 ( -clone 0 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:3,0,0.4806768770037563 ) ^
 ( -clone 0 -gamma 1.25 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.8 ) ^
 -evaluate-sequence median ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -define png:preserve-iCCP ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

REM Experimental
IF /i %sharpAmount% LEQ 0 GOTO addDeconv

GOTO finish
 
:nodownsharp
ECHO.
ECHO Creating the Down-sampled image without additional sharpening, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ^
 -filter Quadratic -distort Resize %mSize% ) ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"
 
REM Experimental
GOTO addDeconv

GOTO finish

REM *** DECONVOLUTION ***
:addDeconv
ECHO.
SET Deconv=n
SET /p Deconv= Apply additional deconvolution sharpening? [Y]es, or default [N]o:
IF /i %Deconv% GEQ y IF /i %Deconv% LEQ yes GOTO deconvolve
IF /i %Deconv% GEQ n IF /i %Deconv% LEQ no GOTO finish
GOTO addDeconv
:deconvolve
SET detailSize=0
SET /p detailSize= Blur size in pixels (e.g. blurred edge width, radius of blur):
IF /i %detailSize% LEQ 0 GOTO deconvolve
SET sharpAmount=50
SET /p sharpAmount= Sharpening amount? (none=0, default/normal=50, high=100+):
:noDeconvDialog
for /f "usebackq" %%L in (`identify ^
 -precision 16 -format "blurSigma=%%[fx:max(0.4,0.3902*%detailSize%)]" ^
 xc:`) do set %%L
ECHO.
ECHO Deconvolving the image, please wait ...
convert ( -quiet "%~dpn1_%mSize%%~x1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ) ^
 ( -clone 0 -gamma 3 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:0,0,%blurSigma% -gamma 0.3333333333333333 ) ^
 ( -clone 0 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:0,0,%blurSigma% ) ^
 -delete 0 ^
 ( -clone 1 -colorspace gray -auto-level ) ^
 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

GOTO finish
 
:finish
REM Clear environment variables
SET blurSigma=
SET deblurAmount=
SET deblurValue=
SET Deconv=
SET detailSize=
SET mSize=
SET sharpAmount=
SET UpDwn=
REM Delete temporary profile copy
IF EXIST "%~dp1IMsource.icc" DEL "%~dp1IMsource.icc"

ECHO.
ECHO *************************************
ECHO *** Your resampled file is Ready! ***
ECHO *************************************
ECHO.

PAUSE
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on September 30, 2014, 11:31:26 am
I added the batch script as an external editor in LR5.6 and that works. You still have to add the new ima ge to the catalog via synchronise folder f.i.

I am now testing to see if uprezzing with this tool gives better results for printing than the uprezzing of the print function in LR. I am playing with different quality images.
Looking at the histogram, there is little difference, also the softproofing shows practically the same oog areas. Also visually on screen there is no directly visible change in lightness etc.
I did a few testprints. So  far little visible quality gain if any, perhaps a bit snappier is one case.

Advantage though is that you can fine tune the uprezzed image. This is definitely an advantage that can be worthwhile. One of the images tested is a rather noisy image, and by applying all corrections except full capture sharpening, but with good noice reduction. Then sharpening completed after uprezzing, gave a better result. Not by much, but visible.

Will post more detailed findings when done.

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Cem on September 30, 2014, 12:05:27 pm
I added the batch script as an external editor in LR5.6 and that works. You still have to add the new ima ge to the catalog via synchronise folder f.i.

Hi Jan,

This is a great idea. It can be further improved by skipping the re-import/synchronize step as follows:

1) Search the text "%~dpn1_%mSize%%~x1" in the bat file (occurs multiple times) and replace all of them with "%~dpn1%~x1".

2) Afterwards, when processing a file from within LR, just select to "edit a copy" (instead of the original in case it is a tif or jpg file).

3) This will create a new file using your chosen rename scheme and the output file from the bat process will just override the copy file. LR will then display the end result without having to re-import or synchronize.

Hope this help. :)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 30, 2014, 12:58:07 pm
Here is what is hopefully a sufficient fix of the "high frequency detail lightening": use gamma 1.25 instead of gamma 3.
This version does not suppress dark halos as much as earlier ones, but I think that, when one does not pixel peep, it may suppress them enough.

Hi Nicolas,

While subject to taste, I do not like the deeper undershoot halos that result from the reduced gamma.

Quote
No free hot lunch: If you want less halos, push the gamma up from 1.25 (and fix its reciprocal). If you want more "lightness preservation" in high frequency areas, push the gamma down toward 1 (and fix its reciprocal).

Yes, it's up to the user to pick his/her own poison, although for a balanced amount of under- / over-shoot halo with boosted sharpening after returning to the original gamma, it's hard to beat the original script Version 1.2.2 settings (given the restraints of 16-b/ch processing). The reduced gamma settings will clip dark side halos pretty quickly when sharpening is increased. Only floating point processing might allow to repair some of that before saving the output.

Quote
This runs considerably more slowly than Bart's original, because it uses a rank filter, and rank filters are slow.

With the original gamma settings, there is virtually no difference between the outputs. So I'm a bit doubtful if it really helps to use this slower method, although I do like the simplicity of clamping with an upper and lower bounds version through Median averaging.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 30, 2014, 01:05:38 pm
Jan, Cem,

Good thinking! Might be very usable for many more people who have a Lightroom oriented workflow.

Of course the script, as it is, is intended to be very flexible. When things stabilize, it would be possible to simplify things for certain types of output (e.g. down-sampling or upsampling centric, with preferred settings for sharpening being hard-coded).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on September 30, 2014, 03:09:16 pm
Hi Jan,

This is a great idea. It can be further improved by skipping the re-import/synchronize step as follows:

1) Search the text "%~dpn1_%mSize%%~x1" in the bat file (occurs multiple times) and replace all of them with "%~dpn1%~x1".

2) Afterwards, when processing a file from within LR, just select to "edit a copy" (instead of the original in case it is a tif or jpg file).

3) This will create a new file using your chosen rename scheme and the output file from the bat process will just override the copy file. LR will then display the end result without having to re-import or synchronize.

Hope this help. :)
Cem, thanks for the tip. Removes one step from this activity.

A first test was an image well captured, low noise, modest capture sharpening applied. I uprezzed a little over 2 times, sharpening 50 .  Cropped both the source tiff and uprezzed tiff , a same area (using auto sync in LR). Then printed both with the same print size. Source tiff at 170 ppi and uprezzed at360 ppi. Printed on hahnemuhle photo silk baryta with an epson4900 set at 360ppi. Prints are indistinguishable visually. So no b enefits . Perhaps the 360 ppi is the limiting factor.
Next test will l be at 720ppi with fine details on. With enough pixels this setting usually shows better resolution.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 30, 2014, 03:41:40 pm
Looking at the histogram, there is little difference, also the softproofing shows practically the same oog areas. Also visually on screen there is no directly visible change in lightness etc.
...
The "lightening of high frequency areas" affects some of the schemes when downsampling. I don't expect it to be an issue when upsampling.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on September 30, 2014, 03:52:34 pm
...
With the original gamma settings, there is virtually no difference between the outputs. So I'm a bit doubtful if it really helps to use this slower method, although I do like the simplicity of clamping with an upper and lower bounds version through Median averaging.
We're beating around one bush. At this point, I don't expect a "Wow!" improvement to what we have already. Just pushing the envelope a little.
This being said, I really don't like the lightening of high freq. content when downsampling. Staying away from pixel peeping, it is my (changeable) opinion that this is more of an issue than dark halos.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 30, 2014, 05:56:41 pm
We're beating around one bush. At this point, I don't expect a "Wow!" improvement to what we have already. Just pushing the envelope a little.
This being said, I really don't like the lightening of high freq. content when downsampling. Staying away from pixel peeping, it is my (changeable) opinion that this is more of an issue than dark halos.

Nicolas, I'm not quite sure what lightening you are seeing. Do you have a specific example? Maybe we are talking about something different, and are thus not focusing on the same issue that should be addressed?

When you try and down-sample e.g. one of Norman Koren's targets, in particular this one (http://www.normankoren.com/Tutorials/Lenstarg_50_5906p_20g_0is.png), and down-sample it to 25%, it should show how a sinusoidal (and for the masochists a bi-tonal) progressive grating of increasingly higher spatial frequencies will perform all the way past the Nyquist frequency. That should give a repeatable image for testing different scenarios.

Alternatively, a zoneplate 'rings (https://www.dropbox.com/s/e3wmc4lqxervncb/Rings.png)' target does a similar thing in all angles, even at modest down-sampling percentages (because the original has 0.5 cycles/pixel diagonal resolution in the corners). Anything below 70% size will challenge the aliasing suppression in all directions. I could make a lower amplitude version if under- / over-shoots need to be quantified at lower spatial frequencies.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 30, 2014, 06:11:34 pm
Cem, thanks for the tip. Removes one step from this activity.

A first test was an image well captured, low noise, modest capture sharpening applied. I uprezzed a little over 2 times, sharpening 50 .  Cropped both the source tiff and uprezzed tiff , a same area (using auto sync in LR). Then printed both with the same print size. Source tiff at 170 ppi and uprezzed at360 ppi. Printed on hahnemuhle photo silk baryta with an epson4900 set at 360ppi. Prints are indistinguishable visually. So no b enefits . Perhaps the 360 ppi is the limiting factor.

Hi Jan,

Thanks for the feedback. The upsampling algorithm attempts to avoid creating artifacts, while maintaining as much of the original resolution as possible. It does not create additional resolution, so a source of 170 PPI will remain just that.

You can also experiment with initial '0' sharpening, which then allows to add your own deconvolution radius and amount, instead of using the built-in modification of the resampling window to 'sharpen'the image.

Quote
Next test will l be at 720ppi with fine details on. With enough pixels this setting usually shows better resolution.

That should allow to add a higher amount of a relatively modest radius deconvolution.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 01, 2014, 04:28:40 am
Nicolas, I'm not quite sure what lightening you are seeing. Do you have a specific example? Maybe we are talking about something different, and are thus not focusing on the same issue that should be addressed?
...
I'm talking about this:
First, the results with sharpening 50 and 100 with "Bart's downsampling scheme" (EWA RobidouxSoft through linear RGB followed by sharpening with one tuned DoG for gamma 1 and one for gamma 3, auto-level luminance blended), and "Nicolas' tweak" (EWA RobidouxSoft through linear RGB followed by sharpening in gamma 1 and gamma 3 using the DoG tuned for gamma 1, followed by per channel median; I'm using gamma 3, which I now think is overdoing it, so as to compare apples to apples) applied to the fly http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg (http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg).
convert \( input.jpg -set colorspace sRGB -colorspace RGB \
  -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -gamma 3 -define convolve:scale=50^,100% \
  -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -define convolve:scale=50^,100% \
  -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  -delete 0 \
  \( -clone 1 -colorspace gray -auto-level \) \
  -compose over -composite \
  -set colorspace RGB -colorspace sRGB LWGBautolevelGamma3s50.png

convert \( input.jpg -set colorspace sRGB -colorspace RGB \
  -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
  -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 \) \
  \( -clone 0 -define convolve:scale=100^,100% \
  -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  -delete 0 \
  \( -clone 1 -colorspace gray -auto-level \) \
  -compose over -composite \
  -set colorspace RGB -colorspace sRGB LWGBautolevelGamma3s100.png

convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
    \( -clone 0 -define convolve:scale=50^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=50^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.33333333333333333 \) \
  -evaluate-sequence median \
  -set colorspace RGB -colorspace sRGB MGBgamma3s50.png

convert \
  \( input.jpg -set colorspace sRGB -colorspace RGB \
     -define filter:c=0.1601886205085204 -filter Cubic -distort Resize 25\% \) \
    \( -clone 0 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 \) \
  \( -clone 0 -gamma 3 -define convolve:scale=100^,100% \
     -morphology Convolve DoG:3,0,0.4806768770037563 -gamma 0.33333333333333333 \) \
  -evaluate-sequence median \
  -set colorspace RGB -colorspace sRGB MGBgamma3s100.png
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 01, 2014, 04:34:36 am
Now, the results of downsampling with schemes that should preserve the lightness in high frequency areas better: Bilinear done correctly, EWA Quadratic, EWA Robidoux and EWA RobidouxSharp:
convert \
    input.jpg -set colorspace sRGB -colorspace RGB \
    -filter Triangle -resize 25\% \
    -set colorspace RGB -colorspace sRGB bilinear.png

convert \
    input.jpg -set colorspace sRGB -colorspace RGB \
    -filter Quadratic -distort Resize 25\% \
    -set colorspace RGB -colorspace sRGB EWAquadratic.png

convert \
    input.jpg -set colorspace sRGB -colorspace RGB \
    -distort Resize 25\% \
    -set colorspace RGB -colorspace sRGB EWArobidoux.png

convert \
    input.jpg -set colorspace sRGB -colorspace RGB \
    -filter RobidouxSharp -distort Resize 25\% \
    -set colorspace RGB -colorspace sRGB EWArobidouxSharp.png
Look at the eyes or measure the average luminance in a patch. Some of this is probably caused by the sharpening "bottoming out" less than it "tops up" but my guess is that at least some of it comes from mixing gamma 3 in.
It looks to me like the halo suppression comes with a subtle form of moire.
P.S. Hopefully it's not a side effect of using a Q16 version of ImageMagick without care or worry. If so, I'll feel like a fool.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 01, 2014, 06:34:15 am
...
When you try and down-sample e.g. one of Norman Koren's targets, in particular this one (http://www.normankoren.com/Tutorials/Lenstarg_50_5906p_20g_0is.png), and down-sample it to 25%, it should show how a sinusoidal (and for the masochists a bi-tonal) progressive grating of increasingly higher spatial frequencies will perform all the way past the Nyquist frequency. That should give a repeatable image for testing different scenarios.
...
First, with the gamma 3 versions of Bart's (LWGB...) and Nicolas' (MGB...).
(Hopefully I'm not violating copyright law by posting downsampled versions. Let me know if I'm wrong and I'll take the results down.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 01, 2014, 06:35:39 am
... now with more neutral schemes.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 01, 2014, 09:57:04 am
First, with the gamma 3 versions of Bart's (LWGB...) and Nicolas' (MGB...).

And if we make a profile plot through the sinusoidal grating we get something like this (see attachments).

As can be seen, due to blending of unresolvable pixels in linear light, the resulting average amplitude in '2.2 gamma space' (actually 1/2.2, or gamma 2.2 pre-compensated) is higher than the plain average between minimum and maximum.

What also can be seen is that deconvolution with an amount of 50 allows to maintain higher resolutions (higher spatial frequencies) with the restored maximum amplitude of the original input. There comes a point where resolution will diminish as we come close to the Nyquist frequency where amplitude goes to zero, and at even higher spatial frequencies we see some aliasing artifacts (which will fold back (mirror) to lower spatial frequencies as aliases).

We can also see that at a deconvolution amount of 50, the highest non-halo adding level, there is little difference between the luminance blending and the median blending approaches. At a higher deconvolution amount, which will add some haloing (predictable by filter design), we also see that Median blending fails to use most of the sharpened versions, and takes the unsharpened middle ground, and thus is less effective in sharpness restoration. The average amplitude level remains at the same elevated level as the others, because that is caused by linear light blending of unresolvable pixels.

Because of the less effective sharpening, the maximum amplitudes are lower (which could be interpreted as less lightening, but is actually just a lower maximum amplitude, also at the minimum values which become lighter).

So all effects can be attributed to less effective restoration of the original input signal amplitudes near high spatial frequencies.

Cheers,
Bart



Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 01, 2014, 10:10:00 am
For those who are not familiar with such a profile plot, this (see attachment) is how the original/input file looks, given that not enough pixels were plotted on the horizontal axis. Important is to note that the amplitudes of the input signal is basically maximum for all spatial frequencies. Ideally, although not achievable without creating ugly artifacts, we aim to keep as much of the original input amplitude in our down-sampled images as possible. It is inevitable that as we approach the limiting spatial frequencies near the Nyquist frequency, the amplitude starts to fall to zero amplitude, a flat average.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 01, 2014, 10:45:57 am
... now with more neutral schemes.

Bilinear has overall lower amplitudes, produces duller images with reduced (micro-)contrast, but with the same average amplitude as the more complex methods.
EWAQuadratic has slightly lower amplitudes than Bilinear (IM Triangle filter), but also lower amplitude of aliasing.
EWARobidoux has significantly higher amplitudes at higher spatial frequencies (and aliasing), and we know that the different Keys Cubic filter produces a different trade-off between resolution and artifacts.
EWArobidouxSharp with different Keys Cubic filter settings has better preserved amplitudes (is much sharper) than the others, but also with more artifacts.

The trouble with the aliasing artifacts is that they get mixed in with the actual signal levels, and can afterwards not be separated to reduce them. That's why I have chosen a filter/blending method that produces low levels of artifacts, and allows significant restoration of detail (with lower levels of aliasing in the mix).

Cheers,
Bart

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on October 01, 2014, 11:32:38 am
Hi Jan,

Thanks for the feedback. The upsampling algorithm attempts to avoid creating artifacts, while maintaining as much of the original resolution as possible. It does not create additional resolution, so a source of 170 PPI will remain just that.

You can also experiment with initial '0' sharpening, which then allows to add your own deconvolution radius and amount, instead of using the built-in modification of the resampling window to 'sharpen'the image.

That should allow to add a higher amount of a relatively modest radius deconvolution.

Cheers,
Bart
What would be a good tool for deconv sharpening?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 01, 2014, 11:54:34 am
What would be a good tool for deconv sharpening?

The script file will offer a simple one-pass deconvolution when sharpening is initially set to '0' or less (negative sharpening amounts will blur).

For superior deconvolution quality you can use a PS plugin like FocusMagic. But you can also use TopazLabs 'Detail', for detail enhancement when there is not enough native resolution to challenge the printer.

Remember, deconvolution is basically a restoration tool, so it can restore some of the seemingly lost resolution, but it does not invent new detail (unless pushed too far where it will start producing halo artifacts). Topaz Detail does not produce halo artifacts, but it selectively boosts detail amplitude to mimic resolution, which perceptually is very effective due to the absence of halos.

Then there are tools like 'Photozoom Professional' and 'Perfect Resize' which add detail and effectively increases resolution of edges to higher levels than available in the source image. Care must be taken to not exaggerate the edge resolution because that will look artificial when there is no structural/surface detail to match the edges.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 01, 2014, 01:51:34 pm
I'm talking about this:
First, the results with sharpening 50 and 100 with "Bart's downsampling scheme" (EWA RobidouxSoft through linear RGB followed by sharpening with one tuned DoG for gamma 1 and one for gamma 3, auto-level luminance blended), and "Nicolas' tweak" (EWA RobidouxSoft through linear RGB followed by sharpening in gamma 1 and gamma 3 using the DoG tuned for gamma 1, followed by per channel median; I'm using gamma 3, which I now think is overdoing it, so as to compare apples to apples) applied to the fly http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg (http://upload.wikimedia.org/wikipedia/commons/8/85/Calliphora_sp_Portrait.jpg).

Attached is a crop animation of the resampled and the original version. The down-sampled version was then upsampled again (by using Nearestneighbor) to approx. align with the original version as a layer. This upsampling allows to avoid effects caused by our human visual system, and reduces (but not eliminates) the risk of gamma effects from viewing the image at non-native (100% zoom) size. There is also a slightly lower risk of the gamma effect of viewing LCD displays from the 'wrong' angle.

Do note, that it should be viewed at a 100% zoom level (use a dedicated image viewer/editor, because a browser may still resize even at 100% setting!), and  from the correct angle to avoid gamma shift by some LCD display types. While there is a bit of luminance shift, it is both in the highlights and in the shadows of high frequency detail in the faceted eyes.

There is no significant lightening other than specific features that align with the pixel grid, and gamma effects as described above.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 02, 2014, 03:58:44 am
Bart:
Trying to sort things out at the perceptual level, it appears to me that EWA RobidouxSharp through linear RGB is generally both sharper and (this is the important bit) has less noticeable haloing than LWGBautolevelGamma3s100. And yet it introduces less moire.
EWA Mitchell through linear RGB, for example, looks really good in the sharpness/halo ratio department. Nice balanced scheme.
Am I missing something? (Away from my large calibrated monitor, but I don't think it matters.)
P.S. OK. Maybe LWGBautolevelGamma3s100 is a nudge sharper. It's less sharp than EWA CatRom through linear light, though, and CatRom does not seem to add an amount of haloing that matches the additional sharpness.
What I don't see is obvious advantages of the new downsampling methods over simply pushing the Keys alpha up from 0 until the sharpness is sufficient.
Again: Am I missing something? (I hope I am.)
(I reserve my opinion w.r.t. upsampling. Different game.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on October 02, 2014, 05:06:12 am
Bilinear has overall lower amplitudes, produces duller images with reduced (micro-)contrast, but with the same average amplitude as the more complex methods.
EWAQuadratic has slightly lower amplitudes than Bilinear (IM Triangle filter), but also lower amplitude of aliasing.
EWARobidoux has significantly higher amplitudes at higher spatial frequencies (and aliasing), and we know that the different Keys Cubic filter produces a different trade-off between resolution and artifacts.
EWArobidouxSharp with different Keys Cubic filter settings has better preserved amplitudes (is much sharper) than the others, but also with more artifacts.

The trouble with the aliasing artifacts is that they get mixed in with the actual signal levels, and can afterwards not be separated to reduce them. That's why I have chosen a filter/blending method that produces low levels of artifacts, and allows significant restoration of detail (with lower levels of aliasing in the mix).

Cheers,
Bart

Bart, interesting chart.  What does bicubic (no sharp) look like?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 02, 2014, 06:25:28 am
Bart:
Trying to sort things out at the perceptual level, it appears to me that EWA RobidouxSharp through linear RGB is generally both sharper and (this is the important bit) has less noticeable haloing than LWGBautolevelGamma3s100. And yet it introduces less moire.
EWA Mitchell through linear RGB, for example, looks really good in the sharpness/halo ratio department. Nice balanced scheme.
Am I missing something? (Away from my large calibrated monitor, but I don't think it matters.)

Hi Nicolas,

After my initial investment in time, I tried to avoid changing too many things too fast. I preferred waiting for some user feedback, likes, dislikes, the dust to settle as it were. It's just a case of not trying to aim at a moving target until it slows down enough, to take a shot, and then aim at the next target.

As I said in an earlier post (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg764816#msg764816), I'd like to test more Keys Cubic settings if time allows. However, until the deconvolution (if any) is automated, it takes a lot of time to do right. I therefore wanted to optimize the setting that you found to be useful (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=19823&start=30#p109820) (for 'minimizing the worst case deviation from the original value'), before benchmarking against (less robust?) alternatives, and also use the insights that were gained in the course of experimentation and code-optimization.

Quote
P.S. OK. Maybe LWGBautolevelGamma3s100 is a nudge sharper. It's less sharp than EWA CatRom through linear light, though, and CatRom does not seem to add an amount of haloing that matches the additional sharpness.
What I don't see is obvious advantages of the new downsampling methods over simply pushing the Keys alpha up from 0 until the sharpness is sufficient.
Again: Am I missing something? (I hope I am.)

We'll have to see how well they perform when scrutinized for aliasing(!)/ringing/blocking behavior (which may also affect rendering of down-sampled noise), and the risk of clipping due to halo under- / over-shoots. I would think that a user controllable Keys alpha input (on a scale from soft to sharp/crisp, or 0% to 100%) to adjust the behavior of the resampling filter should be a parameter for a modern resizing tool. Of course, the negative side effects (from softness to artifacts) should also be minimized in the background.

Quote
(I reserve my opinion w.r.t. upsampling. Different game.)

Absolutely, different game, different rules.

Cheers,
Bart

P.S. Different values for Keys alpha will not only require different deconvolution filter values, but also different gammas than 3.0 for a balanced halo under- / over-shoot. High alpha values will require significantly higher gammas, which may be risky in a non-floating point processing environment, but more experimentation will be required to make sure.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 02, 2014, 08:19:02 am
...
After my initial investment in time, I tried to avoid changing too many things too fast. I preferred waiting for some user feedback, likes, dislikes, the dust to settle as it were. It's just a case of not trying to aim at a moving target until it slows down enough, to take a shot, and then aim at the next target.
...
Ah OK. I get it: I'm impatient. That's what I was missing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on October 02, 2014, 09:38:11 am
(I reserve my opinion w.r.t. upsampling. Different game.)
Would like to see this "game" further pushed forward. Can act as tester, my focus is on cases where large prints are needed, thus need to uprez images.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 02, 2014, 11:50:28 am
Would like to see this "game" further pushed forward. Can act as tester, my focus is on cases where large prints are needed, thus need to uprez images.

Hi Jan,

As it is, the upsampling routines attempt to 'do (very) well' (in avoiding nasty/distracting artifacts in final output, yet remaining relatively sharp in image detail without 'jaggies') on a variety of subject matter (see some examples here (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935)). Of course, it would probably be possible to tweak a bit more for specific image content, so feel free to suggest (provide a small crop of something that you hope can be done better than your current workflow offers) what to consider.

And as always, the better the input image is (i.e. not over-sharpened), the better the output will be. Otherwise we will only enlarge the artifacts that were already there, accurately...

Also consider that there may be alternative, more involved, applications that can do a bit better e.g. because they use more advanced (iterative) post-processing after resizing. In such cases the output from this script could perhaps serve as input for such an application. Lightroom for example does a decent job, but offers IMHO not enough control. It also looks a lot like some sort of bilinear resampling is used for enlarged output of edge detail, which produces less vivid results than a more edge contrast preserving method.

As mentioned before, you can also experiment with deconvolution sharpening by initially selecting a '0' amount of sharpening, which will then prompt a question to add deconvolution sharpening after the the actual resizing took place (you can at that time already copy the resized and saved file without sharpening from your computer, before it gets reloaded and replaced by the deconvolved version when the script continues). You could try out some different values, but a 'blur size' equal to the enlargement factor (e.g. 4 pixels for a 400% enlargement) is a reasonable starting point. If halos start to become visible when you boost the amount, then a smaller blur size may work better. This is only basic single pass deconvolution, but it's more tweakable than a few baked-in presets.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 05, 2014, 06:39:15 am
You can't achieve with ImageMagick what I'm attaching here (actually, you need a program that currently only exists on my laptop's HDD), but if you have opinions RE: the following results, I'd be thankful to have the opportunity to read them.
Perfect examples of theory lining up with results. Things are not always this "perfect".
(Obviously (?), I'm not going for halo free. I'm going for good clean punch.)
P.S. Very thankful to Bart van der Wolf, Jim Kasson, Alan Gibson and Fred Weinhaus for inspiring code and ideas. And of course Anthony Thyssen for introducing me to EWA (Elliptical Weighted Averaging) a few years back, as well as suggesting that Mitchell-Netravali would be a good EWA weight function (and to Cristy et al for providing all of us with ImageMagick and, most importantly, its source code).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 05, 2014, 07:30:39 am
Here are versions with a softer recipe derived with the same theory.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 05, 2014, 08:57:24 am
You can't achieve with ImageMagick what I'm attaching here (actually, you need a program that currently only exists on my laptop's HDD), but if you have opinions RE: the following results, I'd be thankful to have the opportunity to read them.
Perfect examples of theory lining up with results. Things are not always this "perfect".
(Obviously (?), I'm not going for halo free. I'm going for good clean punch.)

Hi Nicolas,

Quite a nice balance between trade-offs for this image. While the higher spatial frequencies (and aliasing) have a higher (esp. undershoot) amplitude, they do mix into a pleasant rendering for this image at this size. I assume that the higher aliasing amplitude is less than the increased amplitude of the highest spatial frequencies that those aliases will be mirrored to, so that results in a net gain.

I do indeed wonder how it does on the slanted edge target (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg748381#msg748381) (which will show how well it is usable for various subjects, and it allows to quantify its resulting sharpness). Another interesting (really tough) image like this one (http://bvdwolf.home.xs4all.nl/temp/7640_CO40_FM1-175pct_sRGB.jpg) (resized to 533x800 pixels poses some interesting challenges that made me revise some of the earlier IM attempts) would also be nice to see your version of.

Cheers,
Bart

P.S. I've been experimenting with higher Keys Cubic alpha values in ImageMagick, but the gains are minimal and the drawbacks need more elaborate artifact suppression than we have currently implemented (so that may be the next thing I'll concentrate on). Especially the dark side halo undershoot becomes problematic pretty fast, and raising the gamma above 5 will start to visibly affect color saturation (of single pixels), in the 32-bit IM version.

P.P.S. Here's a link (https://www.dropbox.com/s/u0moxq7hz5xk9e5/Star.png?dl=0) to another (star) test-target that will reveal aliasing issues quite well, typically a challenge with down-sampling. It's also with this target that the difficulty with higher Keys alpha values is in the increased tendency to produce visible aliasing and other resampling artifacts. Of course, this is a more critical challenge than normal subject matter will pose, but that's the purpose (reveal worst case scenario performance).
Attached is what the current script (Version 1.2.2) produces when down-sampled to 20%, with a sharpening amount of 100.

The Nyquist frequency is as always located at a central diameter of 92 pixels, regardless of down-sampling percentage. Inside that diameter we can expect aliasing that would show up as aliasing mixed with small features in regular patterned image content, and just outside that diameter there may be visual artifacts due to resampling issues (blocking, ringing, quantization).
Make sure to watch at 100% zoom in a dedicated image editor/viewer, the browser may still resize it despite what it tells you.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 05, 2014, 09:22:49 am
...
While the higher spatial frequencies (and aliasing) have a higher (esp. undershoot) amplitude
...
Bart:
I was thinking about exactly that after posting the prototype's results.
Question: What size would you like to see the slanted edge resized to (widthxheight in pixels)? (Note that I am not tackling upsampling head on right now.)
What's the gamma you used to produce it? Straight gamma or linear then sRGB conversion?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on October 05, 2014, 11:16:38 am
You can't achieve with ImageMagick what I'm attaching here (actually, you need a program that currently only exists on my laptop's HDD), but if you have opinions RE: the following results, I'd be thankful to have the opportunity to read them.
...

When looking at them (2:1 and 1:1) I like the softer look for the eye's and and the "other" look for the rest of the image.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 05, 2014, 11:29:51 am
Bart:
I was thinking about exactly that after posting the prototype's results.
Question: What size would you like to see the slanted edge resized to (widthxheight in pixels)? (Note that I am not tackling upsampling head on right now.)

A 25% of the original should be fine. That would produce 200x75 pixels, which is fine in PNG or TIFF, but not ideal for JPEG (which prefers modulo 8 or 16 pixels dimensions, thus 24% would be preferable for that).

Quote
What's the gamma you used to produce it? Straight gamma or linear then sRGB conversion?

The source was linear gamma, but an Adobe RGB profile was assigned/embedded.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on October 06, 2014, 02:30:36 am
Hi Jan,

As it is, the upsampling routines attempt to 'do (very) well' (in avoiding nasty/distracting artifacts in final output, yet remaining relatively sharp in image detail without 'jaggies') on a variety of subject matter (see some examples here (http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=25935)). Of course, it would probably be possible to tweak a bit more for specific image content, so feel free to suggest (provide a small crop of something that you hope can be done better than your current workflow offers) what to consider.

And as always, the better the input image is (i.e. not over-sharpened), the better the output will be. Otherwise we will only enlarge the artifacts that were already there, accurately...

Also consider that there may be alternative, more involved, applications that can do a bit better e.g. because they use more advanced (iterative) post-processing after resizing. In such cases the output from this script could perhaps serve as input for such an application. Lightroom for example does a decent job, but offers IMHO not enough control. It also looks a lot like some sort of bilinear resampling is used for enlarged output of edge detail, which produces less vivid results than a more edge contrast preserving method.

As mentioned before, you can also experiment with deconvolution sharpening by initially selecting a '0' amount of sharpening, which will then prompt a question to add deconvolution sharpening after the the actual resizing took place (you can at that time already copy the resized and saved file without sharpening from your computer, before it gets reloaded and replaced by the deconvolved version when the script continues). You could try out some different values, but a 'blur size' equal to the enlargement factor (e.g. 4 pixels for a 400% enlargement) is a reasonable starting point. If halos start to become visible when you boost the amount, then a smaller blur size may work better. This is only basic single pass deconvolution, but it's more tweakable than a few baked-in presets.

Cheers,
Bart
Coming days or so i will test both Focus Magic and Topaz Details, icm different images and different settigns of the script. Will post results.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 06, 2014, 10:30:32 am
Coming days or so i will test both Focus Magic and Topaz Details, icm different images and different settigns of the script. Will post results.
A few suggestions:
Given that you are enlarging, stick to the "generic" scheme, with sharpening between 0 and 33 or so (up to 50 maybe?), and no added deconvolution or what not. The other schemes have other target uses.
The main possible improvement to the v122 script if you are using it for "real work" is more careful handling of other colorspaces than sRGB. Start by reading http://im.snibgo.com/resamphm.htm#colsp (http://im.snibgo.com/resamphm.htm#colsp).
Generally, I suggest that your final image use the same color space as the input image unless you really know what you are doing (with color spaces, image formats, and ImageMagick).
Off the top of my head but important If your input image belongs to a color space that is a "pure" gamma space (which sRGB is not: it's only approximately a gamma space), I would replace the initial
-set colorspace sRGB -colorspace RGB
by
-evaluate Pow INPUT_IMAGE_GAMMA
and the final
-set colorspace RGB -colorspace sRGB
by
-gamma INPUT_IMAGE_GAMMA
in the :generic section of the bat file. This produces inefficient (and probably slightly inaccurate) code; I can indicate how to streamline things if someone cares.
P.S. In case this is not clear: INPUT_IMAGE_GAMMA is supposed to be replaced by the numerical value of the gamma of your input = output color space. For Adobe RGB, it is 2.19921875, which you would put in the .bat file before running it.
-----
I am very much looking forward to hear about the results of your tests, because even though I have focused my attention on downsampling I think LWGB has a better chance of being a winning scheme when upsampling.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 06, 2014, 11:07:33 am
A few suggestions:
Given that you are enlarging, stick to the "generic" scheme, with sharpening between 0 and 33 or so (up to 50 maybe?), and no added deconvolution or what not. The other schemes have other target uses.
The main possible improvement to the v122 script if you are using it for "real work" is more careful handling of other colorspaces than sRGB. Start by reading http://im.snibgo.com/resamphm.htm#colsp (http://im.snibgo.com/resamphm.htm#colsp).
Generally, I suggest that your final image use the same color space as the input image unless you really know what you are doing (with color spaces, image formats, and ImageMagick).

Given that many photographers use Lightroom as their Digital Asset Management (DAM) system and Raw converter, it is easy enough to output the file that needs to be upsampled, with the destination profile conversion already done. Then that fie has a colorspace that will undergo a relatively benign roundtrip to linear(ish) gamma space, and back to that destination colorspace.

Once the colormanagement conversions of IM are verified to be accurate enough and properly used in linearization, and the Path(s) to the relevant source and destination profiles (including a fallback if no profile is found) is encoded, we may compare the practical implications for image quality of doing the resampling in a colormanaged environment to the current simple solution. However, profile conversions can also be a minefield (because of gamut volume differences, differences in quantization step sizes, perceptual non-linearity, dithering, etc.) ...

Quote
I am very much looking forward to hear about the results of your tests, because even though I have focused my attention on downsampling I think LWGB has a better chance of being a winning scheme when upsampling.

Indeed.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 06, 2014, 11:58:08 am
...
P.S. I've been experimenting with higher Keys Cubic alpha values in ImageMagick, but the gains are minimal and the drawbacks need more elaborate artifact suppression than we have currently implemented (so that may be the next thing I'll concentrate on). Especially the dark side halo undershoot becomes problematic pretty fast, and raising the gamma above 5 will start to visibly affect color saturation (of single pixels), in the 32-bit IM version.
...
Thank you for trying.
I'm getting pretty good results downsampling with EWA Keys (others too).
But I am also aware they have have limitations. Most pronounced when upsampling more than a little, but also in the spectral domain.
My "like" is not based on worst case scenarios.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on October 06, 2014, 01:02:55 pm
Given that many photographers use Lightroom as their Digital Asset Management (DAM) system and Raw converter, it is easy enough to output the file that needs to be upsampled, with the destination profile conversion already done. Then that fie has a colorspace that will undergo a relatively benign roundtrip to linear(ish) gamma space, and back to that destination colorspace.

Once the colormanagement conversions of IM are verified to be accurate enough and properly used in linearization, and the Path(s) to the relevant source and destination profiles (including a fallback if no profile is found) is encoded, we may compare the practical implications for image quality of doing the resampling in a colormanaged environment to the current simple solution. However, profile conversions can also be a minefield (because of gamut volume differences, differences in quantization step sizes, perceptual non-linearity, dithering, etc.) ...

Indeed.

Cheers,
Bart
Bart
Am I also correctly guessing that for downsampling and going from prophoto to sRGB, it's better to go to sRGB before imagemagick?

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 06, 2014, 01:33:50 pm
Bart:
Your windmill is a classic.
Results with my "secret" prototypes. "Dark haloing" appears to need work. Otherwise it looks good to me. Agree? (P.S. Staircasing :(.)
(I assume that it's OK that I reproduce downsampled versions here?)
P.S. Could someone post what recent Lightroom does?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 06, 2014, 02:15:31 pm
Bart
Am I also correctly guessing that for downsampling and going from prophoto to sRGB, it's better to go to sRGB before imagemagick?

My guess is that it's a safer route to do it that way.

Ideally one would do the conversion to the destination space as the last step in the workflow, but in this case it would mean that we will convert a gamma 1.8 pre-compensated colorspace to not exactly linear light for the resampling/sharpening. I'm not sure how much that error will influence the results though. That's why the slanted edge would be useful to see how luminance is affected if it is first assigned with ProPhoto RGB. Color will not blend at exactly linear light either.

That's why (for the moment) first converting to the destination profile will produce fewer unknown issues.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 06, 2014, 02:34:17 pm
My guess is that it's a safer route to do it that way.
...
+1 (if your final product is sRGB).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 06, 2014, 03:23:14 pm
Bart:
Your windmill is a classic.

It does have a lot of interesting features (single pixel lines, slanted edges, a few dust spots, shrubs, tree branches, text, etc.) and different material structures. ImageMagick handles the aliasing risk of the bricks just fine.

Quote
Results with my "secret" prototypes. "Dark haloing" appears to need work. Otherwise it looks good to me. Agree? (P.S. Staircasing :(.)

Yes, although the dark halos do add crispness to the resulting image (as it would with all smaller gamma adjustments), and some of the halos won't be too visible at such a small size. The light halos are a bit better, but a smaller deconvolution radius would do the same. The aliasing/stairstepping is a bit much though. Trying to control the dark side halos might require high gamma values, and could result in desaturated colors.

Quote
(I assume that it's OK that I reproduce downsampled versions here?)

No problem, fair use is allowed.

Quote
P.S. Could someone post what recent Lightroom does?

If nobody else beats me to it, I'll look at that tomorrow.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 06, 2014, 03:29:03 pm
Keeping things soft is a good way to stay out of trouble :|
P.S The full size original has "tasteful" sharpening applied. I wonder if this contributes. Case of "don't sharpen before resizing"? (Sounds like a cop out.)
P.S.2 It's actually pretty neat to enlarge the original and check out the demosaicking artifacts. Whatever method this is sure likes 45 degree angles.
P.S.3 Instead of producing versions with the "sharpness" parameter turned down (which of course would take care of staircasing and halos), I slapped a quick and dirty halo reduction on top of the "secret" schemes. Results attached. This, of course, is one image etc etc etc. I am nonetheless pleased. (Please burst my bubble!)
P.S I have managed to improve on "prototypeMGB", but not a lot.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on October 06, 2014, 06:38:25 pm
+1 (if your final product is sRGB).

Thanks.

Btw. When doing things inside prophoto RGB, I assumed that I would use the correct gamma, not the sRGB one.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on October 06, 2014, 06:41:16 pm
Keeping things soft is a good way to stay out of trouble :|


This reminded me that I have to recheck my capture sharpening after the upgrade to Capture One 8.
I checked it quite a while ago with Bart's tool.

But if I understand it correctly, it's better to keep it a tad soft and sharpen a bit more later in the pipeline.

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 07, 2014, 12:42:23 am
...
But if I understand it correctly, it's better to keep it a tad soft and sharpen a bit more later in the pipeline.
This is my general recommendation. But in any case it's how things fit together from end to end that matters.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 07, 2014, 04:04:26 am
This reminded me that I have to recheck my capture sharpening after the upgrade to Capture One 8.
I checked it quite a while ago with Bart's tool.

But if I understand it correctly, it's better to keep it a tad soft and sharpen a bit more later in the pipeline.

Hi Alain,

If upsampling is going to happen, then we do not want to upsample artifacts, so indeed use Capture sharpening with restraint (either exactly right, as determined with my tool for your lens at various apertures with your Raw converter, or a tad under). Current Raw converters are pretty poor in assisting the user with that aspect of the workflow.

When we do the Capture sharpening exactly right, there is no real down-side to it, because it will not create halos, just restore resolution from Capture bur. The only possible down-side could be if the resampling algorithms cannot cope very well with sharp (but still slightly blurry) real image content. So when the tools are not good, it may be necessary to under-sharpen a bit. The goal of this thread is to develop better methods, which also do cope well with good quality input.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 07, 2014, 04:43:12 am
Btw. When doing things inside prophoto RGB, I assumed that I would use the correct gamma, not the sRGB one.

Alain, this is a bit complex, and to a certain extend ImageMagick specific.

In general, we get better down-sampling quality (accurate color and luminance as it was in the original scene) when we use weighted averaging in a (near) linear gamma space to blend the various RGB pixel values of a larger sized region into one destination pixel at the smaller size. The downside is the risk of dark side halo being more prominent than light side halo once we return to the destination gamma space.

ProPhoto RGB has, besides different color primaries and White-point, a native gamma of 1/1.8. ImageMagick has built-in conversion support between sRGB gamma space and linear gamma space (probably because of lots of image content in the scientific community is in either of those gamma spaces). AFAIK it does not change the assumed color primaries.

So a proper conversion would require a full-blown color-management system (I believe IM uses at least parts of LCMS) to convert between color spaces including rendering intents (various flavors available) and chromatic adaptation (e.g. Bradford).

However, we only temporarily need the gamma linearization for color/luminance blending. So we would need a linear gamma version of all possible source profiles we might encounter to do a more accurate job. Instead we currently cut some corners by using the built-in IM functions. That means that we will convert a 1/1.8 gamma to approx. 2.2/1.8 = 1.2222 gamma space instead of linear gamma 1.0. The question is, how bad is that? Well, it's still better than a 1/1.8 or a 1/2.2 gamma blending (and even suppresses some of the dark side halo), because it is closer to 1.0, but it could be even better (in the sense of being calibrated and thus more predictable and accurate for color blends).

So while it's not ideal, it's not all that bad either. And we can circumvent some of the issues by already converting to the destination profile, before resampling. Not ideal, but not the end of the world either, after all, we are already creating totally new pixels in the process ...

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 07, 2014, 06:08:29 am
Alain:
Going through the sRGB to RGB conversion (and back) is unnecessarily inaccurate when you are dealing with a gamma space at both the input and output of the script.
For example, if what you are feeding to the script is ProPhoto RGB (which is very well approximated by gamma 1.8) and what you are getting out of it is also ProPhoto RGB, you should be getting better results, without using a fully color managed toolchain, by converting to linear RGB using -gamma 1.8 and converting out of linear RGB with -evaluate Pow 1.8. AFAIK ImageMagick will leave the primaries alone provided you don't use profiles (just copying the profiles from the input file to the output file does not do anything to pixel values).
The above works with any color space that is close to a gamma space: if what you feed the script and get out of it is same gamma with same primaries, just use -gamma and -evaluate Pow. (sRGB is close to being a gamma space but actually not that close. It's worthwhile to go through the standard in that case.)
I'll try to post a version of V122 of the script that does exactly that, hopefully later today. It will exploit the fact that pow((pow(a,b),c) = pow(a,b*c) to squeeze out more accuracy.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 07, 2014, 07:06:00 am
P.S. Could someone post what recent Lightroom does?

Attached, Lightroom 5.6 conversion at 100% JPEG quality, with no sharpening, and screen sharpening  (Low, Standard, High).

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 07, 2014, 08:52:15 am
Thank you Bart.
Do you happen to know the cause of the tone mismatch in the bricks of the house and windmill? I remember a lengthy discussion of exactly this issue (but not where the discussion occured; dpreview?).
I hope that these most glaring differences are easily trackable (and are not caused by something like mismatched use of sRGB v4 profiles with black point compensation or something arcane like that).
P.S. I meant between LR and the "secret" prototype (which I assume shows more or less the same as your script).
P.S. What a mess: http://ninedegreesbelow.com/photography/srgb-profile-comparison.html (http://ninedegreesbelow.com/photography/srgb-profile-comparison.html).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 07, 2014, 11:00:52 am
Thank you Bart.
Do you happen to know the cause of the tone mismatch in the bricks of the house and windmill?

They are a different type and color of bricks. The windmill has traditional style (more reddish/yellow colored) bricks, because it is a monument. It functions as a museum and flour mill. The houses chose to use a darker colored version, and smaller sized bricks.

So the colors are rendered accurately, given the lighting conditions at that moment of the day.

Attached 2 crops from the down-sampled windmill sample, at 800% magnified view, the differences are subtle but illustrative. Left is the Lightroom version with Standard screen sharpening, right is the regular (Version 1.2.2) LWGB script version with deconvolution sharpening at 100.

Look at the branches against the sky. They are marginally less blocky and a bit more 'organic' in the LWGB version. Look at the sky color that shines through the support beams, it is preserved a bit better (caused by linear light resampling). The beams themselves are also slightly less blocky and more anti-aliased against the sky (possibly due to EWA resampling). The mishmash of tree branches at the bottom is slightly better visible in the LWGB version. The arc above the door is slightly less blocky in the LWGB version.

In other areas, such as the lighter leaves and branches above the boat, there is also a bit more differentiation in the LWGB version.

The differences are really subtle, they will change with adjustments in sharpening, and it requires serious pixel peeping to notice what is different.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 14, 2014, 07:51:54 am
Given that I am going for punch, and in light of subtle side effects RE: tone preservation in high frequency areas, I've decided to remove from my current "top downsampling prototype" the code that explicitly tries to mitigate haloing. This makes the "secret prototype" something like this. As the chart makes quite obvious, I'm not holding punches in the sharpness department (although I limit myself to adding a single halo, and I make sure that the added halo is one pixel wide).
Warning: Some subtle color toolchain issues make the tones different than what was shown by Bart as coming out of Lightroom. I have not decided to address this, since it would require, among other things, figuring out what Lightroom does, what my doctored ImageMagick does, what my viewer (nip2) does, and nailing the cause(s) of the mismatch. When I compare with the result of resampling with the ImageMagick's correctly implemented bilinear (a.k.a. triangle) through linear RGB, I can tell that my "secret scheme" does not mess tones up. For my purposes, this is good enough.
P.S. It appears that Bart, LR and I are hunting for very roughly the same ballpark look. We're talking apples and oranges, not pumpkins and raisins.
P.S. Using the same method for enlarging is absolutely terrible :( One of the worst image enlargement schemes I've ever put together.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 15, 2014, 04:57:02 am
As it is, I am not only looking for good downsampling. I am also looking for good "near identity transformations", the simplest such transformation being a rotation by a small angle.
The first attachement is the result of rotating the "chart" about its center using the default general purpose ImageMagick method, performing the filtering in linear light.
convert input.png -evaluate Pow 2 +distort SRT 1 -gamma 2 IMdefault.png
The second attachment is obtained using the "secret" scheme inspired, in part, by Bart van der Wolf and Jim Kasson's comments.
P.S. The difference is immediately obvious with the star target suggested by Bart van der Wolf in http://www.luminous-landscape.com/forum/index.php?topic=91754.msg767344#msg767344 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg767344#msg767344). Since I did not know the color space, I simply used
convert input.png -distort SRT 1 IMdefault.png
P.S.2 Comparing the "secret" scheme with the default ImageMagick "distort" scheme is cheating, since the default ImageMagick scheme was purposely chosen to be somewhat "soft" (Elliptical Weighted Averaging with the Robidoux Keys filter, which is roughly equivalent to Mitchell-Netravali). This being said, I think that you'd be hard pressed to find a general purpose scheme that keeps things this sharp and artifact free. (Hubris?)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on October 15, 2014, 10:39:09 am
Looks good.  Are we allowed to play with the secret prototype sauce recipe?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 15, 2014, 10:40:08 am
Jack:
Unfortunately not: Some things are amenable to be done fully transparently; others, not.
The "secret" ingredient will remain a secret.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 15, 2014, 10:58:18 am
As it is, I am not only looking for good downsampling. I am also looking for good "near identity transformations", the simplest such transformation being a rotation by a small angle.

Hi Nicolas,

Just a gentle word of caution. These maximum amplitude charts hide the degree of clipping. So while the image looks better for line art and things that are allowed to clip to black and white, they do not necessarily look as good on lower amplitude versions or other continuous tone image content.

I do appreciate that rotation and other types of (non-linear) scaling may benefit from a different type of filtering, more focused on preservation of sharpness. Since down-sampling and aliasing are commonly found together, aliasing may be mitigated for rotation by first upsampling, or only upsampling for e.g. keystone correction (a poor man's alternative to oversampling), optionally followed by an optimal down-sampling.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 15, 2014, 11:17:34 am
...
Just a gentle word of caution. These maximum amplitude charts hide the degree of clipping. So while the image looks better for line art and things that are allowed to clip to black and white, they do not necessarily look as good on lower amplitude versions or other continuous tone image content.
When I read your word of caution, I was just about done looking at the results of rotating binary images in which 0 is mapped to, say, 255/3, and 255 is mapped to 2*255/3. That is, dark grey on light grey instead of black on white. For example, the "grey" star looks just fine.
I take note of your other piece of advice.
Thank you. :)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 15, 2014, 01:40:22 pm
Another quick set, based on DeltaE_8bit_gamma2.2.tif http://www.brucelindbloom.com (http://www.brucelindbloom.com). First, the "secret" scheme. Then, the ImageMagick default (through linear light). And then a scheme that can be achieved with ImageMagick reasonably close to the secret scheme (a nudge too sharp I would guess). Note that the images are gamma 2.2.
convert DeltaE_8bit_gamma2.2.tif -evaluate Pow 2.2 -define filter:c=0.48146869222618521 -distort Resize 15\% -gamma 2.2 IMKeysC.48146869222618521.png
You may want to compare with http://blog.kasson.com/?p=7244 (http://blog.kasson.com/?p=7244)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: JRSmit on October 18, 2014, 02:42:29 am
As i mentioned earlier, i would report my findings with uprezzing and image developments , all primarely for better large size prints.
Well, that has become an excersize in frustration.
first the good part: the uprezzing , script 1.2.2 works fine. The choices in sharpening both work fine, the only thing missing is the lack of direct feedback like in PS or LR.
However working out the best setting for an image takes quite some time.
As a general rule i would say to use an image to start from, with sharpening reduced, or in LR literally set to 0. You then have more room for sharpening after uprezzing without risk of halo.
then the bad part:
i also tried to use Topaz Detail and Focus Magic, well. . Focus magic has a standalone option, but then cannot handle the large files (its 32bit memory limit?), as plugin(64bit) it has limited functionality, so not of any use for me.
Topaz Detail is only a plugin in PS, fine sofar, but first need to load the image in PS, then in TD, thus loading memory, and 16GB is then not really enough. But if you stay within the memory limit, then TD provides a lot of options for sharpening. However with a steep learning curve.
Where i am now is that i cannot get an advantage over LR wrt to sharpening after uprezzing with FD or with TD, unless using a lot of my time to tinker around. And then still the question remains, does it show in the print.
So , i will continue with finding ways to improve images for large size prints but at a slower pace.
The uprezzing works fine, that is for sure. And the sharpening in the script is quite adequate to start with.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on October 18, 2014, 03:36:20 am
As i mentioned earlier, i would report my findings with uprezzing and image developments , all primarely for better large size prints.
Well, that has become an excersize in frustration.

Hi Jan,

Thanks for the feedback, with a focus on the upsampling side of things. The frustration part I can understand a bit, although it's also part of any learning curve, gaining experience through trial and error.

Quote
first the good part: the uprezzing , script 1.2.2 works fine. The choices in sharpening both work fine, the only thing missing is the lack of direct feedback like in PS or LR.
However working out the best setting for an image takes quite some time.
As a general rule i would say to use an image to start from, with sharpening reduced, or in LR literally set to 0. You then have more room for sharpening after uprezzing without risk of halo.

Yes, this is usually the case, cascading of sharpening runs is usually not a good idea, unless extreme caution is used in the beginning stages. Also, the upsampling will magnify any issues that were created earlier in the workflow, making them more visible.

Quote
then the bad part:
i also tried to use Topaz Detail and Focus Magic, well. . Focus magic has a standalone option, but then cannot handle the large files (its 32bit memory limit?), as plugin(64bit) it has limited functionality, so not of any use for me.

But its deconvolution quality is praised throughout the photographic community. It also works great on upsampled images to remove some of the upsampling blur.

Quote
Topaz Detail is only a plugin in PS, fine sofar, but first need to load the image in PS, then in TD, thus loading memory, and 16GB is then not really enough. But if you stay within the memory limit, then TD provides a lot of options for sharpening. However with a steep learning curve.

Topaz Detail can also be launched by itself directly from Lightroom. You need to install the free utility, a stand alone editor, called Fusion Express (https://support.topazlabs.com/hc/en-us/articles/200091347-Lightroom). That will allow to right mouse click on an image and process it outside of LR with any of the Topaz Labs plugins.

They additionally have a plugin called "photoFXlab (http://www.topazlabs.com/photofxlab)" that can also run as a standalone application, as a control hub for all the other plugins, and it offers additional  functionality like masking and layers blending.
 
Quote
Where i am now is that i cannot get an advantage over LR wrt to sharpening after uprezzing with FD or with TD, unless using a lot of my time to tinker around. And then still the question remains, does it show in the print.
So , i will continue with finding ways to improve images for large size prints but at a slower pace.
The uprezzing works fine, that is for sure. And the sharpening in the script is quite adequate to start with.

Again, thanks for taking the time to give feedback.

Cheers,
Bart
Title: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Jack Hogan on October 27, 2014, 04:26:17 pm
Hello Bart, Nicolas et al,

I've been thinking about v1.2.2 because I really like its idea although I do not like what it does to my pictures...yet :)  I don't think it's just perceptual and I may have an idea as to where that loss of local contrast and saturation may be coming from when downsizing: it appears that v1.2.2 smears the incoming data too far beyond final Nyquist therefore giving up too much information - before then applying (by my standards) aggressive sharpening to attempt to recover it.

The strategy may work with black and white test or similar images because the viewer may not perceive the fact that a fair amount of the final observed 'sharpness' has been re-built artificially.  But it seems to fall apart with natural images where, for instance, no sharpening is ever going to be able to figure out whether what arrived as a bland yellow from the downsizing process was actually supposed to be a saturated orange from a high local contrast area in the original image.

Here is an example with two screen captures, one the original 11500px image fit to the screen by CS5 (bicubic, the benchmark) and the other the same image downsized to 16.37% by v1.2.2, D option and no sharpening, displayed at 100%.  The loss of local contrast and saturation is evident as emphasized by the histograms of a highly saturated, high frequency patch (the square marquee near the top of the saturated yellow/orange tree to the left).  The histogram of the original looks [similar to] that of the bicubic.

(http://i.imgur.com/7CAb9aX.gif)
http://i.imgur.com/7CAb9aX.gif

It seems to me that the combined filtering and downsizing algorithm in v1.2.2 blur the original data to the point where too much information gets averaged out giving up more Michelson contrast than needed (as confirmed by comparing with the benchmark).  Contrast is transferred accurately where it changes slowly.  Where it changes quickly it is not: the deep shadows get averaged up (they get brighter) and saturation is lost.  Here is a full resolution image of the difference between the two screen captures, with a curve for emphasis:

(http://i.imgur.com/UDgLibu.png)
http://i.imgur.com/UDgLibu.png

If my intuition is correct, it seems to me that we would need to use a smaller 'radius' for the pre-filter+algorithm in order to let more local (Michelson) contrast through.  The sharpening would also probably be more effective as it would have more to bite on.

What do you think?

Jack
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Bart_van_der_Wolf on October 27, 2014, 08:00:27 pm
Here is an example with two screen captures, one the original 11500px image fit to the screen by CS5 (bicubic, the benchmark) and the other the same image downsized to 16.37% by v1.22, D option and no sharpening, displayed at 100%.

Hi Jack,

I'm not going to jump to conclusions (although I have some ideas about cause and effect, namely gamma related effects and clipping), but do want to first understand what we are really looking at.

To avoid comparison with Photoshop screen resampling (which might be something like bilinear or worse), it would help if you could make an image crop available of the original image at 100%, e.g. the general region around the tree where you also sampled the histogram from, something like 1500x1500 pixels in size. That would result after down-sampling to 16.7% to something like 250 pixels square, still large enough to be meaningful for (visual) inspection.

Cheers,
Bart
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: NicolasRobidoux on October 28, 2014, 04:07:18 am
...
I've been thinking about v1.22 because I really like its idea although I do not like what it does to my pictures...yet :)  I don't think it's just perceptual and I may have an idea as to where that loss of local contrast and saturation may be coming from when downsizing: it appears that v1.22 smears the incoming data too far beyond final Nyquist therefore giving up too much information - before then applying (by my standards) aggressive sharpening to attempt to recover it.
...
Here is an example with two screen captures, one the original 11500px image fit to the screen by CS5 (bicubic, the benchmark) and the other the same image downsized to 16.37% by v1.22, D option and no sharpening, displayed at 100%.
...
It is a defensible position that one should not apply very strong antialiasing and then sharpen back. Although it may be a good approach, it certainly does not come without cost. But nothing's for free.

Now, just to make sure that there is no confusion: You are aware that Bart's script, when used with the "D" option and sharpening set to 0, uses a method that is used at no other time (EWA with a Quadratic B-spline kernel)?
Among the options presented by the script, "D" with sharpening 50 or 100, or the generic scheme with sharpening 50 or 100, should be preferable, unless you really really do not want the downsampler to introduce any halo.
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Jack Hogan on October 28, 2014, 04:25:10 am

To avoid comparison with Photoshop screen resampling (which might be something like bilinear or worse), it would help if you could make an image crop available of the original image at 100%, e.g. the general region around the tree where you also sampled the histogram from, something like 1500x1500 pixels in size. That would result after down-sampling to 16.7% to something like 250 pixels square, still large enough to be meaningful for (visual) inspection.

Cheers,
Bart

Hi Bart, I believe that my CS5 is set up to do bicubic for screen resizing (at least that's what I have checked in the options).

Here is a link to a 1500x1500 crop from the original (https://drive.google.com/file/d/0BzGif7iQ1sgleUR1azh1WVNFdWc/view?usp=sharing).

And here is a link to the same crop downsized to 400x400 in V1.22 and CS5 Bicubic (https://drive.google.com/file/d/0BzGif7iQ1sglUG9LVGVTOU84a3c/view?usp=sharing) (best for smooth gradients; scale, constrain and resample checked) as layers.  The histograms above refer approximately to the square area at X=205, Y=170, W=38, H=38.

Jack
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Jack Hogan on October 28, 2014, 04:35:35 am
Now, just to make sure that there is no confusion: You are aware that Bart's script, when used with the "D" option and sharpening set to 0, uses a method that is used at not other time (EWA with a Quadratic spline kernel)?
Among the options presented by the script, "D" with sharpening 50 or 100, or the generic scheme with sharpening 50 or 100, should be preferable, unless you really really do not want the downsampler to introduce any halo.

Yes, I am aware of that.  I originally used it with sharpening around 50 but was unhappy with the results for the reasons mentioned.  I put sharpening at 0 in this case to try to help isolate what was causing the undesired loss of local contrast and saturation: the downsizing or the sharpening.  The images in the previous post suggest that the downsizing process is the likely culprit and one possible explanation for its apparently lackluster performance compared to the benchmark could be that the algorithm goes too far in its aggregate low pass filtering.

What would you recommend as a not-secret-yet-leading-edge alternative for downsizing?

Jack
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Bart_van_der_Wolf on October 28, 2014, 06:21:57 am
Hi Bart, I believe that my CS5 is set up to do bicubic for screen resizing (at least that's what I have checked in the options).

Hi Jack,

Due to the (non-)proofed preview, screen grabs are hazardous so I prefer actual resampled file comparisons, which is now possible with your crop, thanks for that. The bicubic resize preference doesn't apply to screen zooms (they are always quick and dirty), but to the default that is shown with e.g. the resize menu.

Quote
Here is a link to a 1500x1500 crop from the original (https://drive.google.com/file/d/0BzGif7iQ1sgleUR1azh1WVNFdWc/view?usp=sharing).

Okay, things are getting clearer now. I've attached a (screen grab noless ;) ) comparison of the original as background, and two zoomed in areas of a bicubic downsample, and a downsample with the V1.2.2 script to 250 pixels a side while using Down-sample plus Sharpening Amount=100, so all defaults.

What becomes obvious, is that the bicubic version is much darker(!) which adds saturation, and more jaggy/aliased which suggests sharpness (but actually are artifacts that may help in this case of organic chaotic detail). So it's not the V1.2.2 script version being much lighter, although it could turn out a bit lighter in some isolated places, due to linear light resampling to preserve color accuracy.

If that is not wanted, and one doesn't want to address that in a separate post-downsample adjustment, then one could do the resampling with some remaining gamma, by temporarily adding some gamma to the linear light converted image before actually resizing the image (or replace the colorspace conversion by a moderate gamma conversion). But I do not think that is wise, because it will produce inaccurate colors. The more correct way to deal with it may be to reduce highlight brightness before downsampling if one wants more saturated highlights.

As for sharpness, there is nothing wrong with the windowing and averaging of the resize filter that is used in the script. There are single pixel wide features where expected, and other features are just not as dark as in the bicubic version, which makes them seem wider. It is always possible to make the deconvolution more aggressive than it should be, by reducing the radius to 0.4 and increase the amount used as desired. One could also use a sharpening setting of 1 (or zero, which activates a different algorithm) instead of the default, and use a separate deconvolution plugin or Smart sharpening with a very small radius and a large amount which can simply be recorded in a PS-action.

Such tweaks could be added to a commercial version of a resampling option, with a sort of crispness control or other creative tweaker. I've mentioned a Blend-if adjustment layer before, this could be made to fit in that functionality.

Cheers,
Bart
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Bart_van_der_Wolf on October 28, 2014, 09:05:16 am
I've attached a cropped animation of the original, and a bicubic downsample, and a downsample with the V1.2.2 script to 250 pixels a side while using Down-sample plus Sharpening Amount=100, so all defaults. I then upsampled the smaller versions with Nearest Neighbor to the original size.

When you look at the display at the correct angle for accurate gamma, you'll see that the Bicubic is darker and thus oversaturated, and that the Script version attempts to keep the correct color. It also shows the highlight edge of the tree trunk almost disappearing with Bicubic, and retained with the script. Also the faint tree trunks in the shaded backgound almost disappear with Bicubic and are retained (the size increase is only due to relatively brighter or less dark than Bicubic pixels, and less stair-stepping and better anti-aliasing) with the script.

IMHO the script version is closer to the original, and Bicubic is darker and more saturated. Most of that effect is due to linear light resampling, i.e. gamma related as I suspected.

Cheers,
Bart

P.S. I like the saturation, no mistake about that, but perhaps starting with a less sharpened, or slightly toned down highlight version is a better way to get that transferred to the down-sampled version.
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Jack Hogan on October 28, 2014, 12:21:19 pm
What becomes obvious, is that the bicubic version is much darker(!) which adds saturation, and more jaggy/aliased which suggests sharpness (but actually are artifacts that may help in this case of organic chaotic detail). So it's not the V1.2.2 script version being much lighter, although it could turn out a bit lighter in some isolated places, due to linear light resampling to preserve color accuracy.

Hi Bart thanks for your efforts.

I've looked at the attachments and it seems to me that what one deems to be lighter/brighter and more/less saturated become subjective very quickly.  For instance in the original vs upsized downsampled animation it looks to me that bicubic may be a touch darker than the original but that V1.22 is much brighter.  Independently of why the brightening occurs (I'll leave that in your capable hands) to me the brightening appears also quite obvious when compared to the original (before bicubic downsizing).  To make sure I was not falling pray to some sort of a perceptual trap I looked at the stats in the same high frequency saturated ROI shown earlier off the TIFFs that you downloaded (Original: X=773 Y=649 H=120 W=120; Downsampled: X=206 Y=172 H=33 W=33).  Here they are with their histograms, Original on top followed by Bicubic and by V1.22:

(http://i.imgur.com/deqDeaI.png)

The original and bicubic means are virtually identical (159,61 vs 159.86), while V1.22's is substantially higher (168.31).  Now look at the standard deviations: bicubic's is 91% of the original while V1.22 is only at 78%.  That's all lost local (Michelson) contrast information.

As for sharpness, there is nothing wrong with the windowing and averaging of the resize filter that is used in the script. There are single pixel wide features where expected, and other features are just not as dark as in the bicubic version, which makes them seem wider. It is always possible to make the deconvolution more aggressive than it should be, by reducing the radius to 0.4 and increase the amount used as desired. One could also use a sharpening setting of 1 (or zero, which activates a different algorithm) instead of the default, and use a separate deconvolution plugin or Smart sharpening with a very small radius and a large amount which can simply be recorded in a PS-action.

You may very well be right, but my feeling is that this is not an issue one can solve with post-sharpening: the issue, if there is one, is imo in the downsizing process.

Hope this feedback helps make the tool better.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on October 28, 2014, 03:05:16 pm
Jack:
I have seen and measured the "lightening of high frequency detail" when downsampling with LWGB.
My guess is that it is caused by blending a result obtained with gamma light.
Deal breaker? Not my call: One of many artifacts anyway.
Title: Landscaper's Motto: Don't Mess with the Tones
Post by: Jack Hogan on October 28, 2014, 06:43:51 pm
Jack:
I have seen and measured the "lightening of high frequency detail" when downsampling with LWGB.
My guess is that it is caused by blending a result obtained with gamma light.
Deal breaker? Not my call: One of many artifacts anyway.

Indeed, different people who work with different content may come to different conclusions as to what artifacts they are willing to compromise on.  On the other hand, and I feel that I am speaking for landscape photographers at large, after one spends some time to get the full sized image to look about right for their tastes the last thing they need a resizing algorithm to do is mess with its tones.  Compromises as far as the byproducts of downsizing are concerned (softness vs ringing etc.) are fine - but don't mess with their tones :-)

Jack
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: NicolasRobidoux on October 29, 2014, 11:48:45 am
...
What would you recommend as a not-secret-yet-leading-edge alternative for downsizing?
http://www.luminous-landscape.com/forum/index.php?topic=91754.msg770017#msg770017 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg770017#msg770017)
A quick and dirty way to get something more or less like it is to replace "Quadratic" by "CatRom" in Bart's script, and use the "D" option with sharpening set to 0.
If you find it too sharp, try replacing "Quadratic" by "RobidouxSharp".
In between, you'll have to use the "-define filter:C=..." approach. CatRom is .5; RobidouxSharp is 0.3689927438004929 = (42*sqrt(2)-7)/142.
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Jack Hogan on October 30, 2014, 05:52:13 am
http://www.luminous-landscape.com/forum/index.php?topic=91754.msg770017#msg770017 (http://www.luminous-landscape.com/forum/index.php?topic=91754.msg770017#msg770017)
A quick and dirty way to get something more or less like it is to replace "Quadratic" by "CatRom" in Bart's script, and use the "D" option with sharpening set to 0.
If you find it too sharp, try replacing "Quadratic" by "RobidouxSharp".
In between, you'll have to use the "-define filter:C=..." approach. CatRom is .5; RobidouxSharp is 0.3689927438004929 = (42*sqrt(2)-7)/142.

Excellent, will try that, thanks.
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: NicolasRobidoux on December 05, 2014, 07:51:40 am
Excellent, will try that, thanks.
Jack: Have you found one of these options not to lighten high frequency areas?
Title: Re: V1.22: Too Much Michelson Contrast Lost in the Transfer
Post by: Jack Hogan on December 07, 2014, 04:28:36 am
Jack: Have you found one of these options not to lighten high frequency areas?

Hi Nicolas,

To tell you the truth when I went back and tried to find where to apply the parameters you kindly provided I realized that I do not know/understand Imagemagick enough to roll my own command blend - so I temporarily reverted back to bicubic thinking I would come back to the issue in the future when I have more time to dedicate to it.  This hasn't happened yet :-)  Happy to test out other folks' suggestions though.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 23, 2015, 05:43:44 am
Hello Jack, I think he meant the ":nodownsharp" approach, but just replacing Quadratic with CatRom.
What I don't know is if I need to define the 'c' component or is implicit in CatRom.

convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ^
 -filter CatRom -distort Resize %mSize% ) ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

I also observed something interesting in the landscape sample. If you average spatially all the pixels on the three of them (original, bicubic and Bart's script), you can see that the bicubic downscale is almost identical to original in saturation and brightness, whereas the script's downscale has less saturation and more brightness (in HSL numbers). Probably this is not relevant but enforces Jack observations on previous posts.

On Bart's post above (http://forum.luminous-landscape.com/index.php?topic=91754.msg773101#msg773101) it's easy to see that linear downscaling holds more tonal resemblance to original, at least perceptually on a 1:1 scale comparison, but I think the problem might come from the stance of giving more bias to highlights than what our non-linear brains would in real life, either from distancing from the real thing or a reproduction of it in a display or photo. Just thinking out loud.

After this insight I wanted to ask if it's possible to do image resizing in bicubic spline, if I'm not mistaken, something similar to avisynth's spline36, which I measured and to my surprise is ~99% identical to Photoshop's bicubic (avisynth applies an internal AA or so it seems (http://forum.doom9.org/showthread.php?p=1474853#post1474853)). I find it a pleasant basic downscaler, with room for a slight post-sharpening in occasions.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 23, 2015, 06:35:25 am
If you specify any of Spline, Hermite, Mitchell, RobidouxSoft, Robidoux, RobidouxSharp or CatRom, you don't need to set c. It's done for you based (roughly: internal values have more precision) on the values shown in http://www.imagemagick.org/Usage/filter/#cubic_bc (http://www.imagemagick.org/Usage/filter/#cubic_bc). They are all BC-cubic splines. -filter Cubic combined with a b or c value gives a Keys cubic. Hermite is not a Keys cubic, so you need to set both b and c if you don't invoque it by name.
(Quadratic is not a cubic spline (!), so it does not fit the pattern.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 23, 2015, 06:47:42 am
It is possible I misunderstood bicubic spline with spline polynomials. This (http://forum.doom9.org/showthread.php?t=147117) is what I refer to, is this possible in ImageMagick? Lanczos has a bit too much ringing, and bicubic are commonly a bit too soft generally speaking.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 23, 2015, 07:57:07 am
Meanwhile for those interested I attach a modification of Bart's script (only the downsample part) for processing full folder files.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 23, 2015, 11:26:29 am
It is possible I misunderstood bicubic spline with spline polynomials. This (http://forum.doom9.org/showthread.php?t=147117) is what I refer to, is this possible in ImageMagick? Lanczos has a bit too much ringing, and bicubic are commonly a bit too soft generally speaking.
That I know, the widest spline filter you can generate easily with ImageMagick has two lobes. This being said, I don't miss the method you suggest, because I find EWA Keys filters like RobidouxSharp and CatRom and deblurred EWA Lanczoses like LanczosSharp (and LanczosSharpest, which I've not had time to implement so you can call it "by name") to be plenty sharp. There is way more to "bicubic" than the one Photoshop version.
Correction I'm actually pretty sure that the only "natural spline" you can generate easily in ImageMagick is the one named "Spline", which has only one lobe.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 23, 2015, 06:11:37 pm
Thank you! Spline36 has 3 lobes, here (http://svn.int64.org/viewvc/int64/resamplehq/doc/kernels.html) is a graph describing its curve. It looks similar to lanczos with 3 lobes but without second ringing. Is lanczos in ImageMagick also limited to 2 lobes?

I normally use CatRom as a fallback of spline36 instead of lanczos, since I tend to prefer a bit of aliasing over ringing.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 24, 2015, 04:03:26 am
Thank you! Spline36 has 3 lobes, here (http://svn.int64.org/viewvc/int64/resamplehq/doc/kernels.html) is a graph describing its curve. It looks similar to lanczos with 3 lobes but without second ringing. Is lanczos in ImageMagick also limited to 2 lobes?

I normally use CatRom as a fallback of spline36 instead of lanczos, since I tend to prefer a bit of aliasing over ringing.
When you are using -filter CatRom -distort Resize you are not using the Catmull-Rom cubic the usual way. (To use it the usual way, use -filter CatRom -resize.) You are using it as a weight generator for an Elliptical Weighted Averaging filter.
In ImageMagick, you can have up to 8 lobes for the windowed Sinc (-resize) and Jinc (-distort Resize) filters. The default is 3 for -filter Lanczos (and LanczosSharp).
Have a look in http://www.imagemagick.org/Usage/resize/ (http://www.imagemagick.org/Usage/resize/).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 24, 2015, 04:51:55 am
Thank you NicolasRobidoux. I was writting a post while you posted.

Obviously I meant avisynth's CatRom on my above post, which is seemingly a bit different than everything else since avisynth uses an internal AA as stated earlier.

I run some interesting tests, all gamma encoded using the EWA flavour.
Trying to reproduce the "ideal" avisynth spline36 which is almost 99'9% exact Photoshop's bicubic, I found that Mitchell-Netravali is very near to that.
Fancying a little more tested Rubidoux Sharp and it is even nearer, basically 98% right there the 2% being translated to a very slight and not very noticeable blurriness, which can be fine tuned further with the "c" component.

That said I think I will base my downscales in this kernel, with an option to process in linear-light if desired.

As for the other results just to say that CatRom has a very strong acutance, its result is very similar to this thread's script (in its non-linear version), I know many photographers favor this strong exaggerated look, but I prefer the mentioned Robidoux Sharp finish with room to some post sharpening if necessary. The remaining test plain spline with post sharpening is too sharp and aliased to my taste, even if tuned not to oversharpen.

I uploaded the results so you judge by yourself (sorry I forgot to upload crops):
http://compare.bakashots.me/compare.php?setId=1410&comparisonId=8735


PS: maybe I should just test but I guess -set filter:lobes won't work with -filter spline right?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 24, 2015, 05:17:16 am

PS: maybe I should just test but I guess -set filter:lobes won't work with -filter spline right?
No, it does not. Only (TTBOMK) with windowed Sinc and Jinc, and Lagrange (the closest thing to "arbitrary number of lobes (polynomial) splines" in ImageMagick).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 24, 2015, 06:18:16 am
PS: maybe I should just test but I guess -set filter:lobes won't work with -filter spline right?
No, it does not. Only (TTBOMK) with windowed Sinc and Jinc, and Lagrange (the closest thing to "arbitrary number of lobes (polynomial) splines" in ImageMagick).

Thank you, I just tested and Lagrange 3 lobes is pretty much CatRom, using 2 or 4 lobes is even sharper so I don't know. Anyways I'm very glad with RobidouxSharp results and will stick with that, I thought on adding some post sharpener ala Unsharp Mask in Photoshop, just testing around got similar values for those I use there, one could argue what could be better, if stressing a bit more on the "c" component or using a post-sharpener, but I like to respect kernel conventions and work from there.

I post what is IMHO the state of the art for downscaling (fill the rest of settings according).
You should only tweak the 1.2 amount value of unsharp, set to 0 to disable.

-define filter:b=0.2620 -define filter:c=0.3690 -filter Cubic -distort resize 25% -unsharp 0x0.3+1.2+0.0
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 24, 2015, 06:38:13 am
...
-define filter:b=0.2620 -define filter:c=0.3690 -filter Cubic -distort resize 25% -unsharp 0x0.3+1.2+0.0
You can get the same (more precise actually:.2620 and .3690 are rounded values; internally, IM uses full precision versions of the corresponding algebraic numbers) with
-filter RobidouxSharp -distort resize 25% -unsharp 0x0.3+1.2+0.0
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 24, 2015, 06:44:12 am
I forgot to ask, is it ok to unsharp in linear light (or high bit depth), or do I need to do after that? Sorry with the newbie questions...
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 24, 2015, 06:45:51 am
Generally, you should get better results if you resample in linear light, but post-sharpen in gamma light.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on March 24, 2015, 11:01:08 am
I forgot to ask, is it ok to unsharp in linear light (or high bit depth), or do I need to do after that? Sorry with the newbie questions...

Hi,

Deconvolution is most predictable in linear light, because it will affect shadows the same as highlights. But that is more about restoration of the original signal's resolution than traditional USM 'sharpening'. Unsharp masking is more about boosting acutance, edge contrast, and that can be done quite well on gamma adjusted images.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Dogway on March 24, 2015, 08:21:28 pm
Thanks. I guess I can just use you employed convolution method "DoG" for RobidouxSharp, changing some settings like strength or radius so there's not much dark halos. Maybe even with your blending approach, although I don't know if there are benefits on that regard when gamma encoded resizes.

I did a brief test with the monkey pic. Took some 2 or 3 meters distance and toggled between original, linear and gamma encoded versions to fit screen (with Photoshop preview upscaling). The linear version was pretty much the same as original even at a distance, while the gamma encoded was darker, so I guess that yeah, if you were from an object at a larger distance you would see things like in linear light resizes.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 27, 2015, 02:14:52 pm
You can get the same (more precise actually:.2620 and .3690 are rounded values; internally, IM uses full precision versions of the corresponding algebraic numbers) with
-filter RobidouxSharp -distort resize 25% -unsharp 0x0.3+1.2+0.0

Thanks for this, Nicolas and Dogway.

Just to throw in a slightly different perspective, I decided to measure MTF curves of a downsized ISO100 D800e slanted edge using the different methods discussed in the latter part of this thread.  The original image is a 300x400 pixel crop directly off DSC_6483.NEF (I downloaded it from dpr and so can you if you so wish) saved as a tiff with absolutely zero processing other than CFA normalization.  I think it's as close as one can get to the actual spatial resolution information captured by the camera.  You can download it from here (http://www.strollswithmydog.com/wordpress/wp-content/uploads/DSC_6483V.tif) and it looks like this

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/DSC_6483V.png)

That beautiful edge was first measured with MTF Mapper (http://sourceforge.net/projects/mtfmapper/) - then downsized 4:1 using Photoshop's standard methods and re-measured.  These are the Spatial Frequency Response (MTF) curves of the original image indicated by a dashed line and after downsizing to 25% of its original size by CS5 nearest neighbour, bilinear and plain vanilla bicubic.

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Photoshop-Downscaling.png)

The original is soaring at those lofty heights because one of its pixels corresponds to four of the others'.  Nearest neighbour tracks it well, which is good at the lower frequencies, not so good beyond Nyquist because the ideal SFR/MTF curve for a perfect downsizing would track the original's up to Nyquist - but show little or no energy above 0.5 cycles/pixel, the point at which aliasing and moirè start rearing their ugly heads. Bilinear and bicubic show different amounts of attenuation, with bicubic looking like the best compromise.  The blue curve below shows the upper boundaries of what I think an ideal MTF curve should look like after perfect downsizing only (no extra sharpening), with bicubic for reference as the dotted line.

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Downsizing-Box-4X.png)

Next I plotted the MTF curves from three downsizing algorithms discussed in this thread ('D' option in the script): 'nodownsharp' quadratic, 'downsample' V1.22 and RobidouxSharp.  None were sharpened after downsizing, with 'downsample' set at a minimalist 1% DoG.  Quadratic and downsample 1% fare much worse than our benchmark bicubic while RobidouxSharp does a good job against it at the cost of a little extra energy beyond Nyqyist.

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/EWA-Comparison.png)

Lastly I decided to give Bart his due and show 'downsample' V1.22 as it was meant to be, with full 100% DoGs.  The following plot shows how the V 1.22 'downsample' blend works: downsizing first, followed by restoration:

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Bart-Comparison.png)

The 1% dotted red curve is the heavily attenuated result of its downsizing component.  With 100% 'sharpening' the DoGs component (solid red line) attempts to make up for the loss of energy in the desirable frequency range by amplifying the attenuated curve (and everything else including unwanted noise) back to where it thinks it should be.  I can't help but wish that both the attenuation and the amplification not be so drastic, although it does do well below Nyquist at the cost of an overshoot in the lower frequencies and lingering on in the higher ones.  

Thoughts?

Cheers,
Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 04:29:17 am
Roughly, RobidouxSharp is the "sharpest reasonable" cubic kernel when used with EWA. (Although some people like the look of EWA Catmull-Rom a.k.a. CatRom, which is even sharper, it is my opinion that this filter gives results which drift too far from "faithfulness". In my book, RobidouxSharp marks the end of "generally usable for EWA".)
EWA Robidoux is a good match for the Nyquist content of gently demosaicked Bayer source.
Even though EWA Robidoux is very popular for downsampling, there is some justification for using something slightly sharper, but not as sharp as RobidouxSharp. EWA Mitchell is a good start.
You could also directly choose the C value using "-define filter:c=YOUR_CHOSEN_VALUE -distort Resize". TTBOMK, you don't need to specify that you want a Cubic filter because the default (Robidoux) is one. In other words, the above should give the same as "-define filter:c=YOUR_CHOSEN_VALUE -filter Cubic -distort Resize". You can also set the "b" value instead of the "c" value, in which case the usable range is 1 (Spline = very soft) to 0 (CatRom = very sharp). "B" is amount of "blurriness" (easy enough to remember).
Terminology: EWA = Elliptical Weighted Averaging = what you get when you use "-distort Resize" instead of "-resize" with ImageMagick. It's also the basis of the "Jinc" methods of madVR. EWA is actually slowly spreading. It is also now available for Blender and a few other FLOSS image and video processing programs.)
P.S. Some people like the look of BC-cubics that are not Keys (for which 2B+C=1). In particular, methods with B+C=1 are popular. There is a strong theoretical basis to using Keys cubics, but image resampling requires compromise between conflicting requirements, which means that the above "strong theoretical basis" is not a dictate.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 28, 2015, 04:42:38 am
Roughly, RobidouxSharp is the "sharpest reasonable" cubic kernel when used with EWA. (Although some people like the look of EWA Catmull-Rom a.k.a. CatRom, which is even sharper, it is my opinion that this filter gives results which drift too far from "faithfulness". In my book, RobidouxSharp marks the end of "generally usable for EWA".)
EWA Robidoux is a good match for the Nyquist content of gently demosaicked Bayer source.
Even though EWA Robidoux is very popular for downsampling, there is some justification for using something slightly sharper, but not as sharp as RobidouxSharp. EWA Mitchell is a good start.
You could also directly choose the C value using "-define filter:c=value -distort Resize". TTBOMK, you don't need to specify that you want a Cubic filter because the default (Robidoux) is one. In other words, the above should give the same as "-define filter:c=YOUR_CHOSEN_VALUE -filter Cubic -distort Resize". You can also set the "b" value instead of the "c" value, in which case the usable range is 1 (Spline = very soft) to 0 (CatRom = very sharp). "B" is amount of "blurriness" (easy enough to remember).
Terminology: EWA = Elliptical Weighted Averaging = what you get when you use "-distort Resize" instead of "-resize" with ImageMagick. It's also the basis of the "Jinc" methods of madVR. EWA is actually slowly spreading. I think now available for Blender and a few other FLOSS image and video processing programs.)
P.S. Some people like the look of BC-cubics that are not Keys (for which 2B+C=1). In particular, methods with B+C=1 are popular. There is a strong theoretical basis to using Keys cubics, but image resampling requires compromise between conflicting requirements, which means that the above "strong theoretical basis" is not a dictate.

Thanks, Nicolas.  Are MTF curves even a good way to evaluate this stuff?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 04:49:56 am
Unless you really know what you are doing, I am pretty sure that you should not test any scheme that does not filter linear light for downsampling. (Sharpening, with USM, say, is a different story. You can use gamma light for that.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 04:53:50 am
Thanks, Nicolas.  Are MTF curves even a good way to evaluate this stuff?
They are very informative, but they do not tell the whole story. I read you report with great interest and am still thinking about it.
If there was one single objective function ("quality index") that correctly characterized "image quality", we would take a parameterized filter bank and blindly optimize. There is no such objective function. The door is not closed to "craft".
P.S. In other words, an absurd MTF is probably a strong hint that the method sucks. But there is more to faithfulness than a nice curve.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 28, 2015, 05:15:04 am
P.S. In other words, an absurd MTF is probably a strong hint that the method sucks. But there is more to faithfulness than a nice curve.

Makes sense.  I wonder if Bart's 'downsample' script plays nice with data that is 16-bit linear to start with?  If not I would have to remeasure it and RobidouxSharp.

:downsample
SET sharpAmount="?"
SET /p sharpAmount= Sharpening amount? (none=0, normal=50+, default=100):
IF /i %sharpAmount% == 0 GOTO nodownsharp
IF %sharpAmount% EQU "?" SET sharpAmount=100
ECHO.
ECHO Creating the Down-sampled image with output sharpening, please wait ...
convert ( -quiet "%~1" -type TrueColor -write "%~dp1IMsource.icc" -depth 16 -set colorspace sRGB -colorspace RGB ^
 -define filter:c=0.1601886205085204 -filter Cubic -distort Resize %mSize% ) ^
 ( -clone 0 -gamma 3 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:3,0,0.4981063336734057 -gamma 0.3333333333333333333 ) ^
 ( -clone 0 -define convolve:scale=^%sharpAmount%%%,100 ^
 -morphology Convolve DoG:3,0,0.4806768770037563 ) ^
 -delete 0 ^
 ( -clone 1 -colorspace gray -auto-level ) ^
 -compose over -composite ^
 -set colorspace RGB -colorspace sRGB ^
 -quality 100 -sampling-factor 4:4:4 ^
 -define png:preserve-iCCP ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 05:18:46 am
Jack: One thing your report makes clear (suggests, really...), is that Photoshop's bicubic is a good, balanced, scheme. (What a surprise.)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 05:46:50 am
Makes sense.  I wonder if Bart's 'downsample' script plays nice with data that is 16-bit linear to start with?  If not I would have to remeasure it and RobidouxSharp.
Not sure. Because of the gamma conversion, you may need a version of ImageMagick that computes in floating point. (Normally, ImageMagick uses 16bit integers as internal storage, which may be a bit thin with a script that does gamma conversion and is fed to frequency analysis.)
It's probably just fine as is. But I'm not sure.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 28, 2015, 05:51:10 am
Jack: One thing your report makes clear (suggests, really...), is that Photoshop's bicubic is a good, balanced, scheme. (What a surprise.)

Right, on the other hand it looks like the script assumes that incoming data is sRGB and it isn't, so it appears that I need to rework the measurements.  I have changed the last two graphs above to reflect the following code (RobidouxSharp as an example):

convert ( -quiet "%~1" -write "%~dp1IMsource.icc" ^
 -filter RobidouxSharp -distort resize %mSize% ) ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

Will this do if the input is 16 bit linear data?

Note that the high frequency 'hovering' has disappeared in the new 'linear' plots above.  It was obviously introduced by the incorrect sRGB <--> RGB round trips.  Here is the original (apparently incorrect) graph for reference, should anyone be interested

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Downsizing-Tool-Comparison-A.png)

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 05:58:52 am
convert ( -quiet "%~1" -write "%~dp1IMsource.icc" ^
 -filter RobidouxSharp -distort resize %mSize% ) ^
 -quality 100 -sampling-factor 4:4:4 ^
 -profile "%~dp1IMsource.icc" -compress Lossless "%~dpn1_%mSize%%~x1"

Will this do if the input is 16 bit linear data?
It should. TTBOMK, ImageMagick will process the data literally "as is", which means that it will leave (16 bit) linear light as (16 bit) linear light. Note that a lot of the commands are to accommodate jpg, png and a reasonable variety of TIFF. If you know you're using png or TIFF (Thousands of Incompatible File Formats) without destructive compression, you can drop "-quality 100 -sampling-factor 4:4:4 ^" (there, really, for JPEGs or at least JPEG compression) and "-compress Lossless" (probably a good thing to leave it in for TIFFs, but not needed for png).
Warning: All my "ImageMagick syntax comments" are off the top of my head. I've used it a lot. But to be safe I should RTFM. And I'm not.
P.S. Note that the above should pretty much only be used to produce TIFF, because if you output to, say, JPEG or PNG, most image viewers will ignore the ICC and assume it's sRGB (especially true with JPEG, less so with PNG). It's to avoid such complications that Bart's script specifically assumes sRGB as input and output.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on March 28, 2015, 06:09:59 am
Thanks for this, Nicolas and Dogway.

Just to throw in a slightly different perspective, I decided to measure MTF curves of a downsized ISO100 D800e slanted edge using the different methods discussed in the latter part of this thread.  The original image is a 300x400 pixel crop directly off DSC_6483.NEF (I downloaded it from dpr and so can you if you so wish) saved as a tiff with absolutely zero processing other than CFA normalization.  I think it's as close as one can get to the actual spatial resolution information captured by the camera.

Hi Jack,

Yes, a slanted edge can reveal lots of useful information which is why I use it a lot, but it doesn't give a complete picture. It e.g. can only suggest that aliasing is possible (or not), but it doesn't know if the original subject had enough contrast to even have a modulation that can cause aliasing. A high contrast edge will survive, but say tiny detail (and small features in digital imaging by definition equal low MTF modulation) may suffer enough loss of contrast due to (re)sampling to become unrecoverable, even with deconvolution. Think about detail with 1% subject contrast (still detectabe by humans), and an MTF response of 10%. That would render the detail at 0.1% modulation, which might be below the quantization/noise threshold.

The MTF view also doesn't reveal the sensitivity for the generation of blocking artifacts or ringing artifacts. Especially the EWA types of resampling (-distort Resize) behave in a very 'organic' way, which avoids some of these types of artifacts much better than tensor type of resampling (-resize). The traditional tensor type of resampling has a higher diagonal resolution, which is nice, but it does cause trouble when we start to push things to the limit. That's why I additionally test with a zoneplate (rings) target, and a star target, and challenging images such as my windmil sample image.

The D800e edge crop does have a bit of a ragged, zipper like, edge structure which is something to be wary about, but I don't think it is too much of an issue for downsampling to 25% or less of the original size. Still, for more objective tests I'd prefer to base my conclusions on a really smooth and sharp edge. One can even compare edges with different contrast, to better judge some (unwanted) effects for really high spatial detail. I can easily generate such edges if needed, at any required angle, but the ISO standard uses an arctan(0.1)=5.71 degrees angle so that's what I usually make (a 1/10 pixel slope is also easy for analysis of CGI versions, camera shots are usually rotated slightly and their slope must be measured).

Quote
That beautiful edge was first measured with MTF Mapper (http://sourceforge.net/projects/mtfmapper/) - then downsized 4:1 using Photoshop's standard methods and re-measured.
[...]
The original is soaring at those lofty heights because one of its pixels corresponds to four of the others'.  Nearest neighbour tracks it well, which is good at the lower frequencies, not so good beyond Nyquist because the ideal SFR/MTF curve for a perfect downsizing would track the original's up to Nyquist - but show little or no energy above 0.5 cycles/pixel, the point at which aliasing and moirè start rearing their ugly heads. Bilinear and bicubic show different amounts of attenuation, with bicubic looking like the best compromise.

 The blue curve below shows the upper boundaries of what I think an ideal MTF curve should look like after perfect downsizing only (no extra sharpening), with bicubic for reference as the dotted line.

True, but again watch out for bicubic's (poor) blocking and ringing behavior, therefore also other tests are required.

Quote
Next I plotted the MTF curves from three downsizing algorithms discussed in this thread ('D' option in the script): 'nodownsharp' quadratic, 'downsample' V1.22 and RobidouxSharp.  None were sharpened after downsizing, with 'downsample' set at a minimalist 1% DoG.  Quadratic and downsample 1% fare much worse than our benchmark bicubic while RobidouxSharp does a good job early on and bests it up to Nyquist.  Note, however, how all three have an unwelcome tendency to hover on in the higher frequencies, letting through faux energy there, a fact potentially indicative of the high frequency trouble discussed earlier in the thread.

Yes, but keep in mind that lower contrast small detail will have low MTF to begin with, so down-sampling will only reduce the chance of it having a meaningful modulation after down-sampling. Which is also why I use deconvolution sharpening without too much hesitation. Some of the noise is probably unrecoverable already, while still meaningful detail modulation will be boosted.

Quote
Looking at the MTF curve of RobidouxSharp one would almost feel like throwing a sharp low-pass at its output past Nyquist.  What would that do to the frequencies below Nyquist?  Would it kill its slim advantage over bicubic there?

That's the choice I made after studying the other artifacts. It seems to be better to down-sample a bit soft and deconvolve, than to down-sample sharp and blur/convolve. The sharper down-sampling generates to many compromise artifacts, and resampling generally requires a bit of sharpening to restore some resampling losses but that's not good when there already are artifacts present.

 
Quote
Lastly I decided to give Bart his due and show 'downsample' V1.22 as it was meant to be, with full 100% DoGs.

Just a small addition, 50% is a neutral 1 iteration deconvolution, 100% is default because it adds a bit of extra sharpening that often is visually pleasing.

Quote
The 1% dotted red curve is the heavily attenuated result of its downsizing component.  With 100% 'sharpening' the DoGs component (solid red line) attempts to make up for the loss of energy in the desirable frequency range by amplifying the attenuated curve (and everything else including unwanted noise) back to where it thinks it should be.  I can't help but wish that both the attenuation and the amplification not be so drastic.  Even so the method does not quite achieve bicubic's apparently effortless performance.

The benchmark seems hard to beat when looking at these curves off grayscale raw data.

Thoughts?

Keep the earlier comments in mind. Lower contrast detail may already be lost, so the potentially risky boost of aliasing prone spatial frequencies may be relatively harmless. Also check for other artifacts, the rings target is cruel enough to reveal such poor behavior in 2D. Gamma effects also play a big role in generating or suppressing artifacts.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 06:13:46 am
Note that the high frequency 'hovering' has disappeared.  It was obviously introduced by the incorrect sRGB <--> RGB round trips.
It was worse than that: It was converting the linear RGB to linear RGB from the get go as if it was sRGB to start with. One way trip that was never undone, and also got in the way of the mild noise/quantization cancellation one gets for free when convolving in "true" linear light.
P.S. I actually was wondering where the "high noise floor" was coming from.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 28, 2015, 06:14:48 am
Bart, please note that I apparently incorrectly fed your script 16-bit linear data, so I reworked the last two graphs in the original post to reflect the data type.  Results are quite a bit different as you can read above.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 28, 2015, 06:29:20 am
Just a small addition, 50% is a neutral 1 iteration deconvolution, 100% is default because it adds a bit of extra sharpening that often is visually pleasing.

Since I have the stuff out, it's easy enough to do one more. Here is 'downsample' with 50% sharpening for reference

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Bart-Comparison-1-50-lin.png)
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on March 28, 2015, 06:30:37 am
Jack: One thing your report makes clear (suggests, really...), is that Photoshop's bicubic is a good, balanced, scheme. (What a surprise.)

Hi Nicolas,

That's why it's been a default in Photoshop and other image editors for so long, but Mitchell Netravali may be better though. However, it was the poor down-sampling quality (aliasing, blocking ringing, you name it, artifacts) that encouraged me into further investigations. A down-sampling of my windmill image exposes some of the shortcomings in real images, and CGI targets prove its weaknesses.

The EWA approach already helps keeping a natural look and suppress certain artifacts, linear gamma color blending helps (although with some brightening of high spatial frequencies), your gamma conversions help to mitigate the formation of halos. IMHO that all produces superior image quality compared to bicubic (of which Photoshop's is a specific tweaked version).

The brightening of the higher spatial frequency content by the way, can be helpful in inkjet output, which tends to darken high spatial frequencies due to ink diffusion. But I'd prefer a bit more control for e.g. web publishing which doesn't darken those frequencies. I may sign up for the ImageMagick discourse server to ask for some assistance in getting my Blend-if tweak implemented, which might be just what the doctor ordered QED...

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 06:30:47 am
To use Bart's script with linear light input and output, get rid of "-set colorspace sRGB -colorspace RGB" and "-set colorspace RGB -colorspace sRGB ^". The first command says "tag the input as being sRGB, and convert it to linear RGB with sRGB primaries" and the second says "tag the result so far as being linear RGB with sRGB primaries, and convert it to sRGB". This does not need to be done since your input is in linear light, and you want to output to the same linear light.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 28, 2015, 06:37:23 am
...(although with some brightening of high spatial frequencies)...
Unfortunately, this is no small matter. Then, again, balancing artifacts is a ... balancing act. (Switch to gamma 2 instead of 3?) I agree with everything else you have to say about the scheme. (Although I'm really not sure about filtering "softer" than post-sharpening. It makes sense. I'm just not sure it's better overall than just downsampling with a filter that has the desired sharpness from the get go.)
P.S. Thank you Bart.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on March 28, 2015, 07:44:26 am
Since I have the stuff out, it's easy enough to do one more. Here is 'downsample' with 50% sharpening for reference

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Bart-Comparison-1-50-lin.png)
(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Bart-Comparison.png)


Thanks Jack, that shows that when comparing 1%, 50% and 100%, the deconvolution 'amount' that gets mixed in with the original offers a nice range of control. Technically, the 100% will lead to a bit of over-correction halo which may be reduced by changing the Gaussian Radius of the DoG.

When you select a 'Sharpening amount' less than zero (effectively a Gaussian blur), e.g. "-1", you'll be offered an optional deconvolution on the already gamma blending down-sampled image. The Blur width, or radius of blur, can be increased to boost lower spatial frequencies more than the default sharpening radius would. Values between the minimum of 1.0 and 1.28 create a more narrow Gaussian that boosts higher spatial frequencies, above 1.28 it will boost lower spatial frequencies. Since that becomes more a creative sharpening than exact restoration, one needs to experiment with the required amount.

Cheers,
Bart

P.S. One can modify the conditional jump to the optional deconvolution after the ':downsample' instructions to allways add additional convolution if one want to combine the standard radius deconvolution with an additional boost of other spatial frequencies.

Just change:
REM Experimental
IF /i %sharpAmount% LEQ 0 GOTO addDeconv

to:
GOTO addDeconv
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 28, 2015, 09:11:03 am
Hi Jack,

Yes, a slanted edge can reveal lots of useful information which is why I use it a lot, but it doesn't give a complete picture. It e.g. can only suggest that aliasing is possible (or not), but it doesn't know if the original subject had enough contrast to even have a modulation that can cause aliasing. A high contrast edge will survive, but say tiny detail (and small features in digital imaging by definition equal low MTF modulation) may suffer enough loss of contrast due to (re)sampling to become unrecoverable, even with deconvolution. Think about detail with 1% subject contrast (still detectabe by humans), and an MTF response of 10%. That would render the detail at 0.1% modulation, which might be below the quantization/noise threshold.

The MTF view also doesn't reveal the sensitivity for the generation of blocking artifacts or ringing artifacts. Especially the EWA types of resampling (-distort Resize) behave in a very 'organic' way, which avoids some of these types of artifacts much better than tensor type of resampling (-resize). The traditional tensor type of resampling has a higher diagonal resolution, which is nice, but it does cause trouble when we start to push things to the limit. That's why I additionally test with a zoneplate (rings) target, and a star target, and challenging images such as my windmil sample image.

The D800e edge crop does have a bit of a ragged, zipper like, edge structure which is something to be wary about, but I don't think it is too much of an issue for downsampling to 25% or less of the original size. Still, for more objective tests I'd prefer to base my conclusions on a really smooth and sharp edge. One can even compare edges with different contrast, to better judge some (unwanted) effects for really high spatial detail. I can easily generate such edges if needed, at any required angle, but the ISO standard uses an arctan(0.1)=5.71 degrees angle so that's what I usually make (a 1/10 pixel slope is also easy for analysis of CGI versions, camera shots are usually rotated slightly and their slope must be measured).

True, but again watch out for bicubic's (poor) blocking and ringing behavior, therefore also other tests are required.

Yes, but keep in mind that lower contrast small detail will have low MTF to begin with, so down-sampling will only reduce the chance of it having a meaningful modulation after down-sampling. Which is also why I use deconvolution sharpening without too much hesitation. Some of the noise is probably unrecoverable already, while still meaningful detail modulation will be boosted.

That's the choice I made after studying the other artifacts. It seems to be better to down-sample a bit soft and deconvolve, than to down-sample sharp and blur/convolve. The sharper down-sampling generates to many compromise artifacts, and resampling generally requires a bit of sharpening to restore some resampling losses but that's not good when there already are artifacts present.

 
Just a small addition, 50% is a neutral 1 iteration deconvolution, 100% is default because it adds a bit of extra sharpening that often is visually pleasing.

Keep the earlier comments in mind. Lower contrast detail may already be lost, so the potentially risky boost of aliasing prone spatial frequencies may be relatively harmless. Also check for other artifacts, the rings target is cruel enough to reveal such poor behavior in 2D. Gamma effects also play a big role in generating or suppressing artifacts.

Cheers,
Bart

Thanks Bart, I appreciate your comments.  I think one of the key take-aways for me from this exercise was realizing that the original MTF curve is multiplied by the inverse of the magnification factor (4 X in this case, with the 25% downsampling we've been doing above) when plotted on the same scale as the downsized MTFs.  Therefore what really matters as far as final image spatial resolution information is concerned is the portion of the original MTF curve that is closest to the origin.  That portion of the curve is more easily associated with what photographers would call 'contrast', rather than 'sharpness', in the original image.  

I had explored this subject here (http://www.strollswithmydog.com/mtf50-perceived-sharpness/) in a different context in the past but only now get the connection  :)

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on March 29, 2015, 05:03:29 am
Jack: Comparing the MTF derived from an actual photograph with the MTF after resampling is worthwhile. Thank you.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on March 29, 2015, 04:15:52 pm
Mon plaisir.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on April 03, 2015, 04:58:51 am
Jack:
Would you be kind enough to the do another set of graphs, ideally showing the output of Robidoux, Mitchell and RobidouxSharp, along with (PS) Bicubic and of course the scaled "raw MTF", and focusing on what happens up to Nyquist (so you may want to raise the "x-axis"). I'm mostly interested in how things are tracked at lowish frequency.
P.S. Have you noticed that Bicubic crosses the Nyquist frequency pretty much at half of the scaled "original" MTF? It's probably not totally coincidental.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on April 03, 2015, 11:36:50 am
Jack:
Would you be kind enough to the do another set of graphs, ideally showing the output of Robidoux, Mitchell and RobidouxSharp, along with (PS) Bicubic and of course the scaled "raw MTF", and focusing on what happens up to Nyquist (so you may want to raise the "x-axis"). I'm mostly interested in how things are tracked at lowish frequency.
P.S. Have you noticed that Bicubic crosses the Nyquist frequency pretty much at half of the scaled "original" MTF? It's probably not totally coincidental.

Sure Nicolas.  The blue lines are Robidouxs and red is Mitchell.  I've noticed that CS5's implementation of bicubic is 'sharper' than Matlab's (which I understand uses the original Key values) so I've show it as the red dots.  Interestingly it tracks Mitchell almost all the way.

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Full.png)

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Half.png)

(http://www.strollswithmydog.com/wordpress/wp-content/uploads/Third.png)

Keep in mind that this edge was captured with a camera without an AA filter.

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on April 04, 2015, 03:25:02 am
Thank you Jack. It's interesting to see Bicubic overshoot the "original" MTF at .15 cycles/pixel. It's also interesting to see the sharpest filters show a "kink" in the MTF at about .05 (although the original has this strange flat line which ends with a corner, so it could be an echo of that somehow).
Also note that I agree with Bart that the "slanted wedge" is not isotropic enough a situation to tell "everything". It's too close to a separable situation, so issues that arise from the use of a separable (= tensor = orthogonal) filter are pretty much swept under the carpet.
-----
Of course I'm most interested in photographs taken without an AA filter.
-----
I am pretty sure that if the "resampling with adaptive gamma blending" approach is to work best, blending the linear and gamma versions of a RGB channel based on its own value instead of luminance/luma (which is a cross-channel measure) is better overall. In other words, the R channel is resampled based on R values, and only R values, instead of having it depend on the luminance (or luma).
No time to update the comparisions/code, unfortunately.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on April 04, 2015, 05:43:32 am
Thank you Jack. It's interesting to see Bicubic overshoot the "original" MTF at .15 cycles/pixel. It's also interesting to see the sharpest filters show a "kink" in the MTF at about .05 (although the original has this strange flat line which ends with a corner, so it could be an echo of that somehow).

Yes it is, although perhaps what we are seeing is simply due to data quantization.  MTF Mapper computes and outputs MTF (SFR) data from 0 to 1 cycle/pixel in increments of 1/64th of a pixel, that's every 0.0156 cy/px. Because the image has been downscaled 4:1, if the x-axis in cy/px is referenced to the downsampled data, as in this case,  the original curve's needs to be multiplied by four; so it has 1/4 the number of data points on the same scale as downsampled data, only every 0.0625 cy/px.

Therefore for the original MTF curve the actual output is only at 0, 0.06, 0.125, 0.19, 0.25, etc. the dashed line being linear interpolation by matlab's plotting function.  And at those points CS5 bicubic seems to pretty well match it.  We are looking at these curves a little closer than they were meant to be looked at :)

Also note that I agree with Bart that the "slanted wedge" is not isotropic enough a situation to tell "everything". It's too close to a separable situation, so issues that arise from the use of a separable (= tensor = orthogonal) filter are pretty much swept under the carpet.

I see (stored away for future better understanding;-)

Jack
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on April 26, 2015, 04:44:05 am
Jack:
Actually, something that is more likely to have "swept things under the carpet" more than using the slanted edge is having downsampled by exactly 50%.
Any halfway (no pun intended) reasonable scheme does OK at 50%, at least visually (if one uses the "align corners" image geometry convention, which is pretty much the standard when downsampling). That is: 50% is the most friendly resampling ratio. There is more difference between schemes when other ratios are used.
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Jack Hogan on April 27, 2015, 03:57:35 pm
Jack:
Actually, something that is more likely to have "swept things under the carpet" more than using the slanted edge is having downsampled by exactly 50%.
Any halfway (no pun intended) reasonable scheme does OK at 50%, at least visually (if one uses the "align corners" image geometry convention, which is pretty much the standard when downsampling). That is: 50% is the most friendly resampling ratio. There is more difference between schemes when other ratios are used.

I believe I downsampled to 25% of the original.  How friendly is 50% of 50%?
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: NicolasRobidoux on April 27, 2015, 04:02:31 pm
I believe I downsampled to 25% of the original.  How friendly is 50% of 50%?
I thought I had read somewhere that you had downsampled in such a way that the total number of pixels after resizing was 25% of the original. In terms of ratios of side lengths, this corresponds to 50% (.5*.5=.25).
Do I remember wrong or misunderstand?
P.S. "The original is soaring at those lofty heights because one of its pixels corresponds to four of the others'." You meant sixteen?
P.S. Indeed, you did use 25% (side length ratio): The ImageMagick command is right there. You did mean sixteen.
25% is not quite as friendly as 50% (although it's still an integer ratio, which means that the convolution weights are exactly the same for every output pixel, just shifted, which may reduce aliasing).
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on May 01, 2016, 03:48:14 pm
ImageMagick 7 with HDRI is out http://www.imagemagick.org/script/binary-releases.php
The script does not work anymore... :(
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on May 01, 2016, 05:10:56 pm
ImageMagick 7 with HDRI is out http://www.imagemagick.org/script/binary-releases.php
The script does not work anymore... :(

Hi Pictus.

That's what I was afraid would be happening. Back to the drawingboard, when I have some time.

On the plus-side, the new version's binary distributions will also open up other possibilities, including floating point number calculations and operations in the Fourier (frequency) domain, not only the spatial domain.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Pictus on May 01, 2016, 07:05:45 pm
Thanks BartvanderWolf.

Hehehehe...
New interesting stuff to play :)

BTW, changing the word "convert" to "magick" makes it work, but something strange happened...
I got purple catchlights with this photo https://dl.dropboxusercontent.com/u/4686872/TAM63_DOG_Pictus-FX.jpg
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on May 02, 2016, 03:58:35 am
Thanks BartvanderWolf.

Hehehehe...
New interesting stuff to play :)

BTW, changing the word "convert" to "magic" makes it work, but something strange happened...
I got purple catchlights with this photo https://dl.dropboxusercontent.com/u/4686872/TAM63_DOG_Pictus-FX.jpg

Cute dog. Yes, issues will need to be traced to the differences in how the commands now work, in detail. That'll take some time for digging in the documentation, and testing different options or alternative methods.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 11, 2016, 10:32:06 am
Hi

A question:

After making a very good smaller size image, it's probably used on the web ;-)

Most of the time there's a need to make the images rather small in file size.

In the script "-sampling-factor 4:4:4' is used, but I read that this is not optimal from the moment that file size does matter.  I read that "4:2:0" would give better quality for the same file size.
Any suggestions?

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 11, 2016, 11:11:44 am
Hi

A question:

After making a very good smaller size image, it's probably used on the web ;-)

Most of the time there's a need to make the images rather small in file size.

In the script "-sampling-factor 4:4:4' is used, but I read that this is not optimal from the moment that file size does matter.  I read that "4:2:0" would give better quality for the same file size.
Any suggestions?

Hi Alain,

Feel free to change it for the purpose you use it for. The script can do both downsampling and upsampling, and attempts to preserve the highest quality (in case further processing will be done), which will also result in relatively large file sizes. Actually, 4:4:4 is higher quality, but also larger file size. So for web publishing, you could change it to 4:2:2, or 4:2:0, or 4:1:1, to conserve some file size and speed up the image transfer and page loading times.

Cheers,
Bart
Title: Re: A free high quality resampling tool for ImageMagick users
Post by: alain on September 11, 2016, 12:09:26 pm
Hi Alain,

Feel free to change it for the purpose you use it for. The script can do both downsampling and upsampling, and attempts to preserve the highest quality (in case further processing will be done), which will also result in relatively large file sizes. Actually, 4:4:4 is higher quality, but also larger file size. So for web publishing, you could change it to 4:2:2, or 4:2:0, or 4:1:1, to conserve some file size and speed up the image transfer and page loading times.

Cheers,
Bart
Bart

I've already have several adapted versions of the script, all with superb results.  Thanks again for the script.

I was more inquiring to experiences about using 4:2:0 versus 4:4:4 and what sort of file size changes that can give with almost the same visible quality in "web use".

Title: Re: A free high quality resampling tool for ImageMagick users
Post by: Bart_van_der_Wolf on September 11, 2016, 01:38:28 pm
Bart

I've already have several adapted versions of the script, all with superb results.  Thanks again for the script.

I was more inquiring to experiences about using 4:2:0 versus 4:4:4 and what sort of file size changes that can give with almost the same visible quality in "web use".

The resulting file size difference is very hard to predict, and it varies with color and resolution content per image. So an image of the same subject in Blue light or Red light will have significantly different compression factors with 4:2:0, but less different with 4:4:4 .

One can aim for a file size in bytes with ImageMagick, but that mostly varies the compression quality. So that will produce variable output quality. When I want to tweak the file size, I use a Windows program called RIOT (http://luci.criosweb.ro/riot/) (no idea if something similar exists for Mac OS), and that calculates the file size with different subsampling quality and compression settings. If you use that on typical image content then you may find a good average setting.

A program like JPEGmini (http://www.jpegmini.com/) does it fully automatic, after analyzing the image content it sets the parameters for the visually least noticeable degradation level one aims for, on a per image basis.

Cheers,
Bart