Image processing Hough圆变换的python实现

Image processing Hough圆变换的python实现,image-processing,computer-vision,hough-transform,Image Processing,Computer Vision,Hough Transform,我正在实现Hough圆变换,并在只包含一个圆周长的二值图像上尝试我的代码,但是对于我尝试的任何半径,我都会得到相同数量的累积点,下面是代码: y0array, x0array= np.nonzero(image1) r=8 acc_cells = np.zeros((100,100), dtype=np.uint64) for i in range( len(x0array)): y0= y0array[i] x0= x0array[i]

我正在实现Hough圆变换,并在只包含一个圆周长的二值图像上尝试我的代码,但是对于我尝试的任何半径,我都会得到相同数量的累积点,下面是代码:

y0array, x0array= np.nonzero(image1)

r=8    
  
acc_cells = np.zeros((100,100), dtype=np.uint64)
        
for i in range( len(x0array)):
    y0= y0array[i]
    x0= x0array[i]

    for angle in range(0,360): 
        b = int(y0 - (r * s[angle]) ) //s is an array of sine of angles from 0 to 360
        a = int(x0 - (r * c[angle]) ) //c is an array of cosine of angles from 0 to 360

        if a >= 0 and a < 100 and b >= 0 and b < 100:
                    
            acc_cells[a, b] += 1
            

