我正在尝试实现霍夫变换算法来检测灰度图像中的圆圈。我运行边缘检测器并使用每个点的梯度方向来获得半径所在的线。然后,我尝试为每个圆心 (cX,cY) 和半径 r 的圆累积选票:
% run edge detection on image
Edges = edgeDetect(img);
% initialize accumulator matrix
Acc = zeros(imgRows, imgCols, maxR);
% get indices of edge points
edgePoints = find(Edges);
% get number of edg points
numOfEdges = size(edgePoints);
for currEdge = 1 : numOfEdges
% get edge cartesian coordinate
[eY eX] = ind2sub([imgRows, imgCols], edgePoints(currEdge));
% find gradient's direction at this point
theta = Directions(eY, eX);
for r = minR : maxR
% find center point according to polar representation of circle
cX = round( eX - r*cos(theta) );
cY = round( eY - r*sin(theta) );
% check if (cX,cY) is within image's boundaries
if 1 <= cX && cX <= imgCols && 1 <= cY && cY <= imgRows
% found a circle with (cX,cY) as center and r as radius
Acc(cY, cX, r) = Acc(cY, cX, r) + 1; % increment matching counter
end
end
end
但是,累加器 (Acc) 中的最大值是 10 - 所以我猜我计算圈数的方式有问题。我试过调试它,但看不出问题出在哪里......有什么想法可能是错的吗?任何帮助都感激不尽!