2

From the given matrices A and B I need to compute a new matrix C. Matrix A represents image pixels and C is a horizontally shifted version of A. The tricky part: this shift is defined per pixel by the values in the disparity matrix B. For exampe: While pixel (1,1) needs to be shifted 0.1 units to the right, pixel (1,2) needs to be shifted 0.5 units to the left.

I implemented this as backward-mapping, where for each pixel in C I compute the required source position in A (which is simply my current pixel's location minus the corresponding offset in B). Since non-integer shifts are allowed, I need to interpolate the new pixel value.

Doing this in Matlab, of course, takes quite some time as images get larger. Is there any built-in function I can utilize for this task?

Atze Kaputtnik
  • 262
  • 3
  • 10
  • 1
    check out the [GRIDDATA](http://www.mathworks.com/help/techdoc/ref/griddata.html) function for the interpolation – Amro Aug 20 '11 at 17:45

2 Answers2

1

The function interp2 interpolates values on a regularly spaced grid, such as a bitmap image. If your pixels didn't lie on a regular grid, then you would use griddata.

nibot
  • 14,428
  • 8
  • 54
  • 58
1

I assume that matrix A is an image, which means that the pixels are regularly spaced, which means you can use INTERP2. I also assume that you calculate for each pixel individually the interpolated value from A. You can, however, perform the lookup in one step, which will be quite a bit faster.

Say A is a 100x100 image, and B is a 10000-by-2 array with [shiftUpDown,shiftLeftRight] for each pixel. Then you'd calculate C this way:

%# create coordinate grid for image A
[xx,yy] = ndgrid(1:100,1:100);
%# linearize the arrays, and add the offsets
xx = xx(:);
yy = yy(:);
xxShifted = xx + B(:,1);
yyShifted = yy + B(:,2);
%# preassign C to the right size and interpolate
C = A;
C(:) = interp2(xx,yy,A(:),xxShifted,yyShifted);
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Yes, matrix A is an image. Judging from the other answers, the formulation in my question didn't really make that clear. INTERP2 and GRIDDATA both do the job just fine. Only that INTERP2 is a lot faster since it assumes regularly spaced data points at the source. – Atze Kaputtnik Aug 21 '11 at 11:30