snibgo's ImageMagick pages

Assembling with dark paths

Images can be assembled in grid patterns with minimum error boundary cuts.

This page follows from material in Dark paths.

This technique is closely related to Rectangle boundaries with dark paths, and also to Tiling with dark paths.

Cutting patches at their overlaps is an alternative to blending. See Blending tiles.

The method

If we have a number of same-size images, we can assemble them with ImageMagick into a grid pattern by using +append to build each row, then -append to build the output image from all the rows.

However, we might want the input images to overlap each other in the output, with a minimum error boundary cut (MEBC) at each overlap. The script mebcTile.bat does the job.

The first argument is the pattern for the filenames, with XX standing for a number, from zero upwards, with no leading zeros. Then the numbers of columns and rows in the grid must be given. Then the horizontal and vertical overlaps must be given; each may be suffixed with one of '%', 'c' or 'p'. Finally, an output filename may be given.

The script will substitute a number for XX in the filename pattern. It will also set the canvas offsets in each input file. (Hence, the file type must be capable of storing canvas offsets. PNG and MIFF are two such file types.) For each input file, it calls mebcOne.bat which does the hard part.

mebcOne.bat examines the offsets. If the image has no offsets, no cut is made. If it has no x-offset, it is cut on the top edge to overlap with the canvas. If it has no y-offset, it is cut on the left edge to overlap with the canvas. Otherwise, it has both x- and y-offsets, so is cut on both edges.

mebcOne.bat operates much like rectDp.bat (see Rectangle boundaries with dark paths) but is simpler as only two edges may need cutting.

Example

Create four sample input files:

%IM%convert ^
  dpt_lvs_sm.jpg ^
  ( +clone ^
    -sigmoidal-contrast 5,20%% ^
    +write dpa_tmp_1.miff ^
    +delete ) ^
  ( +clone ^
    -sigmoidal-contrast 5,70%% ^
    +write dpa_tmp_2.miff ^
    +delete ) ^
  ( +clone ^
    -sigmoidal-contrast 5,90%% ^
    +write dpa_tmp_3.miff ^
    +delete ) ^
  dpa_tmp_0.miff

Assemble them in a 2x2 grid, with 10% overlap in each direction:

call %PICTBAT%mebcTile dpa_tmp_XX.miff 2 2 10c 10c dpa_ass1.png
dpa_ass1.pngjpg

Cleanup

del dpa_tmp_?.miff

Future

Why did I do this with canvas offsets? I forget. I may change it to use command arguments, which would save updating metadata in the input files, and then reading it in mebcOne.bat.

Scripts

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

mebcTile.bat

rem Assemble images with Minimum Error Boundary Cut.

@rem %1 is input files with XX, eg cr_XX.png, XX will be substituted with an integer.
@rem   Any offsets in inputs will be overridden.
@rem %2 is number of image columns
@rem %3 is number of image rows
@rem %4 is horizontal overlap in pixels
@rem %5 is vertical overlap in pixels
@rem %6 is output

@rem Inputs are assumed to be the same size.

@rem %4 and %5 can be suffixed.

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

@setlocal

@call echoOffSave

call %PICTBAT%setInOut %1 mebct


set INFILES=%1

set /A Wm1=%2-1
set /A Hm1=%3-1

set overX=%4
set overY=%5

if not "%6"=="" set OUTFILE=%6

set FIRST_IN=%INFILES:XX=0%

set WW=
for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h" ^
  %FIRST_IN%`) do set %%L
if "%WW%"=="" exit /B 1

call xyCoord %WW% %HH% %overX% %overY%

echo xycXi=%xycXi% xycYi=%xycYi%

set overX=%xycXi%
set overY=%xycYi%

set TEMP_OUT=mebct.miff
del %TEMP_OUT% 2>nul

set CNT=0
for /L %%y in (0,1,%Hm1%) do (

  for /L %%x in (0,1,%Wm1%) do (
    set INONE=%INFILES:XX=!CNT!%

    rem for /F "usebackq" %%L in (`%IM%identify ^
    rem   -format "offX=%%X\noffY=%%Y" ^
    rem   !INONE!`) do set %%L

    %IM%convert ^
      !INONE! ^
      -set page "+%%[fx:%%x*(%WW%-%overX%)]+%%[fx:%%y*(%HH%-%overY%)]" ^
      !INONE!

    call %PICTBAT%mebcOne %TEMP_OUT% !INONE! %overX% %overY%

    if ERRORLEVEL 1 exit /B 1

    set /A CNT+=1
  )
)

%IM%convert %TEMP_OUT% %OUTFILE%

call echoRestore

endlocal & set mebctOUTFILE=%OUTFILE%

mebcOne.bat

rem Called from mebcAssemble.bat and mkQuilt.bat, and others.

rem Given %1 is input/output canvas,
rem %2 is image patch to be merged onto canvas, with the required canvas offset,
rem %3 is horizontal overlap in pixels
rem %4 is vertical overlap in pixels
rem merges images possibly with Minimum Error Boundary Cut along top edge or left edge or both.

rem FUTURE: accept parameters for offsets, then no need to "identify".
rem FUTURE: make the blur an environment variable.

@setlocal enabledelayedexpansion

set CANV=%1
set IMG=%2
set overX=%3
set overY=%4

for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h\noffX=%%X\noffY=%%Y" ^
  %IMG%`) do set /A %%L

