Align multiple images for GIF Animator

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
chcosta
Posts: 6
Joined: 2018-10-08T15:34:51-07:00
Authentication code: 1152

Align multiple images for GIF Animator

Post by chcosta »

Hello,

I have a set of 12 images with the camera traversing an object.
How can ImageMagick align (stabilization) the images?
I want to create an animated GIF of this images.

Images:
https://photos.app.goo.gl/gL4SCvuvZ9umC3mH8

Bad GIF :(
https://photos.google.com/share/AF1QipN ... Y2UVQyTm53

Thank you so much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Align multiple images for GIF Animator

Post by snibgo »

First, figure out what transformations are needed. It looks to me that you need translations in x and y directions, and scaling. You may also need some small rotation. As an optional extra, for bonus point, you could do a perspective distortion so the flat front of the model building doesn't change shape.

One approach is:

Choose two points that are vertically above each other in the model. Perhaps the top of the top-most window, and in the "E" of "AMSTERDAM". This gives two pairs of coordinates in every frame, and you need those points to line up with each other.

Then, "-distort Affine" can be used to distort each image so the two points align with any chosen image.

If you want the perspective distortion, you need to find four points that correspond on all images.

How do you find the two points on each image? There are not many images, so you could do it manually. An automatic method is: choose two points in one of the images, preferably a central image. Decide on a size in pixels of a rectangle centred on each point. Crop out those two rectangles. Then search for them in each of the other images.
snibgo's IM pages: im.snibgo.com
chcosta
Posts: 6
Joined: 2018-10-08T15:34:51-07:00
Authentication code: 1152

Re: Align multiple images for GIF Animator

Post by chcosta »

Hi snibgo, thank you so much for your attention and patience!
Can I do this with just ImageMagick?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Align multiple images for GIF Animator

Post by snibgo »

ImageMagick in a script, yes. Once you have chosen the two points and rectangle size (eg 7x7), the rest could be automatic.

See my web pages, and search for "align" for many possibilities. But the one I outline above seems most promising.

EDIT: The search for the rectangle might need to be a "chain search". The change in camera angle means that a rectangle cropped from the first image may not be found in the last image, because the images are too different. So we start looking in the adjacent image, which hasn't changed much. We find it and crop a rectangle from that image and search for it in the third. Keep going to the end.
snibgo's IM pages: im.snibgo.com
chcosta
Posts: 6
Joined: 2018-10-08T15:34:51-07:00
Authentication code: 1152

Re: Align multiple images for GIF Animator

Post by chcosta »

Thanks snibgo!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Align multiple images for GIF Animator

Post by snibgo »

Yes, the method works fine. It takes a couple of minutes.
Image

The top of the top window, and a point on the left-hand motorbike, stay in the same places. Is that what you want?

EDIT: Oops, the animation didn't have the central undistorted image. Now fixed.
snibgo's IM pages: im.snibgo.com
chcosta
Posts: 6
Joined: 2018-10-08T15:34:51-07:00
Authentication code: 1152

Re: Align multiple images for GIF Animator

Post by chcosta »

YESSSS, snibgo! How do you do? :shock:
My tests got really bad ...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Align multiple images for GIF Animator

Post by snibgo »

The steps are:

1. Decide which image to align the others to. I chose the central "full frontal" image, IMG_20181008_175313652.jpg

2. Decide which points on that image will not move. These should be vertically above each other in all images, ie within the main subject plane. I chose (586,324) and (586,869).

3. Decide on the window size for searches. It needs to be big enough to be distinctive, but not so large that it includes detail that isn't in the same plane. I chose 15x15.

4. Decide how large an area to search in each image. I chose the window size, multiplied by 9.

5. Write a list of the input images, from the central image forwards, listfwd.txt:

Code: Select all

IMG_20181008_175313652.jpg
IMG_20181008_175317636.jpg
IMG_20181008_175323231.jpg
IMG_20181008_175332486.jpg
IMG_20181008_175338634.jpg
IMG_20181008_175350987.jpg
IMG_20181008_175357155.jpg
IMG_20181008_175404592.jpg
IMG_20181008_175413653.jpg
6. Write a list of the input images, from the central image backwards, listbkwd.txt:

Code: Select all

IMG_20181008_175313652.jpg
IMG_20181008_175308782.jpg
IMG_20181008_175302042.jpg
IMG_20181008_175254984.jpg
IMG_20181008_175246234.jpg
IMG_20181008_175240880.jpg
IMG_20181008_175226628.jpg
The rest is automated, in the Windows BAT script doAlign.bat:

Code: Select all

call srchList listfwd.txt 586 324 coordsFwdTop.txt
call srchList listbkwd.txt 586 324 coordsBckTop.txt

call srchList listfwd.txt 586 869 coordsFwdBot.txt
call srchList listbkwd.txt 586 869 coordsBckBot.txt

if not exist distorted md distorted
del /s /q distorted\*

rem -----------------------------
rem Do the central image, undistorted.

call readArray listfwd.txt Files

%IMG7%magick ^
  !Files[0]! ^
  -gravity Center -crop 1000x1000+0+0 +repage ^
  -resize 600x600 ^
  distorted\!Files[0]!


rem -----------------------------
rem Distort the forwards list.

call readArray listfwd.txt Files
call readArray coordsFwdTop.txt CdsTop
call readArray coordsFwdBot.txt CdsBot

set firstCdsTop=%CdsTop[0]%
set firstCdsBot=%CdsBot[0]%

for /L %%I in (1,1,%Files_CNTm1%) do (
  echo !Files[%%I]! !CdsTop[%%I]!,!CdsBot[%%I]!

  %IMG7%magick ^
    !Files[%%I]! ^
    -distort Affine !CdsTop[%%I]!,%firstCdsTop%,!CdsBot[%%I]!,%firstCdsBot% ^
    -gravity Center -crop 1000x1000+0+0 +repage ^
    -resize 600x600 ^
    distorted\!Files[%%I]!
)

rem -----------------------------
rem Distort the backwards list.

call readArray listbkwd.txt Files
call readArray coordsBckTop.txt CdsTop
call readArray coordsBckBot.txt CdsBot

set firstCdsTop=%CdsTop[0]%
set firstCdsBot=%CdsBot[0]%

for /L %%I in (1,1,%Files_CNTm1%) do (
  echo !Files[%%I]! !CdsTop[%%I]!,!CdsBot[%%I]!

  %IMG7%magick ^
    !Files[%%I]! ^
    -distort Affine !CdsTop[%%I]!,%firstCdsTop%,!CdsBot[%%I]!,%firstCdsBot% ^
    -gravity Center -crop 1000x1000+0+0 +repage ^
    -resize 600x600 ^
    distorted\!Files[%%I]!
)

rem -----------------------------
rem Make the GIF.

%IMG7%magick ^
  distorted\*.jpg ^
  amst_aligned.gif
doAlign.bat calls srchList for the forwards and backwards lists, to find the corresponding points in the other images. srchList.bat is:

Code: Select all

rem %1 is a list of images
rem %2,%3 are coordinates in first image
rem %4 is output text file.

set slX=%2
set slY=%3

echo !slX!,!slY!>%4

set HasFirst=0
set PrevFile=
set NUM=0
for /F "tokens=*" %%F in (%1) do (
  echo %%F

  if !HasFirst!==0 (
    set PrevFile=%%F
    set HasFirst=1
  ) else (
    call srchLink !PrevFile! %%F !slX! !slY!
    if ERRORLEVEL 1 exit /B 1

    copy /y sub_si_dbg.png sl_dbg_!NUM!.png

    set /A NUM+=1

    echo !slX!,!slY!>>%4

    set PrevFile=%%F
  )
)
In turn, this calls searchLink, to do one link in the search chain:

Code: Select all

rem Search one link in a search chain.

rem %1 is an image from which a rectangle will be cropped,
rem %2 is an image to be searched.
rem %3,%4 coords of a point in %1
rem Returns slX and slY, corresponding point in %2.


echo %0: %1 %2 %3 %4

set WindWH=15
set /A WindR=(%WindWH%-1)/2

set IMG1=%1
set IMG2=%2

set GuessX=%3
set GuessY=%4

%IMG7%magick ^
  %IMG1% ^
  -crop %WindWH%x%WindWH%+%%[fx:%GuessX%-%WindR%]+%%[fx:%GuessY%-%WindR%] +repage ^
  s1.png

set WindFact=9
set /A WindSide=(%WindFact%-1)/2
set /A SX=%GuessX%-%WindR%-%WindWH%*%WindSide%
set /A SY=%GuessY%-%WindR%-%WindWH%*%WindSide%

if %SX% LSS 0 (
  echo SX=%SX%
  exit /B 1
)

if %SY% LSS 0 (
  echo SY=%SY%
  exit /B 1
)

%IMG7%magick ^
  %IMG2% ^
  -crop %%[fx:%WindWH%*%WindFact%]x%%[fx:%WindWH%*%WindFact%]+%SX%+%SY% +repage ^
  sub.png

set siDEBUG=1
call %PICTBAT%srchImg sub.png s1.png

set /A slX=%SX%+%siCOMP_XW%+%WindR%
set /A slY=%Sy%+%siCOMP_YW%+%WindR%

echo slX=%slX%  slY=%slY%
In turn, this calls srchImg.bat. See Searching an image . This takes most of the time. If you have my process modules, "-process srchimg" would take less time.

Okay, so now we know the coordinates of the two points in each image that correspond to the two points in our central image. doAlign.bat copies the central image, then does the affine transformation for the images in the forwards list, then for those in the backwards list. For each of these, we take a central crop to remove the added virtual pixels at edges. We also resize to 600x600 for the web.

Finally, doAlign.bat takes all these distorted images and makes them into a GIF.
snibgo's IM pages: im.snibgo.com
chcosta
Posts: 6
Joined: 2018-10-08T15:34:51-07:00
Authentication code: 1152

Re: Align multiple images for GIF Animator

Post by chcosta »

Snibgo you're the best!
Thank you so much! You do not know how many hours I've lost in searches to understand how to do this.

I will read your orientations calmly so as not to bother you again. Post here my gif... :lol:
Thank you so much again!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Align multiple images for GIF Animator

Post by snibgo »

I should mention: my script writes the distorted images in the same format as the input, and these are combined into the output GIF. The input files are JPG, so these intermediate files are also JPG. This is very bad practice. They should be written in a lossless format such as PNG or non-JPEG TIFF.

I'll probably write this up as a web page.
snibgo's IM pages: im.snibgo.com
chcosta
Posts: 6
Joined: 2018-10-08T15:34:51-07:00
Authentication code: 1152

Re: Align multiple images for GIF Animator

Post by chcosta »

It will be a great post!
thanks again!
Post Reply