Page 1 of 2

Stitching image on given canvas size ? [SOLVED]

Posted: 2013-02-25T10:48:35-07:00
by Rye
I currently am trying to do the following with Image Magick:



I would like to do the following using image magick:



1. set a specific canvas size, say 1056x576 for example

2. Take one image, this one for example.: https://dl.dropbox.com/u/14586156/stuff ... %20001.gif

3. Have image magick stitch this image multiple times over said canvas so it will look like this.: https://dl.dropbox.com/u/14586156/stuff ... result.gif

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-25T14:44:09-07:00
by fmw42
This would be the command


convert -size 1056x576 tile:Castillo_001.gif Castillo_tiled.gif

Image

except your image does not conform to be tiled as it is does not wrap nor is it cropped to avoid seams. You need to crop out a section that will repeat without a seam.

So crop the image something like this:


Image


Now run this unix set of commands.


width=1056
height=576
infile="Castillo_001_crop2.gif"
hh2=`convert $infile -format "%[fx:h/2]" info:`
convert $infile \
\( -clone 0 -roll +0+${hh2} \) +append \
-write mpr:cell +delete \
-size ${width}x${height} tile:mpr:cell \
Castillo_001_tiles.gif


Image

see
http://www.imagemagick.org/Usage/canvas/#tile_memory

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-25T15:40:51-07:00
by Rye
Sure, I understand it is very possible WITH CROPPING.

However I don't want to crop.

I need a solution that doesn't involve cropping.
(I'd have to crop 600 images so this is not an option).

Thanks for your answer

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-25T16:40:15-07:00
by snibgo
As a general problem: we have a repeating pattern, like a wallpaper. The pattern does not repeat exactly. How do we overlap the pieces so the pattern matches in the best way?

One approach: for this example, we need to examine horizontal positions only. Try overlap with offsets +1, +2, ... +width-1. At each position, "-composite difference". The best match is where the result is darkest.

That gives us the horizontal overlap offset. Repeat for the vertical overlap.

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-25T17:46:31-07:00
by snibgo
The theory sounded good. Does it actually work? Windows script:

Code: Select all

setlocal enabledelayedexpansion

set SRC=Castillo001.gif

FOR /F "tokens=1,2" %%i IN ('%IM%identify -ping -format "%%w %%h" %SRC%') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
)

set /A wm1=%WIDTH%-1
set /A MinGray=999999
set /A OffsAtMin=0

FOR /L %%a in (1,1,%wm1%) DO (

set OFFSET=%%a

"%IMG%convert" ^
  %SRC% ^
  ^( +clone -geometry +!OFFSET!+0 ^) ^
  -compose Difference -composite ^
  -crop 1x%HEIGHT%+!OFFSET!+0 ^
  -resize "1x1^!" ^
  -modulate 100,0,100 -alpha off ^
  txt:gray.txt

rem What is the grey value at this offset?

for /F "eol=# tokens=3 delims=,() " %%b in (gray.txt) do set grey=%%b

if !MinGray! GTR !grey! (
  set MinGray=!grey!
  set OffsAtMin=!OFFSET!
)

echo grey !MinGray! at offset !OffsAtMin!
)


echo grey %MinGray% at offset %OffsAtMin%

rem Result is "grey 11 at offset 128".
rem So we can crop horizontally at offset 128 for a repeating pattern.
rem Do likewise for vertical repeat. For fun, let's pretend it repeats at 128 vertically.

"%IMG%convert" ^
  %SRC% ^
  -crop %OffsAtMin%x128+0+0 +repage ^
  cropped.png

"%IMG%convert" ^
  -size 1056x576 ^
  tile:cropped.png ^
  tiled.png
Yeah, perfect result.

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-25T18:30:45-07:00
by anthony
there are two ways.

one is to try and overlap images so as to cover the whole canvas. That can be tricky as the images may not overlap squarely or may not form a regular pattern.

The second is to try and automatically from the tiling pattern (perhaps rotating do it is aligned properly).

This is tricky, but may be do able.


One solution for the second method ise to possibly use FFT (Fast Fourier Transform). This may be able to locate the repeating pattern and also find the closest orthogonal (axis aligned) separation of the tiles. In other words, it should let you find the overall pattern and then simplify (rotate) and replicate it to a larger canvas.

This can take some work but seems the best course of action. Just needs -- time -- which I do not have.

Here is a photo I took of another repeating pattern, which makes a good test image...
Image

The vignette lighting effect you see is sun-dappling from trees.

I'd like to see what people come up with

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-26T05:21:26-07:00
by snibgo
Rye's image repeats almost exactly both horizontally and vertically, at fixed distances. Anthony's image is real-world: axes are not orthogonal, neither x or y, the repetition distances are not equal, and probably not even in straight lines.

Here is a first stab at analysis.

