2017-03-18 95 views
0

我有一个小型项目“使用Matlab检测火车上的电线”。视频中的线路功率检测

enter image description here。我用红色圈起来的电线是我需要检测的电线。

我尝试egde检测,但不知道下一步该怎么做。任何人都可以提出任何方法来做到这一点这里是video的链接。

回答

1

您可以使用Hough Transform进行在线检测。

我的大部分文章都是基于Hough变换文档。

示例代码:

%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'); 

结果:
enter image description here

备注:
完全可以提高使用形态操作的结果。
修改我的代码“选择暗像素(假设电线很暗)”。
更改Hough变换参数。

+0

感谢Rotem,你的例子对于单帧完美工作。但是,我试图将其应用于整个[视频](https://www.dropbox.com/s/m3j4or1dxxv3fmt/Eric_Video.avi?dl=0)。我试图使用implay,但仍然无法做到。你可以帮我吗 ? – ngoduyvu

+0

您可以使用'VideoReader':[https://www.mathworks.com/help/matlab/ref/videoreader-object.html](https://www.mathworks.com/help/matlab/ref/videoreader- object.html),用于逐帧读取视频。单独为每个帧执行行检测并不是最好的方法,但这是一个好的开始。 – Rotem