连接点和最小值

时间:2017-05-11 08:17:51

标签: matlab

我想知道我如何能够采用每个D(Points)并查看其连接点(在8个连接点中),但仅限于极限侧(即对角线点的顶部和右下角以及同一条线向右,即3点连接8)并选择具有最小值D的连接点的坐标。我想重复这一点,直到我得到D的最小值等于0

    % Creation of matrix example
    c=zeros(500,500);
    c(1:100,250)=1;c(100:300,200)=1;c(300:400,270)=1;  c(400:500,250)=1;
    c(100,200:250)=1;c(300,200:270)=1;c(400,250:270)=1;

     figure, imagesc(c)


    Points= [211,388;64,200;160,437;237,478;110,270;100,34];
    hold on, plot(Points(:,1),Points(:,2),'ro'), hold off

    %Distance map
    D = bwdist(cumsum(c, 2) > 0, 'euclidean');
    figure, imagesc(D)

1 个答案:

答案 0 :(得分:1)

这里的关键功能是sub2ind,它将下标转换为线性索引。当你需要处理数组中的特定点时,它非常方便。

% Let's prepare the 8 shifts needed (i add a no-move shift in first place to simplify the algorithm)

delta_x = [0, -1, -1, -1, 0, 0, 1, 1, 1];
delta_y = [0, -1, 0, 1, -1, 1, -1, 0, 1];

sz_D = size(D);
n_points = size(Points, 1);
is_running = true;

while is_running
    % All the shift combinaisons
    new_ind_x = bsxfun(@plus, Points(:,1), delta_x);
    new_ind_y = bsxfun(@plus, Points(:,2), delta_y);

    % Saturation to stay in the image
    new_ind_x = min(max(new_ind_x, 1), sz_D(2));
    new_ind_y = min(max(new_ind_y, 1), sz_D(1));

    % Get the values in D and find the index of the minimum points
    points_lin_ind = sub2ind(sz_D, new_ind_y, new_ind_x);
    points_val = D(points_lin_ind);
    [min_values, min_ind] = min(points_val, [], 2);

    % Select subscripts in new_ind_x and new_ind_y
    min_lin_ind = sub2ind([n_points, 9], (1:n_points).', min_ind);
    Points = [new_ind_x(min_lin_ind), new_ind_y(min_lin_ind)];

    % Exit condition
    if all(min_values == 0)
        is_running = false;
    end
end

PS:未经测试。