Ahm*_*mad 5 matlab image-processing computer-vision edge-detection hough-transform
我试图检测图像中的弯曲输送机.我使用以下代码使用Hough变换来检测其边缘
%# load image, and process it
I = imread('ggp\2.jpg');
g = rgb2gray(I);
bw = edge(g,'Canny');
[H,T,R] = hough(bw);
P = houghpeaks(H,500,'threshold',ceil(0.4*max(H(:))));
% I apply houghlines on the grayscale picture, otherwise it doesn't detect
% the straight lines shown in the picture
lines = houghlines(g,T,R,P,'FillGap',5,'MinLength',50);
figure, imshow(g), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
deltaY = xy(2,2) - xy(1,2);
deltaX = xy(2,1) - xy(1,1);
angle = atan2(deltaY, deltaX) * 180 / pi;
if (angle == 0)
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');
end
end
Run Code Online (Sandbox Code Playgroud)
如图所示,两条直线成功检测到输送机的顶部和底部边缘,但我不知道如何检测它是否弯曲(图中是否弯曲)以及如何计算其程度.
曲线大致是在下图中手动绘制的(红色):
我在matlab中找不到用于Hough变换的代码或函数来检测这样的平滑曲线(例如,二次多项式:) y= a*x^2.任何其他解决方案也欢迎.
查看您检测到传送带的直线,我假设您可以将处理重点放在感兴趣的区域(图像中的行750至950)。
从这一点开始:
oimg = imread('http://i.stack.imgur.com/xfXUS.jpg'); %// read the image
gimg = im2double( rgb2gray( oimg( 751:950, :, : ) ) ); %// convert to gray, only the relevant part
fimg = imfilter(gimg, [ones(7,50);zeros(1,50);-ones(7,50)] ); %// find horizontal edge
Run Code Online (Sandbox Code Playgroud)
仅选择区域中心周围的强水平边缘像素
[row, col] = find(abs(fimg)>50);
sel = row>50 & row < 150 & col > 750 & col < 3250;
row=row(sel);
col=col(sel);
Run Code Online (Sandbox Code Playgroud)
将二阶多项式和一条直线拟合到这些边缘点
[P, S, mu] = polyfit(col,row,2);
[L, lS, lmu] = polyfit(col, row, 1);
Run Code Online (Sandbox Code Playgroud)
绘制估计曲线
xx=1:4000;
figure;imshow(oimg,'border','tight');
hold on;
plot(xx,polyval(P,xx,[],mu)+750,'LineWidth',1.5,'Color','r');
plot(xx,polyval(L,xx,[],lmu)+750,':g', 'LineWidth', 1.5);
Run Code Online (Sandbox Code Playgroud)
结果是
您可以从视觉上欣赏到2度贴合度如何P更好地适合传送带的边界。看第一个系数
>> P(1)
ans =
1.4574
Run Code Online (Sandbox Code Playgroud)
您会看到x^2曲线的系数不可忽略,这使得曲线显然不是直线。