我第一次体验 Delphi 的 OpenCV 4.10。我正在尝试 HoughCircles() 函数,但我对新的变量类型感到困惑。如何确定圆的个数、圆的半径和圆心?谢谢你。
https://docs.opencv.org/4.x/d4/d70/tutorial_hough_circle.html#autotoc_md669
https://github.com/Laex/Delphi-OpenCV-Class
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
gray.rows/16, // change this value to detect circles with different distances to each other
100, 30, 1, 30 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
带有参数:
:输入图像(灰度)。gray
:存储 3 个值的向量: 对于每个检测到的圆圈。circles
:定义检测方法。目前这是 OpenCV 中唯一可用的。HOUGH_GRADIENT
= 1: 分辨率的反比。dp
=gray.rows/16:检测到的中心之间的最小距离。min_dist
= 200:内部 Canny 边缘检测器的上限。param_1
= 100*:中心检测的阈值。param_2
= 0:要检测的最小半径。如果未知,请输入零作为默认值。min_radius
= 0:要检测的最大半径。如果未知,请输入零作为默认值。max_radius
procedure TForm1.FindCircles(const AMat: TMat);
const
Param1 = 100;
Param2 = 30;
RadiusMin = 1;
RadiusMaks = 300;
var
ImgGray: TMat; // TInputArray
Circles: Vector<TVec4i>; // TOutputArray, but need 3 values
Circle: TVec4i; //array [0..2] of integer;
CirclesCount: integer;
Radius: integer;
Center: TPoint;
i: integer;
begin
cvtColor(AMat, ImgGray, COLOR_BGR2GRAY);
MedianBlur(ImgGray, ImgGray, 3);
try
// Detect circles and store them in the Circles array
HoughCircles(ImgGray, Circles, CV_HOUGH_GRADIENT, 1, // Error
ImgGray.rows/16, Param1, Param2, RadiusMin, RadiusMaks);
if (not Circles.Empty) // ???
then begin
CirclesCount := ???? // how get circles count
if (CirclesCount > 0)
then begin
for i := 0 to CirclesCount - 1 do begin
Circle := Circles[i];
Center := Point(cvRound(Circle[0]), cvRound(Circle[1]));
Radius := cvRound(Circle[2]);
cv.OpenCV.Circle(ImgGray, Center, 1, Scalar(0, 100, 100), 3);
cv.OpenCV.Circle(ImgGray, Center, Radius, Scalar(255, 0, 255), 3);
end;
end;
finally
ImgGray.copyTo(AMat);
ImgGray.release();
end;
end;
OpenCV 端口肯定缺乏数据处理。我设置了
TMat
类型,使用了来自 此示例页面的图片
发现 Mat 尺寸为 1x3 - 因此检测到三个圆圈。
进行了粗略的数据提取:
type
TSingleArray = array[0..9999] of Single;
PSingleArray = ^TSingleArray;
var
AMat, ImgGray: TMat; // TInputArray
Circles: TMat;
n, i: integer;
PFA: PSingleArray;
begin
AMat := imread('D:\houghcircles2.jpg', IMREAD_COLOR);
cvtColor(AMat, ImgGray, COLOR_BGR2GRAY);
MedianBlur(ImgGray, ImgGray, 3);
imwrite('D:\houghcircles21.jpg', ImgGray);
// Detect circles and store them in the Circles array
HoughCircles(ImgGray, Circles, CV_HOUGH_GRADIENT, 2, ImgGray.rows/4, 200, 100);
PFA := PSingleArray(circles.Data);
n := Circles.cols;
for i := 0 to n-1 do
Memo1.Lines.Add(Format('x: %4.1f y: %4.1f radius %4.2f', [PFA[3*i], PFA[3*i+1], PFA[3*i+2]]));
结果:
x: 135.0 y: 125.0 radius 44.80
x: 89.0 y: 47.0 radius 45.80
x: 43.0 y: 131.0 radius 49.00