【问题标题】:Extract row from struct array从结构数组中提取行
【发布时间】:2014-01-20 17:25:06
【问题描述】:

我正在做一些图像处理,我必须操纵 houghlines() 的结果并手动连接一些线。

给出以下 houghlines() -result (我缩短了 point1 和 point2 的条目以保持清晰)

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);

现在,如果 theta 和 rho 的值相等,我想提取特定的行,以便以后可以手动连接 Houghlines。 结果应该是行结构的相应行。像这样:

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

我无法像上面的代码那样明确地解决问题,因为 houglines() 已应用于视频。这意味着 thetarho 的值甚至 lines 的长度对于每一帧都是不同的。 所以这必须动态评估。

我发现使用 FileExchange 中的 nestedStruct() 我可以先按 theta 然后按 rho 对我的结构进行排序。在这一点上,我无法拆卸结构,因为我不知道我必须为生成的结构采用多少元素。
我还尝试了 unique() 索引,但没有成功。

我希望有人能给我一个提示。
提前致谢。

【问题讨论】:

    标签: arrays matlab data-structures struct


    【解决方案1】:

    考虑以下代码:

    % 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
    

    对应于三组唯一行(ABC 在您的问题中)。

    【讨论】:

    • 非常感谢。奇迹般有效。完美。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多