www.gusucode.com > 基于matlab的三维重建代码,提取两幅图片的匹配点源码程序 > 基于matlab的三维重建代码,提取两幅图片的匹配点源码程序/code/code/distortion.m

    %  [rectified_image] = distortion(original_image,distortion_coefficient)
%
% Support routine to TwoViewDemo.m
% Reconstruction from two uncalibrated views
% As described in Chapter 5, "An introduction to 3-D Vision"
% by Y. Ma, S. Soatto, J. Kosecka, S. Sastry (MASKS)
%
% Code distributed free for non-commercial use
% Copyright (c) MASKS, 2003
%
% Last modified 5/5/2003

function [im_out] = distortion(img, K)
s =  size(img);

if size(s,2) == 3   % color image
 im = double(img);
 im1	= im(:,:,1);  
 im2	= im(:,:,2);
 im3	= im(:,:,3);
 [ydim,xdim] = size( im1 );
 im1	= im1 - min(im1(:));
 im1	= im1 / max(im1(:));
 im2	= im2 - min(im2(:));
 im2	= im2 / max(im2(:));
 im3	= im3 - min(im3(:));
 im3	= im3 / max(im3(:));


 [x,y]	= meshgrid( 2/xdim*([1:xdim]-xdim/2), 2/ydim*([1:ydim]-ydim/2) );
 R 	= x.^2 + y.^2;
 x2 	= x .* (1 - K*R);
 y2 	= y .* (1 - K*R);
 alpha	= max( max(x2(:)), max(y2(:)) );
 x2	= x2 / alpha;
 y2	= y2 / alpha;
 im_out(:,:,1)	= interp2( x,y,im1,x2,y2,'cubic' );
 im_out(:,:,2)	= interp2( x,y,im2,x2,y2,'cubic' );
 im_out(:,:,3)	= interp2( x,y,im3,x2,y2,'cubic' );
 ind    = find( isnan(im_out) );	% FIND NANs
 im_out(ind) = 0;	
		% REPLACE WITH ZEROS
 im_out		= im_out - min(im_out(:));
 im_out		= im_out / max(im_out(:));
 im_out          = im_out(2:ydim,2:xdim,:);
 % imagesc(im_out, [0 1]); axis image off; 
else 
 im = double(img);
 [ydim,xdim] = size( im );
 im	= im - min(im(:));
 im	= im / max(im(:));
 [x,y]	= meshgrid( 2/xdim*([1:xdim]-xdim/2), 2/ydim*([1:ydim]-ydim/2) );
 R 	= x.^2 + y.^2;
 x2 	= x .* (1 - K*R);
 y2 	= y .* (1 - K*R);
 alpha	= max( max(x2(:)), max(y2(:)) );
 x2	= x2 / alpha;
 y2	= y2 / alpha;
 im_out	= interp2( x,y,im,x2,y2,'cubic' );
 ind    = find( isnan(im_out) );	% FIND NANs
 im_out(ind) = 0;	
		% REPLACE WITH ZEROS
 im_out		= im_out - min(im_out(:));
 im_out		= im_out / max(im_out(:));
 im_out          = im_out(2:ydim,2:xdim,:);
 % imagesc(im_out, [0 1]); axis image off; 
end;