使用Hough变换进行椭圆检测

时间:2011-06-10 13:49:39

标签: algorithm matlab hough-transform

使用Hough变换,如何在2D空间中检测并获取椭圆的(x0,y0)和“a”和“b”的坐标?

这是ellipse01.bmp:

ellipse image

I = imread('ellipse01.bmp');
[m n] = size(I);
c=0;
for i=1:m
    for j=1:n
        if I(i,j)==1
        c=c+1;
        p(c,1)=i;
        p(c,2)=j;
        end
    end
end
Edges=transpose(p);
Size_Ellipse = size(Edges);
B = 1:ceil(Size_Ellipse(1)/2);
Acc = zeros(length(B),1);
a1=0;a2=0;b1=0;b2=0;
Ellipse_Minor=[];Ellipse_Major=[];Ellipse_X0 = [];Ellipse_Y0 = [];
Global_Threshold = ceil(Size_Ellipse(2)/6);%Used for Major Axis Comparison
Local_Threshold = ceil(Size_Ellipse(1)/25);%Used for Minor Axis Comparison
[Y,X]=find(Edges);
Limit=numel(Y);
Thresh = 150;
Para=[];

for Count_01 =1:(Limit-1)
  for Count_02 =(Count_01+1):Limit
    if ((Count_02>Limit) || (Count_01>Limit))
      continue
    end
    a1=Y(Count_01);b1=X(Count_01);
    a2=Y(Count_02);b2=X(Count_02);
    Dist_01 = (sqrt((a1-a2)^2+(b1-b2)^2));
    if (Dist_01 >Global_Threshold)
      Center_X0 = (b1+b2)/2;Center_Y0 = (a1+a2)/2;
      Major = Dist_01/2.0;Alpha = atan((a2-a1)/(b2-b1));
      if(Alpha == 0)
        for Count_03 = 1:Limit
          if( (Count_03 ~= Count_01) || (Count_03 ~= Count_02))
            a3=Y(Count_03);b3=X(Count_03);
            Dist_02 = (sqrt((a3 - Center_Y0)^2+(b3 - Center_X0)^2));
            if(Dist_02 > Local_Threshold)
              Cos_Tau = ((Major)^2 + (Dist_02)^2 - (a3-a2)^2 - (b3-b2)^2)/(2*Major*Dist_02);
              Sin_Tau = 1 - (Cos_Tau)^2;
              Minor_Temp = ((Major*Dist_02*Sin_Tau)^2)/(Major^2 - ((Dist_02*Cos_Tau)^2));
              if((Minor_Temp>1) && (Minor_Temp<B(end)))
                Acc(round(Minor_Temp)) = Acc(round(Minor_Temp))+1;
              end
            end
          end
        end
      end
      Minor = find(Acc == max(Acc(:)));
      if(Acc(Minor)>Thresh)
        Ellipse_Minor(end+1)=Minor(1);Ellipse_Major(end+1)=Major;
        Ellipse_X0(end+1) = Center_X0;Ellipse_Y0(end+1) = Center_Y0;
        for Count = 1:numel(X)
          Para_X = ((X(Count)-Ellipse_X0(end))^2)/(Ellipse_Major(end)^2);
          Para_Y = ((Y(Count)-Ellipse_Y0(end))^2)/(Ellipse_Minor(end)^2);
          if (((Para_X + Para_Y)>=-2)&&((Para_X + Para_Y)<=2))
            Edges(X(Count),Y(Count))=0;
          end
        end
      end
      Acc = zeros(size(Acc));
    end
  end
end

4 个答案:

答案 0 :(得分:2)

如果使用圆进行粗糙变换,则给出rho = x cos(theta)+ y sin(theta) 对于椭圆,因为它是enter image description here

您可以将等式转换为  rho = a x cos(theta)+ b y sin(theta) 虽然我不确定你是否使用标准的Hough变换,但对于类似椭圆的变换,你可以操纵第一个给定的函数。

答案 1 :(得分:1)

虽然这是一个老问题,但也许我发现可以帮助某人。

使用普通Hough变换检测省略号的主要问题是累加器的维数,因为我们需要为5个变量投票(方程式解释here):

ellipse equation

有一个非常好的algorithm,其中累加器可以是一个简单的一维数组,例如,它在O3中运行。如果您想查看代码,可以查看here(用于测试的图像是上面发布的图像)。

答案 2 :(得分:1)

如果您的椭圆是提供的,则是一个真正的椭圆,而不是一个嘈杂的点样本; 搜索两个最远点给出了主轴的末端, 搜索两个最近点给出了短轴的末端, 这些线的交点(你可以检查它是一个直角)发生在中心。

答案 3 :(得分:0)

如果您知道椭圆的'a'和'b',那么您可以在一个方向上以a / b的系数重新缩放图像并查找圆。我还在考虑当a和b未知时该怎么做。

如果您知道它是圆形,那么对圆圈使用Hough变换。以下是示例代码:

int accomulatorResolution  = 1;  // for each pixel
    int minDistBetweenCircles  = 10; // In pixels
    int cannyThresh            = 20;
    int accomulatorThresh      = 5*_accT+1;
    int minCircleRadius        = 0;
    int maxCircleRadius        = _maxR*10;
    cvClearMemStorage(storage);
    circles = cvHoughCircles( gryImage, storage,
                              CV_HOUGH_GRADIENT, accomulatorResolution, 
                              minDistBetweenCircles,
                              cannyThresh , accomulatorThresh,
                              minCircleRadius,maxCircleRadius );    
    // Draw circles
    for (int i = 0; i < circles->total; i++){
        float* p = (float*)cvGetSeqElem(circles,i);
        // Draw center
        cvCircle(dstImage, cvPoint(cvRound(p[0]),cvRound(p[1])),
                           1, CV_RGB(0,255,0), -1, 8, 0 );
        // Draw circle
        cvCircle(dstImage, cvPoint(cvRound(p[0]),cvRound(p[1])),
                           cvRound(p[2]),CV_RGB(255,0,0), 1, 8, 0 );
    }