2014-01-20 3 views
2

일부 이미지 처리를하고 있는데, houghlines()에서 결과를 조작하고 수동으로 일부 라인을 연결해야합니다. 쎄타 및 (ρ)의 값이 동일한 경우, 지금구조체 배열에서 행 추출하기

lines(1) = struct('theta', 69, 'rho', 14); 
lines(2) = struct('theta', 70, 'rho', 32); 
lines(3) = struct('theta', 69, 'rho', 14); 
lines(4) = struct('theta', 69, 'rho', 20); 
lines(5) = struct('theta', 70, 'rho', 32); 

:

다음 houghlines() -result가 주어진다을 (I 분명 그것을 유지 POINT1 및 POINT2의 항목을 단축) 나중에 Houghlines를 수동으로 연결할 수 있도록 특정 행을 추출하고 싶습니다. 결과는 행 구조의 해당 행이어야합니다. 이처럼 :

A = [lines(1) lines(3)]; 
B = [lines(2) lines(5)]; 
C = [lines(4)]; 

내가 명시 적으로 위의 코드에서 같은 일을 해결할 수 없습니다, 때문에 houglines는() 비디오에 적용됩니다. 이는 theta, ,, 심지어 길이가 의 값이 모든 프레임마다 다릅니다. 이렇게 동적으로 평가해야합니다.

나는 FileExchange에서 의 nestedStruct()을 찾았습니다. 먼저 theta와 rho로 먼저 구조체를 정렬 할 수있었습니다. 이 시점에서 구조체를 분해 할 수없는 이유는 결과 구조체에 대해 얼마나 많은 요소를 가져야하는지 모르기 때문입니다.
나는 또한 unique()으로 실험했지만 색인을 생성했지만 행운은 없습니다.

누군가가 나에게 그 방법을 알려줄 수 있기를 바랍니다.
미리 감사드립니다.

답변

0

다음과 같은 코드를 생각해

% sample 1x5 structure array 
clear lines 
lines(1) = struct('theta', 69, 'rho', 14); 
lines(2) = struct('theta', 70, 'rho', 32); 
lines(3) = struct('theta', 69, 'rho', 14); 
lines(4) = struct('theta', 69, 'rho', 20); 
lines(5) = struct('theta', 70, 'rho', 32); 

% build a two-columns matrix of theta/rho values 
t = [lines.theta]; 
r = [lines.rho]; 
rows = [t(:) r(:)]; 

% find unique rows. ind will be an array of indices 
% (ranging from 1 to number of unique rows) 
[~,~,ind] = unique(rows, 'rows'); 
for i=1:max(ind) 
    % indices of structs with equal theta/rho values 
    idx = find(ind == i) 

    % extract those structs and do something useful with them 
    s = lines(idx); 
    % ... 
end 

위의 예에서를 그룹화 지수이었다 발견

idx = 
    1 
    3 
idx = 
    4 
idx = 
    2 
    5 

독특한 라인 (A에서 B, C의 세 그룹에 해당하여 문제).

+0

대단히 감사합니다. 매력처럼 작동합니다. 완전한. – BastlWastl

관련 문제