snibgo's ImageMagick pages

Shape centres

Various definitions.

For processing masks and distortions, we often need to use a centre of a shape. (I am British, so I spell it "centre", not "center".)

Sample input

We create a sample source.

%IMG7%magick ^
  -size 300x200 xc:Black ^
  -fill White ^
  -draw "polygon 140,10 280,180 160,110 110,150 20,160" ^
  shpc_src.png
shpc_src.png

The script

The script shpCent.bat finds the centre according to one of these definitions of "centre".

In the examples, we show the found coordinates, and mark the location with a coloured dot.

call %PICTBAT%shpCent ^
  shpc_src.png ^
  furthest ^
  shpc_1_

set shpc_1_ 

%IMG7%magick ^
  shpc_src.png ^
  -fill orange ^
  -draw "translate %shpc_1__CX%,%shpc_1__CY% circle 0,0,0,5" ^
  shpc_1.png
shpc_1__CX=140
shpc_1__CY=75
shpc_1.png
call %PICTBAT%shpCent ^
  shpc_src.png ^
  centroid ^
  shpc_2_

set shpc_2_ 

%IMG7%magick ^
  shpc_src.png ^
  -fill orange ^
  -draw "translate %shpc_2__CX%,%shpc_2__CY% circle 0,0,0,5" ^
  shpc_2.png
shpc_2__CX=138.2
shpc_2__CY=101.4
shpc_2.png
call %PICTBAT%shpCent ^
  shpc_src.png ^
  boundingbox ^
  shpc_3_

set shpc_3_ 

%IMG7%magick ^
  shpc_src.png ^
  -fill orange ^
  -draw "translate %shpc_3__CX%,%shpc_3__CY% circle 0,0,0,5" ^
  shpc_3.png
shpc_3__CX=150
shpc_3__CY=95
shpc_3.png
call %PICTBAT%shpCent ^
  shpc_src.png ^
  60cx40c ^
  shpc_4_

set shpc_4_ 

%IMG7%magick ^
  shpc_src.png ^
  -fill orange ^
  -draw "translate %shpc_4__CX%,%shpc_4__CY% circle 0,0,0,5" ^
  shpc_4.png
shpc_4__CX=180
shpc_4__CY=80
shpc_4.png

Future

A trivial script could identify whether a given point is within the shape, ie whether that pixel is white.

For some purposes, we need to avoid centres that make boundaries reentrant, meaning that we should be able to draw a line from the centre to any point on the boundary without leaving the shape. From the center, we should be able to "see" the entire boundary. Some shapes have no centres that pass this test. It would be useful to have a process for finding all centres that do pass the test, or at least to test one given point.

Scripts

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

shpCent.bat

rem %1 input image, white image on black background
rem %2 centre: "furthest" or "centroid" or "boundingbox"
rem      or "XxY" where X and Y are numbers, both possibly suffixed by %% or c or p.
rem      Default is coordinate at furthest distance from shape edge.
rem %3 prefix for environment variables _CX and _CY

@setlocal enabledelayedexpansion

@call echoOffSave

call %PICTBAT%setInOut %1 shpc

set sCent=%2
if "%sCent%"=="." set sCent=
if "%sCent%"=="" set sCent=furthest

set EnvPref=%3
if "%EnvPref%"=="." set EnvPref=
if "%EnvPref%"=="" set EnvPref=shpc

if /I "%sCent%"=="furthest" (

  for /F "usebackq tokens=1,4,5 delims=:, " %%A in (`%IMG7%magick ^
    %INFILE% ^
    -colorspace Gray ^
    -morphology Distance Euclidean:7 ^
    -alpha off -auto-level ^
    -define "identify:locate=maximum" ^
    -define "identify:limit=1" ^
    -identify ^
    NULL:`) do (
    if /I "%%A"=="Gray" (
      set CX=%%B
      set CY=%%C
    )
  )

) else if /I "%sCent%"=="centroid" (

  for /F "usebackq skip=1 tokens=3-6 delims=, " %%A in (`%IMG7%magick ^
    %INFILE% -alpha off -threshold 50% ^
    -define "connected-components:verbose=true" ^
    -connected-components 8 ^
    NULL:`) do (
    if "%%D"=="gray(255)" (
      set CX=%%A
      set CY=%%B
    )
  )

) else if /I "%sCent%"=="boundingbox" (

  for /F "usebackq" %%L in (`%IMG7%magick ^
    %INFILE% -alpha off -threshold 50% ^
    -trim ^
    -format "CX=%%[fx:(w-1)/2+page.x]\nCY=%%[fx:(h-1)/2+page.y]\n" ^
    info:`) do set %%L

) else (
  for /F "usebackq" %%A in (`%IMG7%magick ^
    %INFILE% -format "WW=%%w\nHH=%%h\n" ^
    info:`) do set %%A

  call parseXxY2 !WW! !HH! acw %sCent%

  set CX=!acw_X!
  set CY=!acw_Y!
)

echo %0: CX=%CX% CY=%CY%

if "!CX!"=="" (
  echo %0: sCent=%sCent%  no CX
  exit /B 1
)

if ERRORLEVEL 1 exit /B 1

call echoRestore

endlocal & set %EnvPref%_CX=%CX%& set %EnvPref%_CY=%CY%

All images on this page were created by the commands shown.

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

My usual version of IM is:

%IMG7%magick -version
Version: ImageMagick 7.1.1-15 Q16-HDRI x64 a0a5f3d:20230730 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenCL OpenMP(2.0) 
Delegates (built-in): bzlib cairo freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193532217)

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


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 13-September-2023.

Page created 17-Sep-2023 23:08:23.

Copyright © 2023 Alan Gibson.