Saturday 1 December 2012

Task of the day: sub-pixel translation from the command line

Here is a quick and dirty intro to performing sub-pixel translation with command line vips.

Because (currently) images are cropped very tight at the top and left, but not at the right and bottom, sub-pixel translations should be performed with coordinates in the interval (-1,0]. For example, a sub-pixel translation that takes the point (0,0) to (.25,.3333333) will look better if performed moving the point (0,0) to (-.75,-.6666667). (Note that -.75=.25-1, and -.6666667=.3333333-1.) The only difference between the two results has to do with how integer shifts, and using negative shifts will prevent the abyss from creeping into the final result.

So, let's suppose that we want to sub-pixel translate an image {input.img} with width m and height n by (s,t) where -1<s<=0 and -1<t<=0, putting the result in an image with the same dimensions (the image {output.img}).

The command is just this:

vips im_affinei {input.img} {output.img} {interpolator} 1 0 0 1 -- {s} {t} 0 0 {m} {n}

where you replace what's inside a curly bracket by the actual value. For example, if you decide to resample with the Nohalo interpolator, and your input and output images, named input.png and translated.png, are both 100x80, the code would be

vips im_affinei input.png translated.png Nohalo 1 0 0 1 -- -.75 -.6666667 0 0 100 80

to move the image by .75 to the left and .6666667 higher. ("--" is so that the minus signs are not understood as flag prefixes.)



No comments:

Post a Comment