Wy error..? Need correction...

Asked by Lalit Patil

Lalit Patil (view profile)

on 24 Jan 2013
a = 0.1; b = 0.1; c = 0.01; d = 0.001;
fx = 1800;
fy = 1800;
cx = 360;
cy = 240;
K = [fx 0 cx; 0 fy cy; 0 0 1];
I = zeros(size(Idistorted));
[i j] = find(~isnan(I));
Xp = inv(K)*[j i ones(length(i),1)]';
r2 = Xp(1,:).^2+Xp(2,:).^2;
x = Xp(1,:);
y = Xp(2,:);
x = x.*(1+a*r2 + b*r2.^2) + 2*c.*x.*y + d*(r2 + 2*x.^2);
y = y.*(1+a*r2 + b*r2.^2) + 2*d.*x.*y + c*(r2 + 2*y.^2);
u = reshape(fx*x + cx,size(I));
v = reshape(fy*y + cy,size(I));
I = interp1(Idistorted, u, v);

Walter Roberson

Walter Roberson (view profile)

on 24 Jan 2013

What error are you seeing? What is the error message? On which line?

Lalit Patil

Lalit Patil (view profile)

on 24 Jan 2013

Last line of code shows error.

??? Error using ==> interp1 at 121
X must be a vector.

Products

No products are associated with this question.

Wayne King (view profile)

Answer by Wayne King

Wayne King (view profile)

on 24 Jan 2013

interp1 is 1-D interpolation. If you want to perform interpolation on 2-D data, see the help for interp2

Walter Roberson (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 24 Jan 2013

What are you expecting when you attempt to use a 1 dimensional interpolation on data that is at least 2 dimensional (if it is grayscale) and possibly 3 dimensional (if it is RGB) ?

http://www.mathworks.com/help/matlab/ref/interp1.html

yi = interp1(x,Y,xi) interpolates to find yi, the values of the underlying function Y at the points in the vector or array xi. x must be a vector.

Notice that second sentence.

Also a question: your code has

x = x.*(1+a*r2 + b*r2.^2) + 2*c.*x.*y + d*(r2 + 2*x.^2);
y = y.*(1+a*r2 + b*r2.^2) + 2*d.*x.*y + c*(r2 + 2*y.^2);

In that code, you use the current values of x and y in the first line, and you write the result over-top of x. And then in the second line, you use the now-changed value of x, along with the unchanged value of y, to compute a new y that you then use to overwrite y. Is that your desired sequence? Because it is not clear from the structure of the code that you want the two lines to have results that depend on the order you execute the two, but with you overwriting the variables that way, you would get different results if you switched the two lines around. With the code appearing to be symmetric, it appears to me that you want the "x" on the right hand side of the first line to refer to the same values as the "x" on the right hand side of the second line.

Another question: you have

I = zeros(size(Idistorted));
[i j] = find(~isnan(I));

you just initialized I to all 0, so isnan(I) is going to be false for all of I, so ~isnan(I) is going to be true for all of I, so the result of the find() will be the coordinates of everything in I. If that is what you want, then why confuse things with the isnan() and why not just do

[i j] = find(true(size(I));

or why not build the coordinate list with ndgrid() ?

Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi