📄 lhidbshowgeometry.m
字号:
function LHIdbshowgeometry(D, imageIndex, HOMEIMAGES)
%% LHIdbshowgeometry(D, imageIndex, HOMEIMAGES)
% Example
% LHIdbshowgeometry(LHIquery(D,'folder', 'badminton'),1,HOMEIMAGES);
%
img = LHIimread(D,imageIndex,HOMEIMAGES);
geo = D(imageIndex).annotation.geometry;
handle=figure;maximize(handle); %maximize current display window
[nrow,ncol,channel] = size(img);
if ( length(geo.LongLine) == 4 ) %4 long lines, can compute vanishing points
try
vx=[]; vy=[]; %vanishing point
%% vanishing point
for i=1:2
vx(i) = round(str2num(geo.VanishPoint(i).x));
vy(i) = round(str2num(geo.VanishPoint(i).y));
end
%% classify long lines into two groups by their distances to vanishing points
c1 = 1; c2=3;
X=[];Y=[];XX=zeros(4,2);YY=XX;
for i=1:4,
X = [str2num(geo.LongLine(i).X1) str2num(geo.LongLine(i).X2)];
Y = [str2num(geo.LongLine(i).Y1) str2num(geo.LongLine(i).Y2)];
p1 = [X(1) Y(1) 0];
p2 = [X(2) Y(2) 0];
v1 = [vx(1) vy(1) 0];
v2 = [vx(2) vy(2) 0];
d1 = norm(cross(p2-p1,v1-p1))/norm(p2-p1); %calculate distance
d2 = norm(cross(p2-p1,v2-p1))/norm(p2-p1); %calculate distance
if (d1<d2)
XX(c1,:) = X; YY(c1,:) = Y; c1 = c1 + 1;
else
XX(c2,:) = X; YY(c2,:) = Y; c2 = c2 + 1;
end
end
%find vanishing point (intercept point of two long lines)
[vx(1),vy(1)] = segintercept(XX(1,:),YY(1,:),XX(2,:),YY(2,:));
[vx(2),vy(2)] = segintercept(XX(3,:),YY(3,:),XX(4,:),YY(4,:));
% compute boundary
minX=1;minY=1;maxX=ncol;maxY=nrow;
minX = min(minX,round(min(min(vx(1)),min(vx(2)))));
minY = min(minY,round(min(min(vy(1)),min(vy(2)))));
maxX = max(maxX,round(max(max(vx(1)),max(vx(2)))));
maxY = max(maxY,round(max(max(vy(1)),max(vy(2)))));
% Offset axis
vx = vx - minX + 1;
vy = vy - minY + 1;
spaceImage = uint8(ones(maxY-minY+1,maxX-minX+1,channel)*128);
spaceImage((1-minY+1):(nrow-minY+1),(1-minX+1):(ncol-minX+1),:) = img;
imshow(spaceImage,[]); hold on;
%plot lines
for i=1:2,
X = XX(i,:) - minX;
Y = YY(i,:) - minY;
X1 = XX(i+2,:) - minX;
Y1 = YY(i+2,:) - minY;
plot(X,Y,'-','LineWidth',1,'color','r');
plot(X1,Y1,'-','LineWidth',1,'color','b');
dist1 = sqrt((X(1)-vx(1))^2+(Y(1)-vy(1))^2);
dist2 = sqrt((X(2)-vx(1))^2+(Y(2)-vy(1))^2);
if dist1 < dist2
plot([X(1) vx(1)],[Y(1) vy(1)],'-.','LineWidth',2 ,'color', 'r');
else
plot([X(2) vx(1)],[Y(2) vy(1)],'-.','LineWidth',2 ,'color', 'r');
end
dist1 = sqrt((X1(1)-vx(2))^2+(Y1(1)-vy(2))^2);
dist2 = sqrt((X1(2)-vx(2))^2+(Y1(2)-vy(2))^2);
if dist1 < dist2
plot([X1(1) vx(2)],[Y1(1) vy(2)],'-.','LineWidth',2 ,'color', 'b');
else
plot([X1(2) vx(2)],[Y1(2) vy(2)],'-.','LineWidth',2 ,'color', 'b');
end
end
plot((vx),(vy),'--o','LineWidth',5 ,'color', 'g','MarkerFaceColor','b','MarkerEdgeColor','b',...
'MarkerSize',8);
catch
disp('too large to display');
end
elseif (length(geo.LongLine) == 1) % one long line -> horizon
X = round([str2num(geo.LongLine(1).X1) str2num(geo.LongLine(1).X2)]);
Y = round([str2num(geo.LongLine(1).Y1) str2num(geo.LongLine(1).Y2)]);
image(img);hold on; axis off; axis equal;
plot(X,Y,'--','LineWidth',5,'color','g');
else
disp('No geometry information');
image(img);axis off; axis equal;
end
%maximize(handle); %maximize current display window
%% find intercept point of two line segments
function [x,y] = segintercept(X1,Y1,X2,Y2)
k1 = (Y1(1)-Y1(2))/(X1(1)-X1(2));
x1 = X1(1);
y1 = Y1(1);
k2 = (Y2(1)-Y2(2))/(X2(1)-X2(2));
x2 = X2(1);
y2 = Y2(1);
x = (k1*x1-k2*x2+y2-y1)/(k1-k2);
y = (k1*k2*(x1-x2)+y2*k1-y1*k2)/(k1-k2);
%prevent over flow
x = max(x,-50000);
x = min(x,50000);
y = max(y,-50000);
y = min(y,50000);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -