snibgo's ImageMagick pages

Adaptive blur and sharpen

Modulating the effect by the existing sharpness.

We sometimes want to blur areas that are already blurred, or sharpen areas that are already sharp, or both, or some other combination.

The Slopes page shows how we can do this by modulating the blur by the slope. This page develops that method.

The script on this page applies a Gaussian (circular) blur. A related effect applies a linear blur/sharpen, either parallel or perpendicular to contours. See adapContBlrShp.

Sample input

We will demonstrate on the usual image:

set SRC=toes.png
toes.pngjpg

The script

The script adapBlrShp.bat blurs or sharpens an image, with the greatest effect either where there is most detail (the edges) or where there is least detail (non-edges).

Parameter Description
%1 Input image (mandatory parameter)
%2 Output image
%3 One of:
  • E blur at the edges (where the mask is light)
  • N blur at the non-edges (where the mask is dark)
Default: N
%4 Edge detector sigma
Default: 10
%5 Blur sigma
Default: half of %4
%6 Blend percentage
  • 0 for no effect
  • >0 for blur
  • <0 for sharpen
Default: 100
%7 Colorspace for operation

The input is mandatory. All other parameters are optional. As usual, a dot "." may be used as a placeholder.

The script adapBlrShp.bat blurs an image using a mask that is white where there is most detail, and black where there is least detail. By default the mask is negated, so the blur is greatest where there is least detail. The blurred result is blended with the input at a certain percentage. The default percentage is 100 so the result is the blur. Alternative percentages include zero so there is no effect, and -100 so the effect is a sharpening. Other values can be used, eg 50 for a reduced blur or 150 for an increased blur.

Blur sigma is the primary control for the amount of blurring, with positive blend percentages between 0 and 100 aimed at animation control. (Blending blur with the original creates ghosting.) However, negative blend percentages are used for sharpening by unsharp-mask.

Blend interpolation (between 0 and 100, inclusive) is unlikely to cause OOG and hence clipping. But blend extrapolation (negative values) is likely to cause OOG and hence clipping. Indeed, if the input is auto-levelled, extrapolation is almost certain to cause clipping. See Preventing clipping below.

By default, the mask is made from the magnitude of the slopes of a grayscale version of the input, where the slopes are found by the comet blur method. However, a user-supplied mask can be used instead.

There are four basic modes of operation:

  1. blur non-edges
  2. blur edges
  3. sharpen non-edges
  4. sharpen edges

Blur non-edges (default)

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_ex1.png
abs_ex1.pngjpg

Blur the edges

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_ex2.png E
abs_ex2.pngjpg

Sharpen non-edges

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_ex3.png . . . -100
abs_ex3.pngjpg

Sharpen the edges

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_ex4.png E . . -100
abs_ex4.pngjpg

Over-sharpen the non-edges

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_ex5.png N . . -200
abs_ex5.pngjpg

Over-sharpen the edges

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_ex6.png E . . -200
abs_ex6.pngjpg

Mask

Instead of the default mask (and default processing of that mask), the user can supply a mask:

Make a mask

%IM%convert ^
  -size 267x233 ^
  radial-gradient: ^
  abs_radmask.png
abs_radmask.png

Apply the mask

set absRD_MASK=abs_radmask.png

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_rm1.png

set absRD_MASK=
abs_rm1.pngjpg

Apply the mask, blurring the "edges"
(where the mask is white)

set absRD_MASK=abs_radmask.png

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_rm2.png E

set absRD_MASK=
abs_rm2.pngjpg

Sigmas

There are two sigma controls: one for the edge detector, and one for the actual blur.

Large values for the mask sigma don't work well:

Blur non-edges

set absWR_MASK=abs_msk_s1.png

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_s1.png N 30

set absWR_MASK=
abs_s1.pngjpg abs_msk_s1.pngjpg

The mask from a comet blur is streaky. We can blur before the stretch to remove the streaks, and after to smooth the boundary:

Blur non-edges

