how to find the start and end points of overlapping intervals in one array

7 ビュー (過去 30 日間)
Amani
Amani 2014 年 10 月 27 日
コメント済み: Amani 2014 年 11 月 2 日
after extracting the connected components from an image and find the start and end points of each connected components that are in order I want to merge these connected components if they are overlapped so I need to find the start and end points
for examples a is the extreme points of each connected components
a = [ 1.5000 95.5000; 82.5000 150.5000; 141.5000 197.5000; 191.5000 357.5000; 400.5000 438.5000; 415.5000 481.5000; 519.5000 667.5000];
and I need the result to be
1.5000 357.5000 400.5000 481.5000 519.5000 667.5000
b=arrayfun(@(x) a(x,1):a(x,2),1:size(a,1),'un',0); k=1; while k<numel(b) c=b(k+1:end); idx=cellfun(@(x) any(ismember(x,b{k})),c); if any(idx) b(logical([ zeros(1,numel(b)-numel(idx)-1) 1 idx]))=[]; else b(logical([ zeros(1,numel(b)-numel(idx)) idx]))=[]; end k=k+1; end out=cell2mat(cellfun(@(x) [x(1) x(end)],b','un',0))
but I got this overlapped points
141.5000 197.5000
191.5000 357.5000
519.5000 667.5000
how can I fix it ?
thanks
  1 件のコメント
Amani
Amani 2014 年 10 月 27 日
and I need the result to be
1.5000 357.5000
400.5000 481.5000
519.5000 667.5000

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 10 月 27 日
編集済み: Andrei Bobrov 2014 年 10 月 30 日
a = [ 1.5000 95.5000; 82.5000 150.5000; 141.5000 197.5000; 191.5000 357.5000; 400.5000 438.5000; 415.5000 481.5000; 519.5000 667.5000];
b = reshape(a',1,[]);
ii = strfind(sign(diff(b)),[-1 1]);
out = b;
out([ii,ii+1]) = [];
add
b = reshape(a',1,[]);
x = all(tril(bsxfun(@lt,b(:),b(:)'))==0,2);
i0 = strfind(x(:)',[1 0]);
x(i0) = false;
out = b(x);
add2
a = [ 4.5000 58.5000
4.5000 58.5000
65.5000 118.5000
65.5000 118.5000
140.5000 277.5000
140.5000 277.5000
206.5000 223.5000
206.5000 223.5000
284.5000 295.5000
284.5000 295.5000
360.5000 422.5000
360.5000 422.5000
449.5000 454.5000
449.5000 454.5000
449.5000 532.5000
449.5000 532.5000
532.5000 574.5000
532.5000 574.5000]
a1 = unique(a,'rows');
out = a1(1,:);
for jj = 2:size(a1,1)
if out(end,2) > a1(jj,1) && a1(jj,2) > out(end,2)
out(end,:) = [out(end,1) a1(jj,2)];
elseif out(end,2) <= a1(jj,1)
out(end + 1,:) = a1(jj,:);
end
end
or
a1 = unique(a,'rows');
for jj = 2:size(a1,1)
if a1(jj-1,2) > a1(jj,1) && a1(jj,2) > a1(jj-1,2)
a1(jj,1) = 0 ;
a1(jj-1,2) = 0;
elseif a1(jj-1,2) >= a1(jj,2)
a1(jj,:) = 0;
end
end
out = a1';
out = reshape(out(out>0),2,[])';
  7 件のコメント
Andrei Bobrov
Andrei Bobrov 2014 年 10 月 30 日
new code after 'add2'
Amani
Amani 2014 年 11 月 2 日
thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by