📄 findlines.m
字号:
%Implementation of a line finding algorithm based on a Houges
%transformation made by radon(). The algorithm valutates the lines based on
%their clearness. A Line inside a chaotic part of the image gets less
%points than a line alone. Author: Jan Hoeft (Jan_Hoeft@gmx.de)
function [Lines,countdetect]=findlines(I,isroom,debugout)
if(nargin<3)
debugout=0;
end
if(nargin<2)
isroom=true;
end
%%%%config
do_takethebest=1;
%range of maxima filter
relp=50;
relteta=50;
% for interior scenes 1/6, for exterior scenes 1/15
if(isroom)
thresh=size(I,2)/6;
else
thresh=size(I,2)/15;
end
thetastep=1; % 1 degree
% find the edges
tic
BW = edge(I,'canny');%,[0.1,0.2]);
% show the image
if(debugout>1)
hold off;
imshow(BW);
hold on;
end
% do the radon
theta=0:thetastep:179;
[Accumulator,xp] = radon(BW,theta);
s=size(Accumulator);
toc
tic
% Find local maxima in Accumulator
Accumulatortemp=Accumulator;
Accumulatortemp(Accumulatortemp<thresh)=0;
AccumulatorbinaryMax = imregionalmax(Accumulatortemp);
[Potential_p Potential_teta] = find(AccumulatorbinaryMax == 1);
Potential_count=Accumulator(sub2ind(s,Potential_p,Potential_teta));
%%%%begin filter the results...
%sort the possible vals by count
[dummy,IX]=sort(Potential_count);
IX=flipdim(IX,1);
Potential_count=Potential_count(IX);
Potential_p=Potential_p(IX);
Potential_teta=Potential_teta(IX);
%the regions
regp=[-fix(s(1)/relp),+fix(s(1)/relp)];
regt=[-fix(s(2)/relteta),+fix(s(2)/relteta)];
i=1;
while i<=length(Potential_p) %foreach region
% select all lines in the region
% so complicated because Acc(20,-5)=Acc(s(1)-20,s(2)-5)
% and Acc(20,s(2)+5)=Acc(s(1)-20,5)
regionp=regp+Potential_p(i);
regiont=regt+Potential_teta(i);
phits = Potential_p>regionp(1) & Potential_p<regionp(2);
thits = Potential_teta>regiont(1) & Potential_teta<regiont(2);
hits = phits & thits;
if(regiont(1)<1)
phits2 = Potential_p>(s(1)-regionp(2)+1) & Potential_p<(s(1)-regionp(1)+1);
thits2 = Potential_teta>(s(2)+regiont(1)+1);
hits2=phits2 & thits2;
hits=hits | hits2;
end
if(regiont(2)>s(2))
phits2 = Potential_p>(s(1)-regionp(2)+1) & Potential_p<(s(1)-regionp(1)+1);
thits2 = Potential_teta<regiont(2)-s(2);
hits2=phits2 & thits2;
hits=hits | hits2;
end
% indexes of the lines to delete
indexes=find(hits);
% mod the count according to its sharpness
% different versions... nr 4 is the best
switch 4
case 1
Potential_count(i)=sum(Potential_count(indexes));
case 2
tmp=[];
for y=1:length(indexes)
x=indexes(y);
prange=[-1 1]+Potential_p(x);
prange(1)=max(prange(1),1);
prange(2)=min(prange(2),s(1));
tmp=[tmp Potential_count(x)*2 ...
-sum(sum(Accumulatortemp(prange,Potential_teta(i)) ))];
end
Potential_count(i)=norm(tmp,5);
case 3
%check if line is sharp by changing the position
%get the malus range
prange=[-8 -7 -6 -5 5 6 7 8]+Potential_p(i);
%control borders of the accumulator
prange=max(prange,1);
prange=min(prange,s(1));
%take average over the malus lines
%without counting the lines outside the image
malus=sum(Accumulator(prange,Potential_teta(i))) ...
/ sum(Accumulator(prange,Potential_teta(i))~=0);
%add the unsharpness malus
Potential_count(i)=Potential_count(i) - malus;
case 4
%check if line is sharp by changing the angle
%get the malus range
trange=[-2 -1 1 2]+Potential_teta(i);
%control borders of the accumulator
trange=max(trange,1);
trange=min(trange,s(2));
%take average over the malus lines
malus=sum(Accumulator(Potential_p(i),trange)) ...
/ length(trange);
%add the unsharpness malus
Potential_count(i)=Potential_count(i) - malus;
end
% % cool debug output:
% pdetect = xp(Potential_p(indexes));
% tetadetect = theta(Potential_teta(indexes))'.*pi./180;
% Lines=[cos(tetadetect),sin(tetadetect),-pdetect];
% H=drawlines(Lines,size(I),[1 0 1],2);
% delete(H);
%is the center of the region, because we sorted the system
indexes(1)=[];
% delete
Potential_p(indexes)=[];
Potential_teta(indexes)=[];
Potential_count(indexes)=[];
i=i+1;
end %foreach region
%%%end filter
%relativize the count
CountMax = max(Potential_count);
Potential_count = Potential_count ./ CountMax;
%take only the best
if(do_takethebest)
% %take all with a count better than 25%
% IX=find(Potential_count>0.25);
%take the upper 20
[dummy,IX]=sort(Potential_count);
IX=flipdim(IX,1);
Potential_count=Potential_count(IX);
Potential_p=Potential_p(IX);
Potential_teta=Potential_teta(IX);
IX=1:min(20,size(Potential_count,1));
Potential_count=Potential_count(IX);
Potential_teta=Potential_teta(IX);
Potential_p=Potential_p(IX);
end
% get the lines
countdetect = Potential_count;
pdetect = xp(Potential_p);
tetadetect = theta(Potential_teta)'.*pi./180;
%pol2abc
Lines=[cos(tetadetect),sin(tetadetect),-pdetect];
toc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -