How can I link the edges without using Imline ?

1 次查看(过去 30 天)
I can find the endpoints of a edge. now i want to link the broken edges. I already got a code where did something like this. but problem is that it joins the edges using line function (green line in image). so in this case it just draws the line.but i want to merge the edges so that i can apply operation on it.I mean i join the broken edges by filling '1'.
Another thing is that, i want to join only horizontal lines.But in this code it joins both horizontal & vertical endpoints whose are higher than given input gap value.In this what i do , only for horizontal edges.?
endPoints = bwmorph(canny_output, 'endpoints');
[endPointRows, endPointColumns] = find(endPoints);
numberOfEndpoints = length(endPointRows);
longestGapToClose = 15;
% Label the image. Gives each separate segment a unique ID label number.
[labeledImage, numberOfSegments] = bwlabel(edges_depth_distance);
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 = endPointRows(k);
thisColumn = endPointColumns(k);
% 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 = endPointRows(k);
thisColumn = endPointColumns(k);
% Get the label number of this segment
thisLabel = theLabels(k);
% Get indexes of the other end points.
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 = endPointColumns(otherEndpointIndexes);
otherRows = endPointRows(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;
% 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
Output of this code:
Canny Output:

回答(1 个)

Image Analyst
Image Analyst 2017-8-4
You have to use imline() to create a binary image, then OR that image in with your master binary image. See attached example.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by