Page 1 of 1

Put objects in straight line, with speed.

Posted: 2015-02-12T09:45:39-07:00
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!

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T11:05:52-07:00
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.

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T12:35:54-07:00
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?

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T13:22:06-07:00
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.

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T14:24:26-07:00
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.

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T14:25:34-07:00
by eleison
Btw, can I copy and paste this code without it breaking through SSH?

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T14:54:38-07:00
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.

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T15:08:47-07:00
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

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T15:17:05-07:00
by snibgo
The montage could probably use a gravity setting.

Re: Put objects in straight line, with speed.

Posted: 2015-02-12T16:08:59-07:00
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

Re: Put objects in straight line, with speed.

Posted: 2015-02-15T08:40:02-07:00
by eleison
Thanks! :)