Page 1 of 1

Correcting Alignment of an image series

Posted: 2016-08-11T10:10:44-07:00
by somew3irdo
Hi Guys,

I'm not sure how best to describe what i'm trying to do so please let me know if you need any clarifications.

Basically I have a set of 24 images that were taken of an object that was being rotated on a flat surface. The purpose is to create a smooth high quality short video of the object rotating. once I compile the images into a video, the object seems to bounce around on the screen. I believe this is because the object is not perfectly aligned to the center. All the images are just of that object and basically a white background that I can crop out. I'm trying to batch this process as there is a good number of objects that are already photographed that I'd like to run this on.

How can I process the image set so it can crop all the images while making sure that the object is centered perfectly in the center?

I'm using "Version: ImageMagick 7.0.2-7 Q16 x64 2016-08-06 " on a Win32(64) machine.

I've already attempted to use the -gravity center option with crop with no luck.
"convert img001.png -gravity Center -crop 3840x2160+0+0 result.png"

I appreciate any help on this.

Thanks

Re: Correcting Alignment of an image series

Posted: 2016-08-11T11:53:13-07:00
by fmw42
Try adding +repage after -crop 3840x2160+0+0

Code: Select all

convert img001.png -gravity Center -crop 3840x2160+0+0 +repage result.png
Does that help? If not, then it would help us understand the issue, if you could post a few of the images. You can upload to some place such as dropbox.com and put the URLs here.

Re: Correcting Alignment of an image series

Posted: 2016-08-11T16:51:06-07:00
by snibgo
somew3irdo wrote:Basically I have a set of 24 images that were taken of an object that was being rotated on a flat surface.
If the camera was hand-held, you first need to align the images, perhaps using some immobile background feature. My pages have a number of methods for this.

If the camera was on a tripod, then I don't understand the problem.

Re: Correcting Alignment of an image series

Posted: 2016-08-16T07:58:24-07:00
by jmac698
I believe the problem is, imagine watching a record spin on a turntable, from the side, except the cheap record has it's hole off-centre so it wavers to the side as you watch it spin. One way to fix that is a type of crop that can search the left/right edges for "non-white" and crop at that point. If the object is rotationally symmetrical, that would centre it.

You could also attempt calculate it's off-centredness and do a gradual left/right shift to compensate. That could be done easily in Avisynth, not imagemagick.

Finally, you could attempt to do a kind of image alignment with hugin.

Re: Correcting Alignment of an image series

Posted: 2016-08-16T09:52:40-07:00
by GeeMack
jmac698 wrote:I believe the problem is, imagine watching a record spin on a turntable, from the side, except the cheap record has it's hole off-centre so it wavers to the side as you watch it spin. One way to fix that is a type of crop that can search the left/right edges for "non-white" and crop at that point. If the object is rotationally symmetrical, that would centre it.
I use IM7 on Windows 10 64. I haven't tested the following suggestions, but the concepts should work with a little tweaking. So...

If the backgrounds are all some particular color, give or take a few percent, you could use "-trim" to clear that color right up to the relevant part of the images, then re-center them on a new background. That might look something like this...

Code: Select all

magick *.jpg ^
   -set option:originalsize "%[w]x%[h]" ^
   -fuzz 5% ^
   -trim ^
   +repage ^
   -background white ^
   -gravity center ^
   -extent "%[originalsize]" ^
      result_%03d.jpg
That would output your originals as "result_000.jpg", "result_001.jpg", etc., after trimming all the outer color plus or minus 5% of white, and re-centering the images on white backgrounds the size of the original images.
You could also attempt calculate it's off-centredness and do a gradual left/right shift to compensate. That could be done easily in Avisynth, not imagemagick.
That can be done without much difficulty in IM7, too, if the movement is steady and in a consistent direction across the series of images. One approach would look something like this...

Code: Select all

magick *.jpg -distort affine "0,0 %[fx:t*(80/(n-1))],0" result_%03d.jpg
First you'll need to manually determine the amount of shift from the first to the last image and use that value where I used "80" in the FX expression above. The command will output your modified images as "result_000.jpg", "result_001.jpg" etc. Each image in the stack "t" will be moved to the right by the total amount of shift "80" divided by one less than the number of images "(n-1)".

