Image 在matlab中求相机到消失点的距离
我有一个程序,可以为给定的一组图像找到消失点。有没有办法找到相机和消失点之间的距离 同样,一旦找到消失点,我需要使用matlab中提供的工具手动找到X和Y坐标。如何编写将所有X和Y坐标写入文本或excel文件的代码段 还有没有更好更简单的方法在matlab中找到消失点 Matlab调用函数查找消失点: findVanishingPoint函数: 创建银行功能: gabormask函数: findHorizonEdge函数: findHorizon函数: 线条图像功能: lineBresenham函数:Image 在matlab中求相机到消失点的距离,image,matlab,image-processing,coordinates,vanishing-point,Image,Matlab,Image Processing,Coordinates,Vanishing Point,我有一个程序,可以为给定的一组图像找到消失点。有没有办法找到相机和消失点之间的距离 同样,一旦找到消失点,我需要使用matlab中提供的工具手动找到X和Y坐标。如何编写将所有X和Y坐标写入文本或excel文件的代码段 还有没有更好更简单的方法在matlab中找到消失点 Matlab调用函数查找消失点: findVanishingPoint函数: 创建银行功能: gabormask函数: findHorizonEdge函数: findHorizon函数: 线条图像功能: lineBresenham
函数[i]=lineBresenham(H、W、Sx、Sy、角度)
k=tan(角度);
如果(角度=pi | |角度=0)
Ex=W;
Ey=Sy;
Sx=1;
elseif(角度==pi/2)
Ey=1;
i=(Sx-1)*H+[Ey:Sy];
返回;
elseif k>0&k<(Sy-1)/(W-Sx)
Ex=W;
Ey=圆形(Sy tan(角度)*(Ex Sx));
elseif k<0和abs(k)<(Sy-1)/(Sx-1)
Ex=1;
Ey=圆形(Sy tan(角度)*(Ex Sx));
其他的
Ey=1;
Ex=圆形((Sy-1)/tan(角)+Sx);
结束
Dx=Ex-Sx;
Dy=Ey-Sy;
iCoords=1;
如果(abs(Dy)=Sx)
D=2*Dy+Dx;
英寸=2*Dy;
IncD=2*(Dy+Dx);
X=Sx;
Y=Sy;
i(icoods)=(Sx-1)*H+Sy;
icoods=icoods+1;
while(X=0)
D=D+英寸;
X=X+1;
其他的
D=D+IncD;
X=X+1;
Y=Y-1;
结束
i(iCoords)=(X-1)*H+Y;
icoods=icoods+1;
结束
其他的
D=-2*Dy+Dx;
英寸=-2*Dy;
IncD=2*(-Dy+Dx);
X=Sx;
Y=Sy;
i(icoods)=(Sx-1)*H+Sy;
icoods=icoods+1;
while(X>Ex)
if(D=Sx)
D=2*Dy+Dx;
英寸=2*Dy;
IncD=2*(Dy+Dx);
X=Sx;
Y=Sy;
i(icoods)=(Sy-1)*H+Sx;
icoods=icoods+1;
while(X=0)
D=D+英寸;
X=X+1;
其他的
D=D+IncD;
X=X+1;
Y=Y-1;
结束
i(iCoords)=(Y-1)*H+X;
icoods=icoods+1;
结束
其他的
D=-2*Dy+Dx;
英寸=-2*Dy;
IncD=2*(-Dy+Dx);
X=Sx;
Y=Sy;
i(icoods)=(Sy-1)*H+Sx;
icoods=icoods+1;
while(X>Ex)
如果(D)
消失点在无穷远处,因此到摄影机的距离没有用处
使用xlswrite
或dlmwrite
分别写入excel或文本文件
拍照后,消失点不是无穷大。它在图像中的一个点相交。你可以找到X和Y坐标,但Z坐标呢?@CamelUno消失点的三维齐次坐标是(X,Y,Z,0)这意味着点在无穷远处。它是平行线从无穷远处的投影。所以基本上你可以找到它们相对于两个平面的下落,并定义一条线作为图像,因为消失点随着相机的运动而变化。这是可以确定的。@CamelUno我想你有点混淆了消失点的2D和3D含义。因此,消失点被定义为平行3D线相交的点,仅在无穷远处,否则这些线不平行。但是,如果将整个设置投影到2D,则会得到这些线相交的2D点,该点可能是有限的。您引用的论文仅使用2D pr消失点的投影。但是2D点没有深度,因此无法计算到相机的距离。denahiro是对的。当谈到在图像中定位某个对象时,你谈论的是2D坐标,而到相机的距离没有太多意义。你可能想重新回答你的问题。听起来你只是想nt消失点的x,y坐标,这是一个非常合理的问题。但是你的问题标题没有多大意义。作为旁注,你的相机校准了吗?如果是这样,找到消失点是非常容易的。
clear all; close all;
dname = 'Height';
files = dir(dname);
files(1) = [];
files(1) = [];
for i=1:size(files, 1)
original = imread(fullfile(dname, files(i).name));
original = imresize(original,0.35);
im = im2double(rgb2gray(original));
[row, col] = findVanishingPoint(im);
imshow(original);hold;plot(col,row,'rx');
saveas(gcf,strcat('Height_Result',num2str(i)),'jpg');
close
end
function [row, col] = findVanishingPoint(im)
DEBUG = 0;
IM = fft2(im);
ROWS = size(IM,1); COLS = size(IM,2);
PERIOD = 2^floor(log2(COLS)-5)+2;
SIZE = floor(10*PERIOD/pi);
SIGMA = SIZE/9;
NORIENT = 72;
E = 8;
[C, S] = createGaborBank(SIZE, PERIOD, SIGMA, NORIENT, ROWS, COLS, E);
D = ones(ROWS, COLS);
AMAX = ifftshift(real(ifft2(C{1}.*IM)).^2+real(ifft2(S{1}.*IM))).^2;
for n=2:NORIENT
A = ifftshift(real(ifft2(C{n}.*IM)).^2+real(ifft2(S{n}.*IM))).^2;
D(find(A > AMAX)) = n;
AMAX = max(A, AMAX);
if (DEBUG==1)
colormap('hot');subplot(131);imagesc(real(A));subplot(132);imagesc(real(AMAX));colorbar;
subplot(133);imagesc(D);
pause
end
end
if (DEBUG==2)
figure('DoubleBuffer','on');
end
T = mean(AMAX(:))-3*std(AMAX(:));
VOTE = zeros(ROWS, COLS);
for row=round(1+SIZE/2):round(ROWS-SIZE/2)
for col=round(1+SIZE/2):round(COLS-SIZE/2)
if (AMAX(row,col) > T)
indices = lineBresenham(ROWS, COLS, col, row, D(row, col)*pi/NORIENT-pi/2);
VOTE(indices) = VOTE(indices)+AMAX(row,col);
end
end
if (DEBUG==2)
colormap('hot');imagesc(VOTE);pause;
end
end
if (DEBUG==2)
close
end
M=1;
[b index] = sort(-VOTE(:));
col = floor((index(1:M)-1) / ROWS)+1;
row = mod(index(1:M)-1, ROWS)+1;
col = round(mean(col));
row = round(mean(row));
function [C, S] = createGaborBank(SIZE, PERIOD, SIGMA, NORIENT, ROWS, COLS, E)
if (length(NORIENT)==1)
orientations=[1:NORIENT];
else
orientations = NORIENT;
NORIENT = max(orientations);
end
for n=orientations
[C{n}, S{n}] = gabormask(SIZE, SIGMA, PERIOD, n*pi/NORIENT);
C{n} = fft2(padWithZeros(C{n}, ROWS, COLS));
S{n} = fft2(padWithZeros(S{n}, ROWS, COLS));
end
function [cmask, smask] = gabormask(Size, sigma, period, orient, E)
if nargin < 5; E = 8; end;
if nargin < 4; orient = 0; end;
if nargin < 3; period = []; end;
if nargin < 2; sigma = []; end;
if nargin < 1; Size = []; end;
if isempty(period) & isempty(sigma); sigma = 5; end;
if isempty(period); period = sigma*2*sqrt(2); end;
if isempty(sigma); sigma = period/(2*sqrt(2)); end;
if isempty(Size); Size = 2*round(2.575*sigma) + 1; end;
if length(Size) == 1
sx = Size-1; sy = sx;
elseif all(size(Size) == [1 2])
sy = Size(1)-1; sx = Size(2)-1;
else
error('Size must be scalar or 1-by-2 vector');
end;
hy = sy/2; hx = sx/2;
[x, y] = meshgrid(-hx:sx-hx, -hy:sy-hy);
omega = 2*pi/period;
cs = omega * cos(orient);
sn = omega * sin(orient);
k = -1/(E*sigma*sigma);
g = exp(k * (E*x.*x + y.*y));
xp = x * cs + y * sn;
cx = cos(xp);
cmask = g .* cx;
sx = sin(xp);
smask = g .* sx;
cmask = cmask - mean(cmask(:));
cmask = cmask/sum(abs(cmask(:)));
smask = smask - mean(smask(:));
smask = smask/sum(abs(smask(:)));
function out = padWithZeros(in, ROWS, COLS)
out = padarray(in,[floor((ROWS-size(in,1))/2) floor((COLS-size(in,2))/2)],0,'both');
if size(out,1) == ROWS-1
out = padarray(out,[1 0],0,'pre');
end
if size(out,2) == COLS-1
out = padarray(out,[0 1],0,'pre');
end
function row = findHorizon(im)
DEBUG = 2;
ROWS = size(im,1); COLS = size(im,2);
e = edge(im,'sobel', [], 'horizontal');
dd = sum(e, 2);
N=3;
row = 1;
M = 0;
for i=1+N:length(dd)-N
m = sum(dd(i-N:i+N));
if (m > M)
M = m;
row = i;
end
end
imshow(e);pause
function row = findHorizon(im)
DEBUG = 2;
IM = fft2(im);
ROWS = size(IM,1); COLS = size(IM,2);
PERIOD = 2^floor(log2(COLS)-5)+2;
SIZE = floor(10*PERIOD/pi);
SIGMA = SIZE/9;
NORIENT = 72;
E = 16;
orientations = [NORIENT/2-10:NORIENT/2+10];
[C, S] = createGaborBank(SIZE, PERIOD, SIGMA, orientations, ROWS, COLS, E);
ASUM = zeros(ROWS, COLS);
for n=orientations
A = ifftshift(real(ifft2(C{n}.*IM)).^2+real(ifft2(S{n}.*IM))).^2;
ASUM = ASUM + A;
if (DEBUG==1)
colormap('hot');subplot(131);imagesc(real(A));subplot(132);imagesc(real(AMAX));colorbar;
pause
end
end
ASUM(1:round(1+SIZE/2), :)=0; ASUM(end-round(SIZE/2):end, :)=0;
ASUM(:,end-round(SIZE/2):end)=0; ASUM(:, 1:1+round(SIZE/2))=0;
dd = sum(ASUM, 2);
[temp, row] = sort(-dd);
row = round(mean(row(1:10)));
if (DEBUG == 2)
imagesc(ASUM);hold on;line([1:COLS],repmat(row,COLS));
pause
end
function v = lineimage(x0, y0, angle, s)
if (abs(tan(angle)) > 1e015)
a(1,:) = repmat(x0,s(1),1)';
a(2,:) = [1:s(1)];
elseif (abs(tan(angle)) < 1e-015)
a(2,:) = repmat(y0,s(2),1)';
a(1,:) = [1:s(2)];
else
k = tan(angle);
hiX = round((1-(s(1)-y0+1)+k*x0)/k);
loX = round((s(1)-(s(1)-y0+1)+k*x0)/k);
temp = max(loX, hiX);
loX = max(min(loX, hiX), 1);
hiX = min(s(2),temp);
a(1,:) = [loX:hiX];
a(2,:) = max(1, floor(s(1)-(k*a(1,:)+(s(1)-y0+1)-k*x0)));
end
v = (a(1,:)-1).*s(1)+a(2,:);
function [abscissa, ordinate] = linevector(x0, y0, angle, s)
if (rad2deg(angle) == 90)
abscissa = repmat(x0,s(1),1);
ordinate = [1:s(1)];
else
k = tan(angle);
hiX = round((1-(s(1)-y0+1)+k*x0)/k);
loX = round((s(1)-(s(1)-y0+1)+k*x0)/k);
temp = max(loX, hiX);
loX = max(min(loX, hiX), 1);
hiX = min(s(2),temp);
abscissa = [loX:hiX];
ordinate = k*abscissa+((s(1)-y0+1)-k*x0);
end
function [i] = lineBresenham(H,W,Sx,Sy,angle)
k = tan(angle);
if (angle == pi || angle == 0)
Ex = W;
Ey = Sy;
Sx = 1;
elseif (angle == pi/2)
Ey = 1;
i = (Sx-1)*H+[Ey:Sy];
return;
elseif k>0 & k < (Sy-1)/(W-Sx)
Ex = W;
Ey = round(Sy-tan(angle)*(Ex-Sx));
elseif k < 0 & abs(k) < (Sy-1)/(Sx-1)
Ex = 1;
Ey = round(Sy-tan(angle)*(Ex-Sx));
else
Ey = 1;
Ex = round((Sy-1)/tan(angle)+Sx);
end
Dx = Ex - Sx;
Dy = Ey - Sy;
iCoords=1;
if(abs(Dy) <= abs(Dx))
if(Ex >= Sx)
D = 2*Dy + Dx;
IncH = 2*Dy;
IncD = 2*(Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sx-1)*H+Sy;
iCoords = iCoords + 1;
while(X < Ex)
if(D >= 0)
D = D + IncH;
X = X + 1;
else
D = D + IncD;
X = X + 1;
Y = Y - 1;
end
i(iCoords) = (X-1)*H+Y;
iCoords = iCoords + 1;
end
else
D = -2*Dy + Dx;
IncH = -2*Dy;
IncD = 2*(-Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sx-1)*H+Sy;
iCoords = iCoords + 1;
while(X > Ex)
if(D <= 0)
D = D + IncH;
X = X - 1;
else
D = D + IncD;
X = X - 1;
Y = Y - 1;
end
i(iCoords) = (X-1)*H+Y;
iCoords = iCoords + 1;
end
end
else
Tmp = Ex;
Ex = Ey;
Ey = Tmp;
Tmp = Sx;
Sx = Sy;
Sy = Tmp;
Dx = Ex - Sx;
Dy = Ey - Sy;
if(Ex >= Sx)
D = 2*Dy + Dx;
IncH = 2*Dy;
IncD = 2*(Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sy-1)*H+Sx;
iCoords = iCoords + 1;
while(X < Ex)
if(D >= 0)
D = D + IncH;
X = X + 1;
else
D = D + IncD;
X = X + 1;
Y = Y - 1;
end
i(iCoords) = (Y-1)*H+X;
iCoords = iCoords + 1;
end
else
D = -2*Dy + Dx;
IncH = -2*Dy;
IncD = 2*(-Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sy-1)*H+Sx;
iCoords = iCoords + 1;
while(X > Ex)
if(D <= 0)
D = D + IncH;
X = X - 1;
else
D = D + IncD;
X = X - 1;
Y = Y - 1;
end
i(iCoords) = (Y-1)*H+X;
iCoords = iCoords + 1;
end
end
end