snibgo's ImageMagick pages

De-anti-aliasing

When one image has anti-aliased over another, we can reverse the process. This can also be used for unfocused photopgraphs, but it isn't a general de-blurrer.

Input images

The process needs three input images:

From these three images, we can restore the anti-aliased pixels to an approximation of either the inner or the outer pixels.

blah.

%IM%convert ^
  -size 200x300 gradient: -rotate 90 ^
  -fill #f00 -draw "circle 100,100 150,100" ^
  -fill #0f0 -draw "circle 175,100 225,100" ^
  -colorspace RGB ^
  -blur 0x3 ^
  -colorspace sRGB ^
  daa_src.png
daa_src.png

Aside: In this case, blurring in RGB space is better than in sRGB, as that gives over-dark transitions:

%IM%convert ^
  -size 200x300 gradient: -rotate 90 ^
  -fill #f00 -draw "circle 100,100 150,100" ^
  -fill #0f0 -draw "circle 175,100 225,100" ^
  -blur 0x3 ^
  daa_src_srgb.png
daa_src_srgb.png

The masks might be generated with any method. Here, we set the inner mask to pixels that are 100% saturated, and the outer mask to pixels that are 0% saturated.

%IM%convert ^
  daa_src.png ^
  -colorspace HSL ^
  -channel G -separate +channel ^
  -set colorspace sRGB ^
  ( +clone  ^
    -fill #000 +opaque #fff ^
    -write daa_mask_in.png +delete ) ^
  ( +clone ^
    -fill #fff +opaque #000 ^
    -write daa_mask_out.png +delete ) ^
  NULL:
daa_mask_in.png daa_mask_out.png

Make a gradient between the inner and outer:

%IM%convert ^
  daa_mask_in.png ^
  -negate ^
  -morphology Distance Euclidean:4,600^! ^
  daa_mask_gi.png

%IM%convert ^
  daa_mask_out.png ^
  -morphology Distance Euclidean:4,600^! ^
  -negate ^
  daa_mask_go.png

call %PICTBAT%fanComp ^
  daa_mask_go.png ^
  daa_mask_gi.png ^
  daa_mask_gio.png
daa_mask_gi.png daa_mask_go.png daa_mask_gio.png

The method

The sigma needs to cover a gap of 25 pixels.

As the original was blurred in RGB, this must be also.

%IM%convert ^
  daa_src.png ^
  ( +clone ^
    daa_mask_in.png ^
    -compose CopyOpacity -composite ^
    -write daa_d1.png ^
    -colorspace RGB ^
    -filter Gaussian ^
      -define filter:sigma=8.333 ^
      -distort SRT 0 ^
    -colorspace sRGB ^
    -write daa_d2.png ^
    daa_mask_out.png ^
    -compose CopyOpacity -composite ^
    -write daa_d3.png ^
  ) ^
  -compose Over -composite ^
  daa_d4.png
daa_d1.png daa_d2.png daa_d3.png daa_d4.png

Blah..

%IM%convert ^
  daa_src.png ^
  ( daa_mask_in.png ^
    -morphology Distance "Euclidean:7,6^!" ^
  ) ^
  -compose CopyOpacity -composite ^
  daa_d5.png

%IM%convert ^
  daa_d4.png ^
  daa_d5.png ^
  -compose Over -composite ^
  daa_d6.png

%IM%convert ^
  daa_src.png ^
  daa_d4.png ^
  daa_mask_gio.png ^
  -compose Over -composite ^
  daa_d7.png
daa_d5.png daa_d6.png daa_d7.png

Scripts

listSegments.bat

rem From %1 a segment image file (white lines on black background)
rem lists segments to %2 CSV file.

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

@setlocal enabledelayedexpansion

@call echoOffSave

call %PICTBAT%setInOut %1 ls

set INFILE=%1
set OUTCSV=%2

del %OUTCSV% >nul

set TEMPEXT=.miff
set TEMPDIR=%TEMP%
set TEMPIMG=%TEMPDIR%\%~n1_ls%TEMPEXT%

if "%lsLEFT_COL%"=="" set lsLEFT_COL=#088
if "%lsTOP_COL%"=="" set lsTOP_COL=#00f
if "%lsRIGHT_COL%"=="" set lsRIGHT_COL=#f0f
if "%lsBOTTOM_COL%"=="" set lsBOTTOM_COL=#0f0
if "%lsCENT_COL%"=="" set lsCENT_COL=#f00

%IM%convert %INFILE% -alpha off %TEMPIMG%

rem We find black pixels, and floodfill.

for /F "usebackq" %%L in (`%IM%convert ^
  -ping %TEMPIMG% ^
  -format "WW=%%w\nHH=%%h\nWm1=%%[fx:w-1]\nHm1=%%[fx:h-1]" ^
  info:`) do set %%L


:loopLeft

set BLACK_X=

for /F "usebackq tokens=1-2 delims=, " %%X ^
in (`%IM%convert ^
  %TEMPIMG% ^
  -crop 1x%HH%+0+0 +repage ^
  +transparent #000 ^
  sparse-color:`) ^
do set BLACK_X=%%X& set BLACK_Y=%%Y

