[37] | 1 | % TEST_WARP Test image warping routines |
---|
| 2 | |
---|
| 3 | % load test image |
---|
| 4 | I = imread('pu.png') ; |
---|
| 5 | I = im2double(I) ; |
---|
| 6 | [M,N,K] = size(I) ; |
---|
| 7 | |
---|
| 8 | % dense grid |
---|
| 9 | [x,y] = meshgrid(1:N,1:M); |
---|
| 10 | |
---|
| 11 | % control point grid |
---|
| 12 | xr=linspace(1,N,5) ; |
---|
| 13 | yr=linspace(1,M,5) ; |
---|
| 14 | [xc,yc]=meshgrid(xr,yr) ; |
---|
| 15 | |
---|
| 16 | % -------------------------------------------------------------------- |
---|
| 17 | % test affine defromation |
---|
| 18 | % -------------------------------------------------------------------- |
---|
| 19 | A = [1 0 ; 0 1] ; |
---|
| 20 | T = [0;0] ; |
---|
| 21 | |
---|
| 22 | figure(100);clf; |
---|
| 23 | for i=1:10 |
---|
| 24 | % backward projection of dense grid |
---|
| 25 | [wx,wy]=waffine(A,T,x,y) ; |
---|
| 26 | J = imwbackward(1:N,1:M,I,wx,wy); |
---|
| 27 | |
---|
| 28 | % forward projection of control grid |
---|
| 29 | [xg,yg]=waffine(inv(A),-inv(A)*T,xc,yc) ; |
---|
| 30 | |
---|
| 31 | % change randomly the affine tf |
---|
| 32 | A = A + .1*randn(2) ; |
---|
| 33 | T = T + .1*randn(2,1) ; |
---|
| 34 | |
---|
| 35 | % plot deformed image and grid |
---|
| 36 | tightsubplot(10,i) ;imagesc(J) ;colormap gray; |
---|
| 37 | plotgrid(xg,yg,'color','g','linewidth',4) ;axis off; |
---|
| 38 | end |
---|
| 39 | |
---|
| 40 | drawnow ; |
---|
| 41 | |
---|
| 42 | % -------------------------------------------------------------------- |
---|
| 43 | % test thin-plate spline |
---|
| 44 | % -------------------------------------------------------------------- |
---|
| 45 | Y= [xc(:)';yc(:)'] ; |
---|
| 46 | U = tps(x,y,Y) ; |
---|
| 47 | Y0=Y ; |
---|
| 48 | |
---|
| 49 | figure(101);clf ; |
---|
| 50 | for i=1:10 |
---|
| 51 | % backward projection of dense grid |
---|
| 52 | [wx,wy] = wtps(U,Y) ; |
---|
| 53 | J = imwbackward(1:N,1:M,I,wx,wy); |
---|
| 54 | |
---|
| 55 | % forward projection of control grid |
---|
| 56 | [xg,yg]=witps(xc,yc,Y0,Y) ; |
---|
| 57 | |
---|
| 58 | % change TPS randomly |
---|
| 59 | Y=Y+3*randn(size(Y)) ; |
---|
| 60 | |
---|
| 61 | % plot deformend image and grid |
---|
| 62 | tightsubplot(10,i) ;imagesc(J) ;colormap gray; hold on ; |
---|
| 63 | plotgrid(xg,yg,'color','g','linewidth',4) ; axis off ; |
---|
| 64 | end |
---|