如何在matlab中检测平滑曲线 [英] How to detect smooth curves in matlab

查看:860
本文介绍了如何在matlab中检测平滑曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测图像中的弯曲输送机。我使用以下代码使用Hough变换检测其边缘

 %#load image,并处理它
I = imread( 'ggp\2.jpg');
g = rgb2gray(I);
bw = edge(g,'Canny');

[H,T,R] =霍夫(bw);

P = houghpeaks(H,500,'threshold',ceil(0.4 * max(H(:))));

%我在灰度图片上应用houghlines,否则它没有检测到
%图中所示的直线
lines = houghlines(g,T,R,P , 'FillGap',5 '的minLength',50);
数字,imshow(g),持有

为k = 1:长度(行)

xy = [lines(k).point1;线(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(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','黄色');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
结束
结束



我在matlab中找不到用于Hough变换的代码或函数来检测这样的平滑曲线(例如,2次多项式: y = a * x ^ 2 )。任何其他解决方案也欢迎。



这是原始图片:

解决方案

查看检测传送带的直线,我假设您可以将处理重点放在感兴趣的区域(图像中的行750到950)。

从那一点开始:

  oimg = imread('http://i.stack.imgur.com/xfXUS.jpg' ); %//读取图像
gimg = im2double(rgb2gray(oimg(751:950,:,:))); %//转换为灰色,只有相关部分
fimg = imfilter(gimg,[ones(7,50); zeros(1,50); - ones(7,50)]); %//找到水平边缘

仅选择区域中心周围的强水平边缘像素

  [row,col] = find(abs(fimg)> 50); 
sel = row> 50&行< 150& col> 750& col< 3250;
row = row(sel);
col = col(sel);

为这些边缘点拟合二次多项式和一条线

  [P,S,mu] = polyfit(col,row,2); 
[L,lS,lmu] = polyfit(col,row,1);

绘制估计曲线

  XX = 1:4000; 
figure; imshow(oimg,'border','tight');
持有;
plot(xx,polyval(P,xx,[],mu)+ 750,'LineWidth',1.5,'Color','r');
plot(xx,polyval(L,xx,[],lmu)+750,':g','LineWidth',1.5);

结果是





您可以直观地了解第二度适合 P 如何更好地适应传送带的边界。查看第一个系数

 >> P(1)
ans =
1.4574

你看到的系数是曲线的 x ^ 2 不可忽略,使得曲线明显不是直线。


I am trying to detect a bent conveyor in an image. I used the following code using Hough transform to detect its edges

%# 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

As it is shown, two straight lines successfully detect top and bottom edges of the conveyor but I don't know how to detect if it is bent or not (in the picture it is bent) and how to calculate the degree of that.

The curve approximately is drawn manually in the picture below (red color):

I found no code or function for Hough transform in matlab to detect such smooth curves (e.g., 2nd degree polynomials: y= a*x^2). Any other solution is also welcome.

It's the original image:

解决方案

Looking at your straight lines detecting the conveyor belt, I assume you can focus your processing around the region of interest (rows 750 to 950 in the image).
Proceeding from that point:

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

Select only strong horizontal edge pixels around the center of the region

[row, col] = find(abs(fimg)>50); 
sel = row>50 & row < 150 & col > 750 & col < 3250;
row=row(sel);
col=col(sel);

Fit a 2nd degree polynom and a line to these edge points

[P, S, mu] = polyfit(col,row,2);
[L, lS, lmu] = polyfit(col, row, 1);

Plot the estimated curves

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

The result is

You can visually appreciate how the 2nd degree fit P fits better the boundary of the conveyor belt. Looking at the first coefficient

>> P(1)
ans =
1.4574

You see that the coefficient of x^2 of the curve is not negligible making the curve distinctly not a straight line.

这篇关于如何在matlab中检测平滑曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