if not "%BLACK_X%"=="" (
  for /F "usebackq" %%L in (`%IM%convert ^
    %TEMPIMG% ^
    -fill %lsLEFT_COL% ^
    ^( +clone -draw "color %BLACK_X%,%BLACK_Y% floodfill" -write mpr:FILLED ^) ^
    -metric AE -compare -format "NUM_PIX=%%[distortion]" -write info: ^
    -delete 0 ^
    mpr:FILLED ^
    %TEMPIMG%`) do set %%L

  echo NUM_PIX=!NUM_PIX!

  echo %BLACK_X%,%BLACK_Y%,!NUM_PIX!,IsEdge >>%OUTCSV%

  goto loopLeft
)


:loopTop

set BLACK_X=

for /F "usebackq tokens=1-2 delims=, " %%X ^
in (`%IM%convert ^
  %TEMPIMG% ^
  -crop %WW%x1+0+0 +repage ^
  +transparent #000 ^
  sparse-color:`) ^
do set BLACK_X=%%X& set BLACK_Y=%%Y

if not "%BLACK_X%"=="" (
  for /F "usebackq" %%L in (`%IM%convert ^
    %TEMPIMG% ^
    -fill %lsTOP_COL% ^
    ^( +clone -draw "color %BLACK_X%,%BLACK_Y% floodfill" -write mpr:FILLED ^) ^
    -metric AE -compare -format "NUM_PIX=%%[distortion]" -write info: ^
    -delete 0 ^
    mpr:FILLED ^
    %TEMPIMG%`) do set %%L

  echo %BLACK_X%,%BLACK_Y%,!NUM_PIX!,IsEdge >>%OUTCSV%

  goto loopTop
)


:loopRight

set BLACK_X=

for /F "usebackq tokens=1-2 delims=, " %%X ^
in (`%IM%convert ^
  %TEMPIMG% ^
  -crop 1x%HH%+%Wm1%+0 +repage ^
  +transparent #000 ^
  sparse-color:`) ^
do set BLACK_X=%%X& set BLACK_Y=%%Y

if not "%BLACK_X%"=="" (
  set /A BLACK_X+=%Wm1%

  for /F "usebackq" %%L in (`%IM%convert ^
    %TEMPIMG% ^
    -fill %lsRIGHT_COL% ^
    ^( +clone -draw "color !BLACK_X!,%BLACK_Y% floodfill" -write mpr:FILLED ^) ^
    -metric AE -compare -format "NUM_PIX=%%[distortion]" -write info: ^
    -delete 0 ^
    mpr:FILLED ^
    %TEMPIMG%`) do set %%L

  echo NUM_PIX=!NUM_PIX!

  echo !BLACK_X!,%BLACK_Y%,!NUM_PIX!,IsEdge >>%OUTCSV%

  goto loopRight
)


:loopBottom

set BLACK_X=

for /F "usebackq tokens=1-2 delims=, " %%X ^
in (`%IM%convert ^
  %TEMPIMG% ^
  -crop %WW%x1+0+%Hm1% +repage ^
  +transparent #000 ^
  sparse-color:`) ^
do set BLACK_X=%%X& set BLACK_Y=%%Y

if not "%BLACK_X%"=="" (
  set /A BLACK_Y+=%Hm1%

  for /F "usebackq" %%L in (`%IM%convert ^
    %TEMPIMG% ^
    -fill %lsBOTTOM_COL% ^
    ^( +clone -draw "color %BLACK_X%,!BLACK_Y! floodfill" -write mpr:FILLED ^) ^
    -metric AE -compare -format "NUM_PIX=%%[distortion]" -write info: ^
    -delete 0 ^
    mpr:FILLED ^
    %TEMPIMG%`) do set %%L

  echo NUM_PIX=!NUM_PIX!

  echo %BLACK_X%,!BLACK_Y!,!NUM_PIX!,IsEdge >>%OUTCSV%

  goto loopBottom
)


:loopCent

set BLACK_X=

for /F "usebackq tokens=1-2 delims=, " %%X ^
in (`%IM%convert ^
  %TEMPIMG% ^
  +transparent #000 ^
  sparse-color:`) ^
do set BLACK_X=%%X& set BLACK_Y=%%Y

if not "%BLACK_X%"=="" (
  for /F "usebackq" %%L in (`%IM%convert ^
    %TEMPIMG% ^
    -fill %lsCENT_COL% ^
    ^( +clone -draw "color %BLACK_X%,%BLACK_Y% floodfill" -write mpr:FILLED ^) ^
    -metric AE -compare -format "NUM_PIX=%%[distortion]" -write info: ^
    -delete 0 ^
    mpr:FILLED ^
    %TEMPIMG%`) do set %%L

  echo %BLACK_X%,%BLACK_Y%,!NUM_PIX!,NotEdge >>%OUTCSV%

  goto loopCent
)


type %OUTCSV%

call @echoRestore

@endlocal & set lsTEMPIMG=%TEMPIMG%

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

%IM%identify -version
Version: ImageMagick 6.8.9-0 Q16 x64 2014-04-06 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: bzlib cairo freetype jbig jng jp2 jpeg lcms lqr 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 deantal.h1. To re-create this web page, execute "procH1 deantal".


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.


Page version v1.0 14-June-2014.

Page created 19-Jun-2014 06:48:46.

Copyright © 2014 Alan Gibson.