如何计算纤维长度

时间:2016-04-19 15:29:47

标签: computer-vision

我需要帮助计算光纤长度。我利用欧氏距离的区域最大值找到了光纤中心线的所有坐标值。这是我应用区域最大欧氏距离后得到的图像。现在我想通过使用这些点在每根光纤上绘制一条线,我该怎么做才能自动提取每根光纤长度。我尝试使用样条曲线拟合来做到这一点。但问题是我无法启动光纤的起点和终点。如何计算每根光纤长度?

fiber image after applying regional maximal

   close all;
clear all;
clc
ima=imread('ecm61.png');
ima=bwareaopen(ima,50);
[rowsInImage,columnImage]=size(ima);
skel= bwmorph(ima,'skel',Inf);
figure 
imshow(skel)
B = bwmorph(skel, 'branchpoints');
E = bwmorph(skel, 'endpoints');
[x,y] = find(E);
%plot(x,y,'+')
B_loc = find(B);
Dmask = false(size(skel));
for k = 1:numel(y)
    D = bwdistgeodesic(skel,y(k),x(k));
    distanceToBranchPt = min(D(B_loc));
    Dmask(D < distanceToBranchPt) =true;
end
skelD = skel - Dmask;

figure
imshow(skelD);
hold all;
[x,y] = find(B); plot(y,x,'ro')
numberOfEndpoints=length(y);
% Label the image.  Gives each separate segment a unique ID label number.
[labeledImage, numberOfSegments] = bwlabel(skelD);
fprintf('There are %d endpoints on %d segments.\n', numberOfEndpoints, numberOfSegments);
% Get the label numbers (segment numbers) of every endpoint.
for k = 1 : numberOfEndpoints
    thisRow = x(k);
    thisColumn = y(k);
    %line([endPointRows(k),endPointColumns(k)],[endPointRows(k+1),endPointColumns(k+1)])
    % Get the label number of this segment
    theLabels(k) = labeledImage(thisRow, thisColumn);
    fprintf('Endpoint #%d at (%d, %d) is in segment #%d.\n', k, thisRow, thisColumn, theLabels(k));
end

% For each endpoint, find the closest other endpoint
% that is not in the same segment
for k = 1 : numberOfEndpoints
    thisRow = x(k);
    thisColumn =y(k);
    % Get the label number of this segment
    thisLabel = theLabels(k);

    otherEndpointIndexes = setdiff(1:numberOfEndpoints, k);
    %if mustBeDifferent
        % If they want to consider joining only end points that reside on different segments
        % then we need to remove the end points on the same segment from the "other" list.
        % Get the label numbers of the other end points.
        %otherLabels = theLabels(otherEndpointIndexes);
        %onSameSegment = (otherLabels == thisLabel); % List of what segments are the same as this segment
        %otherEndpointIndexes(onSameSegment) = []; % Remove if on the same segment
    %end

    % Now get a list of only those end points that are on a different segment.
    otherCols = y(otherEndpointIndexes);
    otherRows = x(otherEndpointIndexes);

    % Compute distances
    distances = sqrt((thisColumn - otherCols).^2 + (thisRow - otherRows).^2);
    % Find the min
    [minDistance, indexOfMin] = min(distances);
    nearestX = otherCols(indexOfMin);
    nearestY = otherRows(indexOfMin);

    %if minDistance < longestGapToClose;
    if minDistance < rowsInImage
        % Draw line from this endpoint to the other endpoint.
        line([thisColumn, nearestX], [thisRow, nearestY], 'Color', 'g', 'LineWidth', 2);
        fprintf('Drawing line #%d, %.1f pixels long, from (%d, %d) on segment #%d to (%d, %d) on segment #%d.\n', ...
            k, minDistance, thisColumn, thisRow, theLabels(k), nearestX, nearestY, theLabels(indexOfMin));
    end
end
title('Endpoints Linked by Green Lines', 'FontSize', 12, 'Interpreter', 'None');

after using edge linking

1 个答案:

答案 0 :(得分:1)

我会这样做:

  1. 缩略
  2. Pruning
  3. 在每个十字路口找到不同的路径。它会为您提供不同的细分,您可以使用方向重新连接它们。