www.pudn.com > src.rar > warp.m, change:2010-07-20,size:3899b


 
%-------------------------------------------------------- 
% input : texref (texture of reference view) 
%         dmref (depth map of reference view) 
% 
% flow : (1) synthesis a disparity map 
%        (2) warp pixels of each view using disparity map 
%-------------------------------------------------------- 
 
function result= warp(texref,dmref)  
 
% unit: centimeter  
Zc=250;  
zfar=1000;  
znear=100;  
 
tc=6.4;  
f=30;  
 
kx=40; 
au=f*kx;    % not sure 
 
% disparity: disparity of each pixel (ref view coord)  
disparity=zeros(size(texref,1),size(texref,2),1); 
 
% mpdp : max positive disparity of this frame (outward) 
% (unit : pixels) 
mpdp=0; 
% mndp : max negative disparity of this frame (inward) 
mndp=0; 
 
%---------------------------------------------- 
% desc: synthesis a disparity map 
%---------------------------------------------- 
for y=1:size(texref,1)  
    for x=1:size(texref,2)  
         
        % get "actual" depth Z from depth map  
        v=double(dmref(y,x,1));  
        Z=zfar+v*(znear-zfar)/255;  
 
        % claculate disparity of each pixel 
        disparity(y,x)=((au*tc)/(2*Z))-((au*tc)/(2*Zc)); 
         
        if(disparity(y,x)>mpdp) 
            mpdp=disparity(y,x); 
        end 
         
        if(disparity(y,x)<mndp) 
            mndp=disparity(y,x); 
        end 
    end  
end 
 
% actual disparity on screen will be left disparity + 
% right disparity, so we have to multiply it by 2 here. 
mpdp=mpdp*2; 
mndp=mndp*2; 
 
%--------------------------------------------------- 
% desc: warp pixels of each view using disparity map 
%--------------------------------------------------- 
 
% texwarp : warped texture 
texwarpL=uint8(zeros(size(texref,1),size(texref,2),3));  
texwarpR=uint8(zeros(size(texref,1),size(texref,2),3)); 
 
% dmwarp : warped depth map 
dmwarpL=uint8(zeros(size(texref,1),size(texref,2),1)); 
dmwarpR=uint8(zeros(size(texref,1),size(texref,2),1)); 
 
% crack : hole map (include crack and hole) 
% true -> hole, false -> not a hole 
crackR=boolean(ones(size(texref,1),size(texref,2))); 
crackL=boolean(ones(size(texref,1),size(texref,2))); 
 
% dpwarp : warped disparity map 
dpwarpL=zeros(size(texref,1),size(texref,2),1); 
dpwarpR=zeros(size(texref,1),size(texref,2),1); 
 
for y=1:size(texref,1)  
    for x=1:size(texref,2) 
         
        % because of "occlusion-compatible warp order" 
        % the order of warping will be inverse 
         
        % right view 
        % order -> left to right 
         
        % boundary check 
        if(x-round(disparity(y,x))>=1 && x-round(disparity(y,x))<=size(texref,2)) 
             
            % forward warping 
            texwarpR(y, x - round(disparity(y,x)),:)=texref(y,x,:); 
            dmwarpR(y, x - round(disparity(y,x)))=dmref(y,x); 
            dpwarpR(y, x - round(disparity(y,x)),:)=disparity(y,x,:); 
             
            % this pixel of the new view has a value, 
            % so this coord will not a hole. 
            crackR(y, x - round(disparity(y,x)))=false;             
        end 
         
        % left view 
        % order -> right to left 
        xt=size(texref,2)-x+1; 
 
        if(xt+round(disparity(y,xt))>=1 && xt+round(disparity(y,xt))<=size(texref,2)) 
            texwarpL(y, xt + round(disparity(y,xt)),:)=texref(y,xt,:); 
            dmwarpL(y, xt + round(disparity(y,xt)))=dmref(y,xt); 
            dpwarpL(y, xt + round(disparity(y,xt)),:)=disparity(y,xt,:); 
            crackL(y, xt + round(disparity(y,xt)))=false; 
        end 
    end  
end 
 
result=struct(  'tex_warped_left',texwarpL,'tex_warped_right',texwarpR,... 
                'dm_warped_left',dmwarpL,'dm_warped_right',dmwarpR,... 
                'crack_map_left',crackL,'crack_map_right',crackR,... 
                'dp_warped_left',dpwarpL,'dp_warped_right',dpwarpR,... 
                'max_pos_dp',mpdp,'max_neg_dp',mndp);