0
votes

I am working on image warping. I have matrix D and I, I want to get J by using function interp2 The definition of J is

,

where D and I have the same size.

Obviously, some values in matrix J is not assigned, I want to assign them with values by using interp2.

This is what I've tested for transform without using interp2

for i=1:1:h
    for j = 1:1:w
         J(i,j+img_d(i,j)) = img_c(i,j);
    end
end

J has a lot of value un-assigned. Interp2 provides some interpolation algorithms, but I don't know how to use.

Then I tried the following:

img_d = imread('1_d.png');
img_c = imread('1_c.png');
[h,w] = size(img_d);
for i = 1:1:h
    for j = 1:1:w
        J(i,j+img_d(i,j)) = img_c(i,j,1);
    end
end
[Xq,Yq] = meshgrid((1:1:w),(1:1:h));
V = interp2(J,Xq,Yq,'spline');

And matlab reports errors:

Error using griddedInterpolant Sample values must be a single or double array.

Error in interp2>makegriddedinterp (line 228) F = griddedInterpolant(varargin{:});

Error in interp2 (line 112) F = makegriddedinterp({X,Y},V,method,extrap);

The error is because that the dimension of J is not the same as the meshgrid, how to fix this?

This is what I implemented for backward warping

    img_d = imread('1_d.png');img_c = imread('1_c.png');
    [h,w] = size(img_d);
    J_1 = zeros(h,w);
    J_2 = zeros(h,w);
    J_3 = zeros(h,w);
    for i = 1:1:h
        for j = 1:1:w
            min_value = j;
            col = 1;
            for k = 1:1:w
                temp = j-(k + img_d(i,k));
                if temp < min_value
                    min_value = temp;
                    col = k;
                end
            end
            J_1(i,j) = img_c(i,col,1);
            J_2(i,j) = img_c(i,col,2);
            J_3(i,j) = img_c(i,col,3);
        end
    end
    J = cat(3,J_1,J_2,J_3);

Too slow.....

1
Could you please be more precise? What is it you don't understand of matlab's help?lrnzcig
What is in D? Integers, floats?Dan
@Dan the values in D and I are all integers, I think I need to use reverse interpolation instead of fowardgood BOB
@goodBOB I don't think you need interpolation here (unless you are trying to vectorize it using nearest neighbour interp but doesn't sound like it). Can you not just do this with indexing. You would only need to interpolate if your transformed image was "referencing" your source image in between pixels. But if D is all integers then it's basically just jumbling up the elements. I suggest you make a 3-by-3 toy example with all inputs and outputs to make your question clearerDan
@Dan Ok,I will add an example. But simply by referencing, is it guaranteed that every pixel of J is assigned with a value?good BOB

1 Answers

0
votes

The answer is simple, just reverse the matrix, interp2 is used for imresize