【问题标题】:How to find distance between two connected points through white pixels?如何通过白色像素找到两个连接点之间的距离?
【发布时间】:2019-06-15 11:44:47
【问题描述】:

我想找到一条曲线的两个端点。

我使用凸包找到 8 个边界点,然后为每个点计算与所有其他点的距离。具有最大距离的点被假定为端点。它适用于大多数形状简单的情况,如下所示:

我正在使用这段代码找到两点:

% Calculate 8-boundary points using Convex Hull
% (points consists of all white pixels for the curve)
out=convhull(points);
extremes=points(out,:);

% Finding two farthest points among above boundary points
arr_size = size(extremes);
max_distance = 0;
endpoints = [extremes(1,:); extremes(2,:)];
for i=1:arr_size(1)
    p1 = extremes(i,:);
    for j=i+1:arr_size(1)
        p2 = extremes(j,:);
        dist = sqrt((p2(1)-p1(1))^2 + (p2(2)-p1(2))^2);
        if dist>max_distance
            max_distance = dist;
            endpoints = [p1; p2];
        end
    end
end

disp(endpoints);

在大多数情况下它都有效,但是当它应用于 U 型形状时就会出现问题。

我想找出两点之间的白色像素数。我跟着这个 Counting white pixels between 2 points in an image 但它有助于只有直线而不是曲线。任何帮助表示赞赏!

编辑 1:

我已经根据@Rotem 的回答更新了我的代码,它解决了我的问题。

更新代码:

for i=1:arr_size(1)
    p1 = extremes(i,:);
    distanceMap = calculate_distance_map(arr, p1);
    for j=i+1:arr_size(1)
        p2 = extremes(j,:);
        dist = distanceMap(p2(2), p2(1));
        if dist>max_distance
            max_distance = dist;
            endpoints = [p1; p2];
        end
    end
end

但是这需要很长时间。关于如何减少计算时间的任何建议?

【问题讨论】:

  • 我会使用广度优先搜索。
  • 我找到了一个受动态规划算法启发的解决方案

标签: matlab image-processing bresenham


【解决方案1】:

我找到了一个受dynamic programming算法启发的解决方案。

解释在 cmets 中:

function n_points = BlueDotsDist()

I = imread('BlueDots.png');

%Fix the image uploaded to the site - each dot will be a pixel.
I = imresize(imclose(I, ones(3)), 1/6, 'nearest');

%Convert each color channel to a bynary image:
R = imbinarize(I(:,:,1));G = imbinarize(I(:,:,2));B = imbinarize(I(:,:,3));
%figure;imshow(cat(3, double(R), double(G), double(B)));impixelinfo

%Find blue dost:
[blue_y, blue_x] = find((B == 1) & (R == 0));

%Compute distance map from first blue point
P = CalcDistMap(R, blue_y(1), blue_x(1));

%Compute distance map from second blue point
Q = CalcDistMap(R, blue_y(2), blue_x(2));

%Get 3x3 pixels around second blue point.
A = P(blue_y(2)-1:blue_y(2)+1, blue_x(2)-1:blue_x(2)+1);
dist_p = min(A(:)); %Minimum value is the shortest distance from first point (but not the number of white points).

T = max(P, Q); %Each element of T is maximum distance from both points.
T(T > dist_p) = 10000; %Remove points that are more far than dist_p.

%Return number of white points between blue points.
n_points = sum(T(:) < 10000);




function P = CalcDistMap(R, blue_y, blue_x)
%Each white pixel in R gets the distance from the blue dot in coordinate (blue_x, blue_y).

%Initialize array with values of 10000 (high value - marks infinite distance).
P = zeros(size(R)) + 10000; %P - distance from blue dot

P(blue_y, blue_x) = 0; %Distance from itself.

prvPsum = 0;
while (sum(P(:) < 10000) ~= prvPsum)
    prvPsum = sum(P(:) < 10000); %Sum of "marked" dots.
    for y = 2:size(R, 1)-1
        for x = 2:size(R, 2)-1
            p = P(y, x);
            if (p < 10000) %If P(y,x) is "marked"
                A = P(y-1:y+1, x-1:x+1); %3x3 neighbors.
                A = min(A, p+1); %Distance is the minimum of current distance, and distance from p pixel + 1.
                A(R(y-1:y+1, x-1:x+1) == 0) = 10000; %Ignore black pixels.
                A(P(y-1:y+1, x-1:x+1) == 0) = 0; %Restore 0 in blue point.
                P(y-1:y+1, x-1:x+1) = A; %Update distance map.
            end
        end
    end
end

距离图说明(10000替换为0):

P(距第一个蓝点的距离):

Q(到第二个蓝点的距离):

最大(P,Q):

电话:

【讨论】:

  • 我只想找到离形状最远的两个点,你的代码真的很有帮助。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 2013-02-07
相关资源
最近更新 更多