I want to implement one of the following mappings in MATLAB and verify its performance on an image, but I couldn't. Please help me with the implementation
1 Answer
You want to apply to your original image $I_1[n,m]$ a transform (x,y)=T(n,m) such as a warping effect, to obtain a new image $I_2[n,m]$.
In your example, you want to shift original pixel positions (n,m) to new coordinates based on the calculated position (x,y) from the transform equation (x,y) = T(n,m). As usually the case, this direct transform of indices will possibly result in noninteger positions (x,y) and therefore be quantized to slightly distorted locations, or even worse some pixels of the final image may not be filled, leaving them empty.
One approach to solve these two problems is to apply the algorithm from the reverse, by using the inverse transform of T(,) provided it exists. As in the pseudo matlab code here: Result is as shown below As a specific case of your examples: WAVE1: T(n,m) = (n + 20*sin(2*pi*m/128) , m ) then the inverse transform is invT(x,y) =( x - 20*sin(2*pi*y/128) , y) which yields m = y and n = x - 20*sin(2*pi*y/128)
% NOTE1: This code is not efficient. You shall Vectorize it easily
% and get rid of the for-loops, yielding a substantial boost in efficiency.
% NOTE2: boundary rows/columns may be left untouched which needs a simple fix
% The transform that you want to appply is (x,y)=T(n,m)
% The transform that we will use is its inverse (n,m)=invT(x,y) (you shall invert your transform)
% Original image I1 is of size S1xS2, transformed image I2 is of size N1xN2
for x=1:N1 % let N1 = S1 and N2= S2 for this example WAVE1
for y=1:N2
% s1- compute back coordinates in the original image domain
% [n m] = invT(x,y);
m = y;
n = x - 20*sin(2*pi*y/128);
% s2-s3 Apply (bilinear) interpolation to find the image intensity, if
% n,m does not fall into integer indices
d1 = n - floor(n);
d2 = m - floor(m);
% s3- define the image pixel
if ( ((n>=1)&(n<S(1))) & ((m>=1)&(m<S(2))) )
I2(x,y) = (1-d1)*(1-d2) * I1(floor(n),floor(m)) + ...
(1-d1)*d2 * I1(floor(n),floor(m)+1) + ...
d1*(1-d2) * I1(floor(n)+1,floor(m)) + ...
d1*d2 * I1(floor(n)+1,floor(m)+1) ;
end%if
end
end