Using Optical Flow to warp an image
Show older comments
Hi guys. I have computed the optical flow between images A and B. I wish to use this to warp image C to D. The problem is: the flow is a velocity vector with decimal values. Images are in the form of matrices for which rows and columns are integer values. Hence, C(i,j)+ optical flow(i,j) won't give me D(i,j) Please tell me how I can convert a matrix to a graph or how I can handle the decimal values. Thanks !!!
2 Comments
David Young
on 12 Dec 2011
Do you have a single optic flow vector for the whole image, or a sparse set of optic flow vectors for some features in the image, or a dense set of optic flow vectors for each pixel in the image?
QEWE
on 14 Dec 2011
Answers (1)
David Young
on 14 Dec 2011
Given a vector for every pixel, you can use interp2 to do the warping and to handle the non-integer lookup. Suppose that vx represents the x-component of the flow, such that the rightwards component of velocity at D(i,j) is x(i,j). Likewise vy is the y-component. Then you can do something similar to this:
C= imread('pout.tif'); % test image
[x, y] = meshgrid(1:size(C,2), 1:size(C,1));
% generate synthetic test data, for experimenting
vx = 0.1*y; % an arbitrary flow field, in this case
vy = 0.1*x; % representing shear
% compute the warped image - the subtractions are because we're specifying
% where in the original image each pixel in the new image comes from
D = interp2(double(C), x-vx, y-vy);
% display the result
imshow(D, []);
The bit that matters is the line in the middle with the call to interp2.
By the way, if you had a sparse set of flow vectors instead, you'd need to look at using imtransform with maketform.
5 Comments
QEWE
on 15 Dec 2011
QEWE
on 15 Dec 2011
David Young
on 15 Dec 2011
The example I gave works, so I'm not sure why you are not getting the results you expect with your data. interp2 does indeed ensure that every pixel moves as specified by vx and vy for that pixel. Are you sure that your vx and vy arrays are correct?
QEWE
on 16 Dec 2011
Natesh Srinivasan
on 17 Mar 2013
@David
I think this is incorrect for non uniform flow fields. For Example, consider the following code, where A is the input image and B is the 'warped' image
function testinterpolation()
A = rand(5,5)
[X, Y] = meshgrid([1:5],[1:5]);
Mx = X/10;
My = Y/10;
xplusMx = X - Mx;
yplusMy = Y - My;
B = interp2 (A,xplusMx,yplusMy,'linear',0)
end
we cannot predict where a particular pixel has come from if by simply taking a minus sign because this could have been from a completely different pixel.
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!