% ortofoto demo - data upravena z klad55.jpg 
% http://geoportal.cuzk.cz/UKAZKOVA_DATA/ORTOFOTO_JPG.zip

url = 'http://geo.fsv.cvut.cz/user/gin/git1/matlab/img/';
A = imread(strcat(url, 'klad55-ortofoto.jpg')); % ortofoto snimek (vyrez)      
B = imread(strcat(url, 'klad55-perspektiva.jpg'));

A_ib = [115 123; 445 177; 467 302; 332 362; 
        279 446;  95 234; 389 465; 170 533];
B_ib = [ 42  93; 409  70; 467 174; 345 260; 
        312 353;  48 200; 436 337; 214 467];
    
% obraz B je trucolor, tj. pole o trech dimenzich, viz XData a YData
P = cp2tform(B_ib, A_ib, 'projective');

figure(2), imshowpair(A, Q, 'falsecolor');

B( 1 , : ,:) = 0;    B( 1 , : ,3) = 255;    
B(end, : ,:) = 0;    B(end, : ,3) = 255; 
B( : , 1 ,:) = 0;    B( : , 1 ,3) = 255;    
B( : ,end,:) = 0;    B( : ,end,3) = 255;

R = size(A,1);
S = size(A,2);

for r = 1:size(B,1)
    for s = 1:size(B,2)
       [y, x] = tformfwd(P, s, r);
       x = round(x);
       y = round(y);
       if x < 1 || x > S || y < 1 || y > R
          continue;
       end
       A(x,y,1) = B(r,s,1);
       A(x,y,2) = B(r,s,2);
       A(x,y,3) = B(r,s,3);
    end
end

figure(1), imshow(A);
% imwrite(A, 'klad55-vystup-m.png', 'png');