set absWR_MASK=abs_msk_s2.png
set absMSK_PROC=-blur 0x5 -contrast-stretch 40%%x50%% -blur 0x5

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_s2.png N 10 15

set absMSK_PROC=
set absWR_MASK=
abs_s2.pngjpg abs_msk_s2.pngjpg

Colorspace

Any operation that mixes colours from a number of pixels, such as blur and sharpen, is more correctly done in linear colorspace. (Well, that should read scene-referred colorspace, but we assume linear is a good approximation.)

call %PICTBAT%abs_colsp %SRC%
Colorspace blur non-edges blur edges sharpen non-edges sharpen edges
sRGB abs_csnb_sRGB.pngjpg abs_cseb_sRGB.pngjpg abs_csns_sRGB.pngjpg abs_cses_sRGB.pngjpg
RGB abs_csnb_RGB.pngjpg abs_cseb_RGB.pngjpg abs_csns_RGB.pngjpg abs_cses_RGB.pngjpg
Lab abs_csnb_Lab.pngjpg abs_cseb_Lab.pngjpg abs_csns_Lab.pngjpg abs_cses_Lab.pngjpg

There is practically no difference between the colorspace versions.

Blur and sharpen

We can call the script to blur, and call it again to sharpen that result:

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_bands.png

call %PICTBAT%adapBlrShp ^
  abs_bands.png ^
  abs_bands.png E . . -100
abs_bands.pngjpg

Or we can save the mask used in the blur, and use the same mask for sharpening.

set absWR_MASK=abs_msk.png

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_bands2.png

set absWR_MASK=

We show the intermediate result and the mask.

abs_bands2.pngjpg abs_msk.pngjpg
set absRD_MASK=abs_msk.png

call %PICTBAT%adapBlrShp ^
  abs_bands2.png ^
  abs_bands2s.png E . . -100

set absRD_MASK=
abs_bands2s.pngjpg

The default settings give a fairly sharp line between the blurred and sharpened areas. We can cure this by setting absMSK_PROC:

set absMSK_PROC=-contrast-stretch 40%%x40%% -blur 0x5
set absWR_MASK=abs_msk3.png

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_bands3.png

set absWR_MASK=
set absMSK_PROC=

We show the intermediate result and the mask.

abs_bands3.pngjpg abs_msk3.pngjpg
set absRD_MASK=abs_msk3.png

call %PICTBAT%adapBlrShp ^
  abs_bands3.png ^
  abs_bands3s.png E . . -100

set absRD_MASK=
abs_bands3s.pngjpg

Preventing clipping

Extrapolation can cause clipping. Speaking more correctly, it can cause values to go out of the range 0 to 100%, which is out of gamut (OOG), which will be clipped by most file formats.

call %PICTBAT%adapBlrShp ^
  %SRC% ^
  abs_shp.miff E . . -200

%IM32f%convert ^
  abs_shp.miff ^
  -format "min=%%[fx:minima]  max=%%[fx:maxima]\n" ^
  info: 
min=-0.361418  max=1.55134
abs_shp.miffjpg

The script oogPower.bat pushes values back into gamut. See Putting OOG back in the box.

call %PICTBAT%oogPower ^
  abs_shp.miff abs_shp2.miff

%IM32f%convert ^
  abs_shp2.miff ^
  -format "min=%%[fx:minima]  max=%%[fx:maxima]\n" ^
  info: 
min=5.2359e-30  max=1
abs_shp2.miffjpg

Scripts

For convenience, .bat scripts are also available in a single zip file. See Zipped BAT files.

adapBlrShp.bat

rem From image %1,
rem makes output %2 blurred most at edges, or most at non-edges.
rem %3 E=blur at the edges, or N=blur at non-edges.
rem %4 edge detector sigma
rem %5 blur sigma
rem %6 blend percentage: 0=no effect, 100=blur, -100=sharpen
rem %7 colorspace
@rem
@rem Also uses:
@rem   absWR_MASK if not blank, writes mask to this file
@rem   absRD_MASK if not blank, reads mask from this file
@rem   absMSK_PROC
@rem   absMSK_PRE