echo !IMG! offX=%offX% offY=%offY%

set /A xpy=%offX%+%offY%

if %xpy%==0 (
  if exist %CANV% (
    %IM%convert ^
      %CANV% ^
      %IMG% ^
      -compose Over -layers Merge ^
      %CANV%
  ) else (
    %IM%convert ^
      %IMG% ^
      %CANV%
  )

  if ERRORLEVEL 1 exit /B 1

) else if %offX%==0 (
  set /A upY=!offY!-%overY%
  echo Merge top upY=!upY!

  %IMDEV%convert ^
    ^( %CANV% +write mpr:CANV ^
       +repage ^
       -crop %WW%x%overY%+0+%upY% +repage ^
    ^) ^
    ^( %IMG% +write mpr:IMG ^
       +repage ^
       -crop %WW%x%overY%+0+0 +repage ^
    ^) ^
    -colorspace Gray ^
    -compose Difference -composite ^
    -rotate -90 ^
    -process darkestpath ^
    -morphology dilate:-1 2x1+1+0:1,1 ^
    -blur 0x0.5 ^
    -rotate 90 ^
    -background White -extent %WW%x%HH% ^
    mpr:IMG ^
    +swap ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    mpr:CANV ^
    +swap ^
    -background None ^
    -compose Over -layers Merge ^
    +repage ^
    %CANV%

  if ERRORLEVEL 1 exit /B 1

) else if %offY%==0 (
  set /A leftX=!offX!-%overX%
  echo Merge leftX=!LeftX!

  %IMDEV%convert ^
    ^( %CANV% +write mpr:CANV ^
       +repage ^
       -crop %overX%x%HH%+%leftX%+0 +repage ^
    ^) ^
    ^( %IMG% +write mpr:IMG ^
       +repage ^
       -crop %overX%x%HH%+0+0 +repage ^
    ^) ^
    -colorspace Gray ^
    -compose Difference -composite ^
    -process darkestpath ^
    -morphology dilate:-1 2x1+1+0:1,1 ^
    -blur 0x0.5 ^
    -background White -extent %WW%x%HH% ^
    mpr:IMG ^
    +swap ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    mpr:CANV ^
    +swap ^
    -background None ^
    -compose Over -layers Merge ^
    +repage ^
    %CANV%

  if ERRORLEVEL 1 exit /B 1

) else (
  set /A upY=!offY!-%overY%
  set /A leftX=!offX!-%overX%
  set /A upY=!offY!
  set /A leftX=!offX!

  echo Merge top and left upY=!upY! leftX=!LeftX!

  %IMDEV%convert ^
    ^( %CANV% +write mpr:CANV ^
       +repage ^
       -crop %WW%x%overY%+0+%upY% +repage ^
    ^) ^
    ^( %IMG% +write mpr:IMG ^
       +repage ^
       -crop %WW%x%overY%+0+0 +repage ^
    ^) ^
    -colorspace Gray ^
    -compose Difference -composite ^
    -rotate -90 ^
    -process darkestpath ^
    -morphology dilate:-1 2x1+1+0:1,1 ^
    -blur 0x0.5 ^
    -rotate 90 ^
    -background White -extent %WW%x%HH% ^
    ^( ^
      ^( mpr:CANV ^
         +repage ^
         -crop %overX%x%HH%+%leftX%+0 +repage ^
      ^) ^
      ^( mpr:IMG ^
         +repage ^
         -crop %overX%x%HH%+0+0 +repage ^
      ^) ^
      -colorspace Gray ^
      -compose Difference -composite ^
      -process darkestpath ^
      -morphology dilate:-1 2x1+1+0:1,1 ^
      -blur 0x0.5 ^
      -background White -extent %WW%x%HH% ^
    ^) ^
    -compose Multiply -composite ^
    mpr:IMG ^
    +swap ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    mpr:CANV ^
    +swap ^
    -background None ^
    -compose Over -layers Merge ^
    +repage ^
    %CANV%

  if ERRORLEVEL 1 exit /B 1

)

endlocal

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

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


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 10-Dec-2015.

Page created 03-Oct-2016 13:16:20.

Copyright © 2016 Alan Gibson.