source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/zisserman/vgg_multiview/vgg_poly3d_orthorectify.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

  • Property svn:executable set to *
File size: 3.2 KB
Line 
1%vgg_poly3d_orthorectify  Frontoparallel rectification of image of 3D polygon.
2%   [H,imsize] = vgg_poly3d_orthorectify(Q,u) finds homography H that
3%   removes perspective distortion of image Q*hom(u) of a 3D polygon. Parameters:
4%     u ... double(2,N), inhomog. coordinates of polygon vertices measured in an orthonormal
5%       coordinate frame in the 3D polygon plane.
6%     Q ... double(3,3), homography that maps u to image plane.
7%       Image of the polygon is thus x = Q*hom(u) (homog. coords.).
8%     H ... double(3,3), rectifying homography
9%     imsize ... double(2,1), size of the target image
10%   The rectification is done as follows:
11%     Irect = vgg_warp_H(I{k},inv(H),'linear',[1 imsize(2) 1 imsize(1)]).
12%
13%   [H,imsize] = vgg_poly3d_orthorectify(Q,u,smax) allows to specify maximum ratio of
14%   area element in output and input image (if omitted, smax=5). This is to prevent
15%   the output image from becoming huge if the perspective distortion is large.
16%
17%   If Q is K-cell of double(3,3), rectification is done simultaneously for K images
18%   of the 3D polygon, so that pixels with the same coordinates correspond in the
19%   rectified images. Then H is K-cell of double(3,3) and imsize is double(2,K).
20%
21%   [H,imsize] = vgg_poly3d_orthorectify(Q,u,smax,in_imsize) specifies sizes of input
22%   images. This is needed if some vertices project outside some input images.
23
24function [H,imsize] = vgg_poly3d_frontorectify(H,u,smax,imsize)
25
26if ~iscell(H)
27  H = {H};
28end
29
30if nargin<3
31  smax = 5;
32end
33
34% find scales of H in all u
35for k = 1:length(H)
36  if nargin>3 % consider only vertices inside input image
37    i = boxclipu( [[1;1] imsize(:,k)], vgg_get_nonhomg(H{k}*vgg_get_homg(u)) );
38  else
39    i = logical(ones(1,size(u,2)));
40  end
41  s(k,~i) = nan;
42  if nnz(i)==0, continue, end
43  s(k,i) = sqrt(abs( dethomog(H{k},u(:,i)) ));
44end
45
46% determine the scale;
47% forbid scaling polygons more than smax-times
48s = s(:);
49s(isnan(s)) = [];
50if isempty(s) % polygon is not in the image
51  imsize = [0;0];
52  return
53end
54if nargin < 5
55  smax = inf;
56end
57s = min(s) * min(smax,max(s)/min(s));
58
59% scale H and u consistently by s
60u = u*s;
61Hs = diag([1/s 1/s 1]);
62for k = 1:length(H)
63  H{k} = H{k}*Hs;
64end
65
66% translate the polygon to the origin
67b = bboxu(u);
68imsize = ceil( b(:,2) - b(:,1) );
69Ht = eye(3);
70Ht(1:2,3) = b(:,1) + 1;
71
72for k = 1:length(H)
73  H{k} = H{k}*Ht;
74end
75
76if length(H)==1
77  H = H{1};
78end
79
80return
81
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
83
84% D = dethomog(H,q)  Determinant of Jacobian of homography transformation r = nhom(H*hom(q)), D = det(dr/dq). Works vector-wise for size(q)=[2 ?].
85%
86function D = dethomog(H,q)
87% Derivative of homography r = h(q) = nhom(H*[q;1]) is:
88%   dh = [eye(2) -r]/y(3)*H(:,1:2), where y = H*[q;1].
89q(3,:) = 1;
90r = vgg_get_nonhomg(H*q);
91D = ((H(1,1)-r(1,:)*H(3,1)).*(H(2,2)-r(2,:)*H(3,2)) - (H(1,2)-r(1,:)*H(3,2)).*(H(2,1)-r(2,:)*H(3,1))) ./ (H(3,:)*q).^2;
92return
93
94
95function i = boxclipu(b,u)
96% i = boxclipu(b,u)  Returns logical indices of points inside box b.
97i = all( u >= b(:,row1(u)) & u <= b(:,2*row1(u)), 1 );
98return
99
100
101function ub = bboxu(u)
102% ub = bboxu(u)  Bounding box of point cloud u.
103ub = [min(u')' max(u')'];
104return
Note: See TracBrowser for help on using the repository browser.