@if "%1"=="" findstr /B "rem @rem" %~f0 & exit /B 1

@setlocal enabledelayedexpansion

rem @call echoOffSave

call %PICTBAT%setInOut %1 abs

if not "%2"=="" if not "%2"=="." set OUTFILE=%2

set BLR_AT=%3
if "%BLR_AT%"=="." set BLR_AT=
if "%BLR_AT%"=="" set BLR_AT=N

set EDGE_SIG=%4
if "%EDGE_SIG%"=="." set EDGE_SIG=
if "%EDGE_SIG%"=="" set EDGE_SIG=10

set BLUR_SIG=%5
if "%BLUR_SIG%"=="." set BLUR_SIG=
if "%BLUR_SIG%"=="" set /A BLUR_SIG=%EDGE_SIG%/2

set BLEND_PC=%6
if "%BLEND_PC%"=="." set BLEND_PC=
if "%BLEND_PC%"=="" set BLEND_PC=100

set COLSP=%7
if "%COLSP%"=="." set COLSP=

set TMPDIR=\temp\
set TMPGRAY=%TMPDIR%abs_gray.miff
set TMPMAG=%TMPDIR%abs_gray_mag.miff

if /I "%BLR_AT%"=="E" (
  set sNEG=
) else if /I "%BLR_AT%"=="N" (
  set sNEG=-negate
) else (
  echo Bad BLR_AT [%BLR_AT%] should be E or N.
  exit /B 1
)

if "%absMSK_PROC%"=="" set absMSK_PROC=-contrast-stretch 45%%x45%%
if "%absMSK_PROC%"=="." set absMSK_PROC=

if "%absRD_MASK%"=="" (
  set sRD_MASK=%TMPMAG% -auto-level %absMSK_PROC%
) else (
  set sRD_MASK=%absRD_MASK%
)

if "%absWR_MASK%"=="" (
  set sWR_MASK=
) else (
  set sWR_MASK=+write %absWR_MASK%
)


if "%IM32f%"=="" call %PICTBAT%setim8

set PREV_COLSP=
for /F "usebackq" %%L in (`%IM32f%convert ^
  %INFILE% ^
  -format "PREV_COLSP=%%[colorspace]" +write info: ^
  -colorspace Gray ^
  %absMSK_PRE% ^
  %TMPGRAY%`) do set %%L
if "%PREV_COLSP%"=="" exit /B 1
echo PREV_COLSP=%PREV_COLSP%

set COLSP_IN=
set COLSP_OUT=
if "%COLSP%" NEQ "" if /I "%COLSP%" NEQ "PREV_COLSP" (
  set COLSP_IN=-colorspace %COLSP% -set colorspace sRGB
  set COLSP_OUT=-set colorspace %COLSP% -colorspace %PREV_COLSP%
)

call %PICTBAT%slopeXYblMag ^
  %TMPGRAY% %TMPMAG% %EDGE_SIG%

%IM32f%convert ^
  %INFILE% ^
  %COLSP_IN% ^
  ( +clone ^
    ( %sRD_MASK% ^
      %sWR_MASK% ^
      %sNEG% ^
    ) ^
    -compose Blur ^
    -set option:compose:args %BLUR_SIG%x%BLUR_SIG% ^
    -composite ^
  ) ^
  -compose Blend -define compose:args=%BLEND_PC% -composite ^
  %COLSP_OUT% ^
  +depth ^
  -depth 32 ^
  -define "quantum:format=floating-point" ^
  %OUTFILE%

if ERRORLEVEL 1 exit /B 1


call echoRestore

@endlocal & set absOUTFILE=%OUTFILE%

All images on this page were created by the commands shown, using:

%IM%identify -version
Version: ImageMagick 6.9.5-3 Q16 x86 2016-07-22 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG to JPG.

Source file for this web page is adapblrshp.h1. To re-create this web page, execute "procH1 adapblrshp".


This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v1.0 4-December-2017.

Page created 04-Dec-2017 15:52:57.

Copyright © 2017 Alan Gibson.