I extend my above script to evaluate all possible offsets, both x and y. It finds the average difference in the area that overlaps the original, and the original offset by (x,y). The Windows script takes 2.5 hours; a compiled program might take around a minute.

The result is the same size as the input. Darker pixels indicate a close match at that (x,y) offset; lighter pixels are worse matches. The top-left pixel is exactly black because the image matches itself at (0,0) offset. Bottom-right is noisy as the overlapping area is small.

Code: Select all

convert grays.txt -auto-level grays.png
grays.png: https://www.dropbox.com/s/cgys9wt4mztv09t/grays.png
graysHL.png, with some troughs highlighted by hand: https://www.dropbox.com/s/lc5uqo5v4rze8k7/graysHL.png

We can see the pattern of repeats. More usefully, we could extract the coordinates of the centre of each trough. We could then find the closest troughs to (0,0), and associate each of those with troughs at roughly the same distance and angle from the first. Then a distort map can be built so the repeats are at exactly the same distance and angle as each other.

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-26T16:12:06-07:00
by snibgo
I should have set "-depth 16" in the script, to retain precision.

The troughs nearest to (0,0) are at (87,12) and (35,82). So we can crop that area, and repeat it as often as we like.

Code: Select all

set SRC=tiles_closeup_sm.jpg

"%IMG%convert" ^
  %SRC% ^
  -crop 87x82+0+0 +repage ^
  cropped.png

rem 87+35=122
rem 12+82=94

"%IMG%convert" ^
  -size 200x200 xc:pink ^
  cropped.png -composite ^
  cropped.png -geometry +87+12 -composite ^
  cropped.png -geometry +35+82 -composite ^
  cropped.png -geometry +122+94 -composite ^
  g2.png
g2.png: https://www.dropbox.com/s/dqxj4u7y7w9j75y/g2.png

This process could have been started by cropping an area, either from a corner or the centre, and doing a sub-image search to find the pattern. But how large should that crop be? Ideally it would be the size of the repetition (87x82 if we examine the top-left), and we don't know that until we do some work. We could guess, then iterate if the guess turns out too large or too small.

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-26T16:40:47-07:00
by anthony
snibgo wrote:AOne approach: for this example, we need to examine horizontal positions only. Try overlap with offsets +1, +2, ... +width-1. At each position, "-composite difference". The best match is where the result is darkest.

Hmmm that sounds more like a sub-image search using itself!!!!

