Put objects in straight line, with speed.

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
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Put objects in straight line, with speed.

Post by eleison »

Objects (id: bounding-box centroid area mean-color):
I have this image:
Image
And I want this result, put all squares in a straight line with increasing numbers.
Image

Using connected components function i get this result:

Code: Select all

  0: 604x553+0+0 301.7,275.9 325867 srgb(255,255,255)
  7: 33x41+44+286 60.0,306.0 1353 srgb(0,0,0)
  8: 33x33+502+301 518.0,317.0 1089 srgb(0,0,0)
  4: 33x32+383+257 399.0,272.5 1056 srgb(0,0,0)
  2: 31x34+254+238 269.0,254.5 1054 srgb(0,0,0)
  6: 33x31+444+275 460.0,290.0 1023 srgb(0,0,0)
  3: 30x31+179+245 193.5,260.0 930 srgb(0,0,0)
  1: 29x30+315+229 329.0,243.5 870 srgb(0,0,0)
  5: 22x35+115+263 125.5,280.0 770 srgb(0,0,0)
I know I can do this by cropping the pictures and then use +append to connect them. Put I need it to perform fast as possible, and without saving tempfiles. Also I'm not sure how to put the white space in between each square when i append them. Would be nice if I could do this in one single convert.

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Put objects in straight line, with speed.

Post by fmw42 »

If you crop them and then trim the white so that all you have is the black boxes and you name them in order and put them in the named order as input to montage, it can put equal white space around them and line them up.

See
http://www.imagemagick.org/Usage/montage/
http://www.imagemagick.org/Usage/crop/#crop_repage
http://www.imagemagick.org/Usage/crop/#trim

always add +repage after -crop and -trim, unless you need to keep the virtual canvas location information.
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Put objects in straight line, with speed.

Post by eleison »

Thanks for the reply! I can see wich one is first by looking at the X-axis cordinates. I'm in a hurry right know, but will take a look at the links you posted.

Is it possible to move objects within an image without saving as new files?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Put objects in straight line, with speed.

Post by fmw42 »

You would have do a convert to crop and trim out all the images from the input and save them all to miff:, then pipe that to montage.

convert image \
\( -clone 0 -crop ... +repage \) \
\( -clone 0 -crop ... +repage \) \
... etc for each numbered image ... \
\( -clone 0 -crop ... +repage \) \
-fuzz XX% -trim +repage \
-delete 0 miff:- | montage <your arguments> - results

note the pipe |
and the - in the montage which is the input from the miff:- output of the convert.

This is unix syntax. I don't know if Windows allows pipes and in Windows parentheses do not need escaping by \, but the new line escape is ^ rather than \

You should always identify your IM version and platform when asking questions.
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Put objects in straight line, with speed.

Post by eleison »

convert box_line_test_nr.jpg \
\( -clone 0 -crop 33x41+44+286 +repage \) \
\( -clone 0 -crop 22x35+115+263 +repage \) \
\( -clone 0 -crop 30x31+179+245 +repage \) \
\( -clone 0 -crop 31x34+254+238 +repage \) \
\( -clone 0 -crop 29x30+315+229 +repage \) \
\( -clone 0 -crop 33x32+383+257 +repage \) \
\( -clone 0 -crop 33x31+444+275 +repage \) \
\( -clone 0 -crop 33x33+502+301 +repage \) \
-fuzz 5% -trim +repage \
-delete 0 miff:- | montage -tile 9x1 -geometry 16x16+1+1 - results

Thanks for the help! This is what i got so far, but I did probably missunderstand somethinge because it's not working. I got ImageMagick 6.9.0-4 Q16 x86_64 and CentOS release 6.6.
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Put objects in straight line, with speed.

Post by eleison »

Btw, can I copy and paste this code without it breaking through SSH?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Put objects in straight line, with speed.

Post by snibgo »

"montage -tile 9x1" Why 9? 8 looks more likely.

"results" should be the filename for an image, eg results.png or results.tiff or whatever.
snibgo's IM pages: im.snibgo.com
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Put objects in straight line, with speed.

Post by eleison »

Thanks, hum, I tried change results to the filename in my first try, but must have missed something else. Now it works, but they're not ending up as my result image at horizontal baseline.

convert box_line_test_nr.jpg \( -clone 0 -crop 33x41+44+286 +repage \) \( -clone 0 -crop 22x35+115+263 +repage \) \( -clone 0 -crop 30x31+179+245 +repage \) \( -clone 0 -crop 31x34+254+238 +repage \) \( -clone 0 -crop 29x30+315+229 +repage \) \( -clone 0 -crop 33x32+383+257 +repage \) \( -clone 0 -crop 33x31+444+275 +repage \) \( -clone 0 -crop 33x33+502+301 +repage \) -fuzz 0% -trim +repage -delete 0 miff:- | montage -tile 8x1 -geometry +1+1 - test_montage.jpg

This is what I got:
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Put objects in straight line, with speed.

Post by snibgo »

The montage could probably use a gravity setting.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Put objects in straight line, with speed.

Post by fmw42 »

Montage needs -gravity but the only one that properly works is north. So you must flip all your input images and then flip back at the end

Try

Code: Select all

convert box_line_test_nr.jpg \
 \( -clone 0 -crop 33x41+44+286 +repage \) \
 \( -clone 0 -crop 22x35+115+263 +repage \)  \
\( -clone 0 -crop 30x31+179+245 +repage \)  \
\( -clone 0 -crop 31x34+254+238 +repage \)  \
\( -clone 0 -crop 29x30+315+229 +repage \)  \
\( -clone 0 -crop 33x32+383+257 +repage \)  \
\( -clone 0 -crop 33x31+444+275 +repage \)  \
\( -clone 0 -crop 33x33+502+301 +repage \) \
-fuzz 0% -trim +repage -delete 0 -flip miff:- |\
montage -gravity north -tile 8x1 -geometry +1+1 - miff:- |\
convert - -flip test_montage.jpg
eleison
Posts: 45
Joined: 2013-12-10T15:14:46-07:00
Authentication code: 6789

Re: Put objects in straight line, with speed.

Post by eleison »

Thanks! :)
Post Reply