0

I'm trying to apply projective homography on an image and a marked rectangle. My solution works well for all of the affine transformations, but in the case of projective transformations, there's a systematic error that keeps increasing when I run it in a loop. I can't seem to figure out the reason. A quick answer will be really appreciated. please have look on the code and screen shots. Thanks

clc;clear;close all;
background_img_dir = 'D:/eyedeus/dataset/background_imgs/';
background_img = [background_img_dir '1.jpg'];

img = imread('D:/workspace/dataset/taj.jpg');
rect = [236 333 325 226; 304 303 441 440];

K = eye(3);
steps=pi/100; ang=-steps;
rotaxis=[0.5,0.5,1]; rotaxis=rotaxis./norm(rotaxis);

N = 100;
for i = 1:N

    ang=ang+steps;
    R=makehgtform('axisrotate',rotaxis,ang);
    H = K * R(1:3,1:3) * inv(K);
    Hp = H';

    T = maketform('projective', Hp);
    xsize=size(img,1);
    ysize=size(img,2);
    t_img = imtransform(img, T, 'UData',[0 1],'VData',[0 1]);

    x = [rect ; ones(1,4)];
    pts = H * x;
    pts = pts(1:2, :) ./ repmat(x(3,:),2,1);

    clf;
    imshow(t_img); hold on;
    line([pts(1,:) pts(1,1)], [pts(2,:) pts(2,1)], 'color', 'b', 'LineWidth', 2);
    pause(0.1);
end

First image with marked blue rectangle:

After applying homography:

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Ali Hassan
  • 1
  • 1
  • 1
  • Ali, Do you have any idea how to apply homography on the x-axis? please check my [question](http://stackoverflow.com/questions/42264563/homographic-image-transformation-issue-for-sattelite-images) – Addee Feb 17 '17 at 00:08

1 Answers1

1

Looks like a typo in the division step of the homography. Instead of this:

pts = H * x;
pts = pts(1:2, :) ./ repmat(x(3,:),2,1);

you want this:

pts = H * x;
pts = pts(1:2, :) ./ repmat(pts(3,:),2,1);
Peter
  • 14,559
  • 35
  • 55
  • didn't work, I know there's a difference of notation for homography matrix in imtransform, I can't figure it out, that's why the normalization isn't working, and the result is slightly off without normalization and is weird with normalization. – Ali Hassan Sep 27 '14 at 04:35
  • OK, then one more idea: You're specifying UData and VData, so that the image coordinates are rescaled between 0 and 1 before transforming. You probably need to rescale your rectangle coordinates by image width and height before transforming, then again back up to pixels after transforming. – Peter Sep 29 '14 at 13:23
  • @Peter Can you please check this [question](http://stackoverflow.com/questions/42264563/homographic-image-transformation-issue-for-sattelite-images) – Addee Feb 17 '17 at 00:09