From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: sub-pixel shifting of a matrix
Date: Thu, 25 Feb 2010 06:54:05 +0000 (UTC)
Organization: Xoran Technologies
Lines: 32
Message-ID: <hm56qd$t3c$>
References: <h8ss9r$jhf$> <h8svoe$rkb$> <hm3fkm$c2u$> <hm53ot$ig8$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1267080845 29804 (25 Feb 2010 06:54:05 GMT)
NNTP-Posting-Date: Thu, 25 Feb 2010 06:54:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: comp.soft-sys.matlab:611481

"Rajesh " <> wrote in message <hm53ot$ig8$>...
> interp2 works well alright (thanks shalin mehta) but it fills the shifted region of the matrix with NaNs. 

You can extrapolate with zeros or any other desired value using the EXTRAPVAL option.

>may be it can be padded by copying the adjacent columns or rows, as the case may be, using 'padarray' function. 

That's an option if you have the image processing toolbox. If you don't have it, you can create a convolution matrix modified for replicate extrapolation using my interpMatrix tool:

> @Mat
> does conv2 work for fractions >1?? but even this, i guess,  leaves the shifted column (for shift in x direction) in shambles. 

I don't know what you mean by leaving the column in shambles.

For shifts >1, you could just convolve using a zero-padded version of the convolution kernel. However, it would probably be more efficient to do an integer shift of the image first, which just requires that you reorder the image data, and do a fractional shift using the method I've described. circshift() will work for the integer shift, but I've found circshift to be slower than as compared to just reordering the data manually, as in


Incidentally, I've just noticed that interp2 has a "*METHOD" option allowing you to tell it that the samples are uniformly spaced, for more optimized execution. This probably means that interp2 will implement the interpolation as a separable convolution for you under the hood.

> I need to shift an image by say 6.3 pixels in x direction in such a way that as if only the object in the image has moved and the background is retained as it is.

If the object is completely surrounded by background values of zero, I don't see what's challenging about that. What I've outlined above will work.