You can slide them to the left by making that FX expression a negative number. You can also combine that movement with a shift up or down by adding a similar FX expression as the vertical coordinate in the second coordinate pair. Something like this would shift the images 80 pixels to the right and 30 pixels up across the series of images...

Code: Select all

... "%[fx:t*(80/(n-1))],-%[fx:t*(30/(n-1))]" ...
There will be some streaks on one edge or another of the shifted images that you'll need to crop, chop, slice, or otherwise deal with because of shifting the image out one side of the canvas and dragging the edge pixels in from the other.

In a BAT file those single percent signs "%" will all need to be doubles "%%".

Re: Correcting Alignment of an image series

Posted: 2016-08-28T12:12:41-07:00
by bspencer
snibgo wrote:
somew3irdo wrote:Basically I have a set of 24 images that were taken of an object that was being rotated on a flat surface.
If the camera was hand-held, you first need to align the images, perhaps using some immobile background feature. My pages have a number of methods for this.

If the camera was on a tripod, then I don't understand the problem.
I went to your page at http://im.snibgo.com/ . Thanks for all of the time you've put into that!

I have the same issue - two images of the exact same scene but one is shifted. I would like to have both images centered the same. Which of the methods on your page would accomplish this?

Thanks!

Re: Correcting Alignment of an image series

Posted: 2016-08-28T12:49:39-07:00
by snibgo
bspencer wrote:...two images of the exact same scene but one is shifted.
It depends on what you mean by "shifted".

Perhaps they are two crops from the same original image, so the shifting is a simple translation with no scaling or rotation. Is so, then just pick an area on one image and search for that in the other. See my "Searching an image" page. The area you pick should have some detail; if you pick plain blue sky, this will randomly match some other area of plain blue sky.

If the shifting is more complex, then so is the solution, and many of my pages give methods.

I've recently developed a new method that starts by finding rotation, and then translation, and then scale. (Not yet published.)

Re: Correcting Alignment of an image series

Posted: 2016-08-28T12:53:40-07:00
by bspencer
For example, if you had two images of a person sitting on a beach. In one image the person is facing to the left and the other to the right. However, the photos were taken with a hand-held device and, in turn, the person is slightly shifted to the right in one image. The goal is for the person to be in the same, centered position in each photo.

Re: Correcting Alignment of an image series

Posted: 2016-08-28T13:23:24-07:00
by snibgo
You can upload images to somewhere like dropbox.com and paste URLs here.

Perhaps you want the faces to be aligned, so the problem is to find the centre of the face in each photo, and simple subtraction then gives you the translation.

That requires that we detect the positions of the face. Sadly, I don't have a method for detecting faces (but I'd like one). This isn't an easy problem. For some images, we may be able to identify what isn't a face, eg because it is sky, bench or grass.

Re: Correcting Alignment of an image series

Posted: 2016-08-28T13:26:08-07:00
by bspencer
I appreciate your help. I'm going to do some digging around in GIMP and see if I can figure something out. Thanks again!

Re: Correcting Alignment of an image series

Posted: 2016-08-28T13:41:17-07:00
by fmw42
You can pick control points in both images. The use +distort perspective to warp one to match the other.

Am I correct that you want the chairs and body aligned, so that when you alternate images, you see the head moving back and forth.

If that is the case, then pick control points on the chair and/or body and do the perspective transformation. See

http://www.imagemagick.org/Usage/distor ... rol_points
http://www.imagemagick.org/Usage/distor ... t_viewport
http://www.imagemagick.org/Usage/distorts/#perspective
http://www.imagemagick.org/Usage/distorts/#box3d

If you post a pair of images, we can probably help more after we see what the issue is.

Re: Correcting Alignment of an image series

Posted: 2016-08-28T17:03:38-07:00
by glennrp
snibgo wrote: Sadly, I don't have a method for detecting faces (but I'd like one).
Funny you should ask. I just posted an image to Facebook a few minutes ago and was amused by the face recognition that it did:
https://www.facebook.com/photo.php?fbid ... 8133015969 (note, facebook didn't supply the names, I did that, but facebook located them on the photo for me).