答案 0 :(得分:1)
您可以使用Hough Transform进行线路检测。
我的大多数帖子都基于Hough Transform文档。
示例代码:
%Read input image from imgur
I = imread('https://i.stack.imgur.com/EcHfy.png');
J = zeros(size(I,1), size(I,2));
%Select dark pixels (assume wires are dark).
J((I(:,:,1) < 80) & (I(:,:,2) < 80) & (I(:,:,3) < 80)) = 1;
%figure;imshow(J);
%Find the edges in the image using the edge function.
BW = edge(J,'canny');
%figure;imshow(BW);
%Compute the Hough transform of the binary image returned by edge.
[H,theta,rho] = hough(BW);
%Display the transform, H, returned by the hough function.
% figure;imshow(imadjust(mat2gray(H)),[], 'XData',theta, 'YData',rho, 'InitialMagnification','fit');
% xlabel('\theta (degrees)');ylabel('\rho');axis on;axis normal ;hold on;colormap(gca,hot);
%Find the peaks in the Hough transform matrix, H, using the houghpeaks function.
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
%Superimpose a plot on the image of the transform that identifies the peaks.
%x = theta(P(:,2));y = rho(P(:,1));plot(x,y,'s','color','black');
%Find lines in the image using the houghlines function.
lines = houghlines(BW,theta,rho,P,'FillGap',50,'MinLength',7);
%Create a plot that displays the original image with the lines superimposed on it.
figure, imshow(I), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
说明:
您可以使用形态操作来改善结果
修改我的代码&#34;选择暗像素(假设电线是暗的)&#34;。
更改霍夫变换参数。