Prev: C++ communication tcp/ip with simulink
Next: Clustering
From: Shalin Mehta on 24 Feb 2010 06:58 Hi Dumebi, I did not receive a notification of your reply - so didn't get back to you on this. interp2's syntax is as follows: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/interp2.html out=interp2(xx,yy,in,xxi,yyi). You could do the following to shift a matrix by 0.5 pixels in X and 0.75 pixels in Y. [M N]=size(in); %Assuming square matrix. [xx yy]=meshgrid(1:N,1:M); %xx,yy are both outputs of meshgrid (so called plaid matrices). out=interp2(xx,yy,in,xx+0.5,yy+0.75); best Shalin "Dumebi Okwechime" <dumebi833(a)hotmail.com> wrote in message <hceds5$fa0$1(a)fred.mathworks.com>... > Hi Shalin, > > Was wondering what your input parameters were when using interp2. > Thanks > > Dumebi > > "Shalin Mehta" <shalinDOTmehtaHATESSPAM(a)gmail.com> wrote in message <h8svoe$rkb$1(a)fred.mathworks.com>... > > Just found a useful solution: Use interp2 with '*linear' option. Works factor of 2.5 faster than imtransform. Any other ideas? > > > > best > > shalin > > "Shalin Mehta" <shalinDOTmehtaHATESSPAM(a)gmail.com> wrote in message <h8ss9r$jhf$1(a)fred.mathworks.com>... > > > Hello world, > > > > > > I have data defined over a grid, e.g. > > > [xx yy]=meshgrid(-1:0.1:1); %each pixel represents distance of 0.1 > > > data=xx.^2+yy.^2 ; > > > > > > I need to translate this data by sub-pixel distances which will inevitably involve interpolation. I can live with linear interpolation as the data is sampled quite finely. I am using imtransform to achieve this. The shifted-data need to be obtained only over the original grid. I want extrapolated locations to be zero. > > > > > > xshift=0.235; yshift=-0.95; %Since shift is finer than sampling, sub-pixel shifting is required. > > > xdata=[1 wt]; > > > ydata=[1 ht]; > > > T=maketform('affine',[1 0 0; 0 1 0; xshift yshift 1]); > > > shifteddata=imtransform(data,T,'bilinear','XData',xdata,'YData',ydata); > > > > > > Results are satisfactory but slow as this shifting happens in the inner-most loop. > > > Can anyone suggest a method that involves direct indexing and hence would be faster? Any neat algorithm published in the literature for sub-pixel translation? > > > > > > best > > > Shalin
From: Matt J on 24 Feb 2010 10:12 "Shalin Mehta" <shalinDOTmehtaHATESSPAM(a)gmail.com> wrote in message <h8svoe$rkb$1(a)fred.mathworks.com>... > Just found a useful solution: Use interp2 with '*linear' option. Works factor of 2.5 faster than imtransform. Any other ideas? ================= Yes, interp2 will still be quite slow because of all the different functionality it has to support, for example, interpolation on non-uniform grids. It is much faster to express the interpolation as a separable convolution and use conv2(). Namely if your shift in x and y are 0<sx<1 and 0<sy<1 respectively, then you can do Image=conv2(Image,[sx, 1-sx],'same'); Result=conv2(Image,[sy, 1-sy],'same');
From: Rajesh on 25 Feb 2010 01:02 interp2 works well alright (thanks shalin mehta) but it fills the shifted region of the matrix with NaNs. may be it can be padded by copying the adjacent columns or rows, as the case may be, using 'padarray' function. but again this loop takes longer time. especially when you are dealing with series of matrices/images to be shifted in a loop as Mat also points out. @Mat does conv2 work for fractions >1?? but even this, i guess, leaves the shifted column (for shift in x direction) in shambles. Wish we could have something like 'circshift' handle fractions along with integers. :( 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. and i need to implement it in a loop for several images with varying values of shift in x direction. so needless to say the faster the better as the original post has asked for. thanks.
From: Matt J on 25 Feb 2010 01:54 "Rajesh " <rv_acharya(a)rediffmail.com> wrote in message <hm53ot$ig8$1(a)fred.mathworks.com>... > 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: http://www.mathworks.com/matlabcentral/fileexchange/26292-regular-control-point-interpolation-matrix-with-boundary-conditions > > @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 Result=zeros(size(Image)); Result(1:end+1-k,1:end+1-j)=Image(k:end,j:end); 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.
From: Matt J on 25 Feb 2010 10:25
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hm56qd$t3c$1(a)fred.mathworks.com>... > 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. ================ Come to think of it, that can't (always) be true. The interpolation only boils down to a convolution if the sample spacing is 1 pixel. So the question is whether interp2 is smart enough to check for this special case. |