acc_cell_max = np.amax(acc_cells)
print(r, acc_cell_max)
y0array,x0array=np.非零(image1)
r=8
acc_单元=np.0((100100),数据类型=np.uint64)
对于范围内的i(len(x0array)):
y0=y0阵列[i]
x0=x0阵列[i]
对于范围内的角度(0360):
b=int(y0-(r*s[angle])//s是角度为0到360的正弦数组
a=int(x0-(r*c[angle])//c是从0到360角度的余弦数组
如果a>=0且a<100且b>=0且b<100:
acc_单元[a,b]+=1
acc_单元_max=np.amax(acc_单元)
打印(r、acc\U单元\U最大值)

为什么会发生这种行为?

你必须像以前那样找出圆圈的中心。你必须找到每个边的坐标

您可以在detectCircles函数中检查hough圆的python实现

另外,请看一下hough圆的matlab函数实现

功能[y0detect,x0detect,累加器]=houghcircle(最小值,r,阈值)
%HOUGHCIRCLE-在二值图像中检测具有特定半径的圆。这
%它只是一个标准的圆Hough变换的实现
%以显示此方法的工作原理。
%
%评论:
%函数使用标准Hough变换检测二值图像中的圆。
%根据圆的Hough变换,将图像空间中的每个像素
%对应于Hough空间中的圆,反之亦然。
%图像的左上角是坐标系的原点。
%
%用法:[y0detect,x0detect,累加器]=houghcircle(Imbinary,r,thresh)
%
%论据:
%Imbinary-二值图像。显示值等于1的图像像素
%HOUGHCIRCLE函数的候选像素。
%r——圆的半径。
%thresh(阈值)—一个阈值,用于确定最小阈值数目
%图像空间中属于圆的像素。阈值必须是
%大于或等于4(默认值)。
%
%返回:
%y0detect—检测到的圆的行坐标。
%x0detect-检测到的圆的列坐标。
%累加器-Hough空间中的累加器阵列。
%
%作者:
%阿明·萨拉弗雷斯
%在线计算机视觉
%       http://www.computervisiononline.com
%       amin@computervisiononline.com
%
%致谢:感谢CJ Taylor和Peter Bone提出的建设性意见
%
%2004年5月5日-原版
%2004年11月24日-改进版,更快更好的性能(由CJ Taylor建议)
%2012年8月31日-Peter Bone/Better documentation提出的实施建议
如果nargin==2
阈值=4;%将阈值设置为默认值
结束
如果thresh<4
错误('HOUGHCIRCLE::Treshold值必须大于或等于4');
结束
%投票表决
累加器=零(大小(最小));%初始化累加器
[yIndex xIndex]=查找(Imbinary);%查找边缘像素的x,y
numRow=大小(最小值,1);%二进制图像中的行数
numCol=大小(最小,2);%二进制图像中的列数
r2=r^2;%半径的平方,以防止其在循环中计算
对于cnt=1:numel(xIndex)
低=xIndex(cnt)-r;
高=xIndex(cnt)+r;
if(lownumCol)
高=numCol;
结束
对于x0=低:高
yOffset=sqrt(r2-(xIndex(cnt)-x0)^2);
y01=圆形(yIndex(cnt)-yOffset);
y02=圆形(yIndex(cnt)+yOffset);
如果y01=1
累加器(y01,x0)=累加器(y01,x0)+1;
结束
如果y02=1
累加器(y02,x0)=累加器(y02,x0)+1;
结束
结束
结束
%求累加器的局部极大值
y0detect=[];x0detect=[];
累加器binarymax=imregionalmax(累加器);
[Potential_y0 Potential_x0]=find(累加器二进制最大值==1);
累加器Temp=累加器-脱粒;
对于cnt=1:numel(电势_y0)
如果累加器temp(电势_y0(cnt),电势_x0(cnt))>=0
y0detect=[y0detect;电位_y0(cnt)];
x0detect=[x0detect;电位(cnt)];
结束
结束
function [y0detect,x0detect,Accumulator] = houghcircle(Imbinary,r,thresh)
%HOUGHCIRCLE - detects circles with specific radius in a binary image. This
%is just a standard implementaion of Hough transform for circles in order
%to show how this method works.
%
%Comments:
%       Function uses Standard Hough Transform to detect circles in a binary image.
%       According to the Hough Transform for circles, each pixel in image space
%       corresponds to a circle in Hough space and vise versa. 
%       upper left corner of image is the origin of coordinate system.
%
%Usage: [y0detect,x0detect,Accumulator] = houghcircle(Imbinary,r,thresh)
%
%Arguments:
%       Imbinary - A binary image. Image pixels with value equal to 1 are
%                  candidate pixels for HOUGHCIRCLE function.
%       r        - Radius of the circles.
%       thresh   - A threshold value that determines the minimum number of
%                  pixels that belong to a circle in image space. Threshold must be
%                  bigger than or equal to 4(default).
%
%Returns:
%       y0detect    - Row coordinates of detected circles.
%       x0detect    - Column coordinates of detected circles. 
%       Accumulator - The accumulator array in Hough space.
%
%Written by :
%       Amin Sarafraz
%       Computer Vision Online 
%       http://www.computervisiononline.com
%       amin@computervisiononline.com
%
% Acknowledgement: Thanks to CJ Taylor and Peter Bone for their constructive comments
%
%May 5,2004         - Original version
%November 24,2004   - Modified version,faster and better performance (suggested by CJ Taylor)
%Aug 31,2012        - Implemented suggestion by Peter Bone/ Better documentation 


if nargin == 2
    thresh = 4; % set threshold to default value
end

if thresh < 4
    error('HOUGHCIRCLE:: Treshold value must be bigger or equal to 4');
end

%Voting
Accumulator = zeros(size(Imbinary)); % initialize the accumulator
[yIndex xIndex] = find(Imbinary); % find x,y of edge pixels
numRow = size(Imbinary,1); % number of rows in the binary image
numCol = size(Imbinary,2); % number of columns in the binary image
r2 = r^2; % square of radius, to prevent its calculation in the loop

for cnt = 1:numel(xIndex)
    low=xIndex(cnt)-r;
    high=xIndex(cnt)+r;

    if (low<1) 
        low=1; 
    end

    if (high>numCol)
        high=numCol; 
    end

    for x0 = low:high
        yOffset = sqrt(r2-(xIndex(cnt)-x0)^2);
        y01 = round(yIndex(cnt)-yOffset);
        y02 = round(yIndex(cnt)+yOffset);

        if y01 < numRow && y01 >= 1
            Accumulator(y01,x0) = Accumulator(y01,x0)+1;
        end

        if y02 < numRow && y02 >= 1
            Accumulator(y02,x0) = Accumulator(y02,x0)+1;
        end
    end
end

% Finding local maxima in Accumulator
y0detect = []; x0detect = [];
AccumulatorbinaryMax = imregionalmax(Accumulator);
[Potential_y0 Potential_x0] = find(AccumulatorbinaryMax == 1);
Accumulatortemp = Accumulator - thresh;
for cnt = 1:numel(Potential_y0)
    if Accumulatortemp(Potential_y0(cnt),Potential_x0(cnt)) >= 0
        y0detect = [y0detect;Potential_y0(cnt)];
        x0detect = [x0detect;Potential_x0(cnt)];
    end
end