Assuming the tile pattern is smaller than 50% of the input image (it would be pretty bad if it wasn't)...
Image

Code: Select all

convert "Castillo 001.gif"  \( +clone -crop 50%x+0+0 \) miff:- |
    compare -subimage-search - miff:- | 
       convert - -delete 0 tile_search_result.png
The first just extracts a quarter image. then it is compared at every position over a quarter of the image, and produces a similar map. The last just saves the second (map) image.

Resulting in...
Image

NOTE: This is VERY slow. But even so, on my 8 core machine completed in -- 11.5 seconds!

WARNING: the image for some reason contains a random transparency! This is a bug, as the gray scale map should not contain any transparency. I re-processed the above image to simply turn off alpha channel.

The result is also not just a simple difference of grayscale values, but the length of the color different vector (RMSE), as such it should work well even for images with little grayscale variance.

You can get the same result with "composite Difference" using a difference of individual color channels, which is then squared, added and square-root.

For large image compares a FFT convolution would be better. Fred, can you provide an equivalent FFT example?


One point. this time determination only determined the size and relative offsets of the tile pattern. It does NOT try to center the main part (high entropy) of the tile in the center of the tile, so that the low entropy areas (background areas) are used along the borders of the tile.

Extracting the tile appropriatally would be the next step. As would any rotation correction, and edge blending
Hmmm.... Do I hear a script in the making here... Fred?

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-26T16:49:05-07:00
by anthony
Repeating with my far more complicated image...
Image

Code: Select all

time convert tiles_closeup_sm.jpg \( +clone -crop 50%x+0+0 \) miff:- |
   compare -subimage-search - miff:- |
      convert - -delete 0 -alpha off tile_search_result_2.png
Image
Time: 1 minute 52.0 seconds

Not as good a result, probably as the image needs perspective and barrel distortion fixing.
But an upper-left tile could be determined.

A better result may be possible using a center crop (to get a centered tile pattern) and a determination of distances between match peaks.

Code: Select all

time convert tiles_closeup_sm.jpg \( +clone -gravity center -crop 50% \) miff:- |
   compare -subimage-search - miff:- |
      convert - -delete 0 -alpha off tile_search_result_3.png
Image
Time: 1 minute 53.4 seconds (not surprising same image sizes)

the bright spot is of course the offset of the 50% centered crop relative to the top left corner, which works out to be the center of the 'quarter image map'.

One pre-step may be to first determine a high entropy area so that you can extract a smaller sub-image from the original image, and still be sure you file matching tiling points. A smaller sub-image will generate a larger 'map' image, and (cross fingers) be a bit faster in sub-image searching. Finding such high entropy sub-images was part of my work in another topic "Overlapping Images", and this is highly relevent. In this case you want a high entropy sub-image near the center of the original image, rather than the edge as I needed in finding overlaps.
See viewtopic.php?f=1&t=22526

Regardless a center crop does let you see the hexagonal tile pattern that is present, and can more easily determine the size, relative offsets, and orientation of those tiles for extraction, rotation and blending.

Anyone like to give the extraction and blending step a go?

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-26T21:05:25-07:00
by anthony
For locating the peaks see the rough notes in
IM Examples, Comparing Images...
http://www.imagemagick.org/Usage/compare/#peak_finding

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-26T21:52:52-07:00
by Rye
snibgo wrote:The theory sounded good. Does it actually work? Windows script:

Code: Select all

setlocal enabledelayedexpansion

set SRC=Castillo001.gif

FOR /F "tokens=1,2" %%i IN ('%IM%identify -ping -format "%%w %%h" %SRC%') DO (
  set WIDTH=%%i
  set HEIGHT=%%j
)

set /A wm1=%WIDTH%-1
set /A MinGray=999999
set /A OffsAtMin=0

FOR /L %%a in (1,1,%wm1%) DO (

set OFFSET=%%a

"%IMG%convert" ^
  %SRC% ^
  ^( +clone -geometry +!OFFSET!+0 ^) ^
  -compose Difference -composite ^
  -crop 1x%HEIGHT%+!OFFSET!+0 ^
  -resize "1x1^!" ^
  -modulate 100,0,100 -alpha off ^
  txt:gray.txt

rem What is the grey value at this offset?

for /F "eol=# tokens=3 delims=,() " %%b in (gray.txt) do set grey=%%b

if !MinGray! GTR !grey! (
  set MinGray=!grey!
  set OffsAtMin=!OFFSET!
)

echo grey !MinGray! at offset !OffsAtMin!
)


echo grey %MinGray% at offset %OffsAtMin%

rem Result is "grey 11 at offset 128".
rem So we can crop horizontally at offset 128 for a repeating pattern.
rem Do likewise for vertical repeat. For fun, let's pretend it repeats at 128 vertically.

"%IMG%convert" ^
  %SRC% ^
  -crop %OffsAtMin%x128+0+0 +repage ^
  cropped.png

"%IMG%convert" ^
  -size 1056x576 ^
  tile:cropped.png ^
  tiled.png
Yeah, perfect result.
Hmm... I tried to get that command working under windows, however it will do me no good.

I have ImageMagick installed on my machine, put the bat (this script you wrote) and the image file in the same directory.

However when I now call this script it tells me:

- command "identify" not found
- Illegal Parameter - - crop
- Illegal Parameter - 1056x576

Am I doing something wrong ? Is there anything you have to consider while trying to runs such script ?

BTW: AWESOME feedback ! Never thought to get so many productive answers.

Thanks alot !

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-27T03:29:48-07:00
by snibgo
Rye wrote:- command "identify" not found
What happens when you type the command

Code: Select all

identify -version
?

It should say something like "Version: ImageMagick 6.7.9-6 2012-09-13 Q16 http://www.imagemagick.org". If it says "'identify' is not recognized as an internal or external command, operable program or batch file" then Windows doesn't know where the program is.

Most people install ImageMagick ticking the box saying "Add directory to system path".

Personally, I don't. Instead, I use environment variables IM and IMG to point to IM's directory.

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-27T15:59:33-07:00
by Rye
I reinstalled ImageMagick with said checkbox disabled...

The erros I said still occur.

Typing identify -version yields:
The command "identify" is either written incorrectly or couldn't be found.

Also that command that is suggested at the installation:

Code: Select all

convert wizard: wizard.jpg
convert wizard.jpg win
only outputs:

Illegal Parameter - wizard.jpg
Illegal Parameter - win

typing imdisplay only yields:
The command "imdisplay is either written incorrectly or couldn't be found.

I used this installation package: ftp://ftp.imagemagick.org/pub/ImageMagi ... 86-dll.exe
What in the world is going on... ?

Re: Stitching image on given canvas size using ImageMagic ?

Posted: 2013-02-27T17:00:24-07:00
by snibgo
Rye wrote:I reinstalled ImageMagick with said checkbox disabled...
I suggest you reinstall with the checkbox _enabled_.

What version of Windows are you running? Are you running the commands in a "command prompt" window?

What happens when you type:

Code: Select all

"c:\program files\imageMagick-6.8.3-Q16\identify" -version
(using the actual directory you have installed to). What happens when you "dir" that directory?