如何在分支点和终点之间绘制中点



这是我的图像 我已经找到了端点和分支点,但我想在这两点之间绘制一个点,因为我会帮助我在点之间添加边。 请给我一些代码来找到两点之间的中点或质心。

clc;
clear all;
% read in a sample image -- also see letters.png, bagel.png
J=imread('ds2.jpg');
% Normalize and Binarization
b = imresize(J,[100,100]);
th = graythresh(b);
BW1 = im2bw(b, th);
figure;
imshowpair(b, BW1, 'montage');
% the standard skeletonization:
 skelimg = bwmorph(~BW1,'thin',inf);
 mn = bwmorph(skelimg,'branchpoints');
 [row, column] = find(mn);
 branchpts = [row column];

 Endimg = bwmorph(skelimg,'endpoints');
 [row,column] = find(Endimg);
 Endpts = [row column];
 figure;imshow(skelimg);
 hold on;
 plot(branchpts(:,2),branchpts(:,1),'g*');
 plot(Endpts(:,2),Endpts(:,1),'g*');
 hold on;

参考图像

bwdistgeodesic(有用的链接(可以帮助您。 你可以做这样的事情:

clc;
clear all;
% read in a sample image -- also see letters.png, bagel.png
J=im2double(imread('circles.png'));
% Normalize and Binarization
b = imresize(J,[100,100]);
th = graythresh(b);
BW1 = im2bw(b, th);
figure;
imshowpair(b, BW1, 'montage');
% the standard skeletonization:
skelimg = bwmorph(BW1,'thin',inf);
mn = bwmorph(skelimg,'branchpoints');
[row, column] = find(mn);
branchpts = [row column];

Endimg = bwmorph(skelimg,'endpoints');
[row,column] = find(Endimg);
Endpts = [row column];
n = size(Endpts,1);
Cntrpts = zeros(n,2);
for ii = 1:n
    % compute end & branch points geodesic distance transform
    dEnd = bwdistgeodesic(skelimg, Endpts(ii,2), Endpts(ii,1), 'quasi-euclidean');
    [~,closestBranchIdx] = min(dEnd(mn));
    dStart = bwdistgeodesic(skelimg, branchpts(closestBranchIdx,2), branchpts(closestBranchIdx,1), 'quasi-euclidean');
    D = dStart + dEnd;
    D = round(D * 8) / 8;
    D(isnan(D)) = inf;
    paths = imregionalmin(D);
    % compute geodesic distance on found path from end point and divide max distance by 2 for center point
    dCenter = bwdistgeodesic(paths, Endpts(ii,2), Endpts(ii,1), 'quasi-euclidean');
    dCenter(isinf(dCenter)) = nan;
    c = nanmax(dCenter(:)) / 2;
    [~,idx] = nanmin(abs(dCenter(:) - c));
    [yc,xc] = ind2sub(size(dCenter),idx);
    Cntrpts(ii,:) = [yc,xc];
end
figure;imshow(skelimg);
hold on;
plot(Cntrpts(:,2),Cntrpts(:,1),'ro')
plot(branchpts(:,2),branchpts(:,1),'g.');
plot(Endpts(:,2),Endpts(:,1),'b.');
hold on;
disp(B)

编辑 - 所有检测到的点之间的中心:

Allpts = [Endpts;branchpts]; % all possible points
n = size(Allpts,1);
Cntrpts = nan(n^2,2);
for ii = 1:n
    for jj = [1:(ii-1) (ii+1):n]            
        % distance from start & end points
        dEnd = bwdistgeodesic(skelimg, Allpts(ii,2), Allpts(ii,1), 'quasi-euclidean');
        dStart = bwdistgeodesic(skelimg, Allpts(jj,2), Allpts(jj,1), 'quasi-euclidean');
        D = dStart + dEnd;
        D = round(D * 8) / 8;
        D(isnan(D)) = inf;
        if all(isinf(D)) % seed points not connected
            Cntrpts(ii,:) = [nan nan];
        end
        % distance of center point (just half the distance)
        paths = imregionalmin(D);
        dCenter = bwdistgeodesic(paths, Allpts(ii,2), Allpts(ii,1), 'quasi-euclidean');
        dCenter(isinf(dCenter)) = nan;
        c = nanmax(dCenter(:)) / 2;
        [~,idx] = nanmin(abs(dCenter(:) - c));
        [yc,xc] = ind2sub(size(dCenter),idx);
        Cntrpts((ii-1)*n + jj,:) = [yc,xc];
    end
end
figure;imshow(skelimg);
hold on;
plot(Cntrpts(:,2),Cntrpts(:,1),'r.')
plot(branchpts(:,2),branchpts(:,1),'g.');
plot(Endpts(:,2),Endpts(:,1),'b.');

最新更新