档案交换一周

Our best user submissions

Detecting Ellipses in Images

Brett's Pick this week is使用1D霍夫变换检测椭圆检测, by马丁·西蒙诺夫斯基

Contents

介绍

过去,我已经写了几次关于在图像中检测圈子的文章(这里,这里, 和这里)。我也写过绘制椭圆。今天,我想写检测椭圆

Since ellipses are described by more parameters than are lines or circles, detecting ellipses is more challenging than is detecting lines or circles. And while we have nice functionality for detecting the simpler shapes (Houghlines,imfindcircles), we do not (currently) have a function to detect ellipses. Enter Martin's ellipse-detection function.

来源

幸运的是,马丁(Martin)提到了他的算法来源。他使用了一篇论文(Y. Xie,Q。Ji。以MATLAB代码实现它。我喜欢马丁为我做的工作 - 这让我更加欣赏文件交换。

The Implementation

与马丁的椭圆夹()在一个下午的大部分时间里,我有一些想法要分享。首先,认识到椭圆检测是昂贵的记忆猪。计算与非零像素数的平方缩放,N,在您的搜索图像中。几乎可以肯定,您想在“边缘图像”上操作,而不是您感兴趣的区域的简单二进制掩码,并使用函数的九个输入参数来限制搜索。在这些参数中,您可以指定要考虑的主要轴长度和最小纵横比的范围。您还可以指定一些参数以限制所寻求的椭圆的角度;如果您知道椭圆方向,这可能非常有用先验。即使使用了此类限制,详尽的搜索检查NxNcandidates for major axes. Martin's function provides a parameter that allows you to trade off between speed and accuracy. (The "randomize" parameter is not a Boolean variable, as the name suggests; rather it reduces the search space fromNxNNx随机化。如果随机化为0,则搜索是详尽的 - 以计算成本为准精度。)

A Test Drive

因此,让我们在我们创建的示例图像上尝试一下。我们可以从包含圆圈的图像开始,然后翘曲以生成椭圆:

inputImage = imread('coloredchips.png'); tform = affine2d([1 0 0; 0.75 1 0; 0 0 1]); inputImage = imwarp(inputImage, tform); imshow(inputImage) title(“测试图像”)

分割

I分段图像:

  1. 使用ColorThresholder至create a mask of the background
  2. 将颜色图像分为r,g和b组件(imsplit)
  3. 单独掩盖R,G和B平面(R(backgroundMask) = 0,...))
  4. 将蒙面的平面重新分配到RGB图像中()
  5. Converting the masked RGB image to grayscale (IM2Gray)
  6. 计算(使用精心选择的输入参数)灰度图像的边缘(边缘)
  7. 过滤结果以选择所需的主要轴长度和区域(成像静脉分析仪,bwpropfilt)

在短短几分钟内,我有一个边缘图像来检测这些椭圆:

Detecting the Ellipses

With this binary edge mask in hand, I was ready to search for ellipses--first, naively:

bestFits =椭圆夹(Edgemask);

The operation completed in (just) less than a second, but the results were underwhelming:

(顺便说一句,打电话椭圆夹()on the binary mask of the warped chipswithout首先计算边缘需要20分钟以上,结果甚至更糟!)

改善结果

To improve the performance, I judiciously selected input parameters to reduce the computational cost. First, I usedimdistline至measure the major and minor axes lengths:

Then I used量角器要了解椭圆的方向:

几分钟后,我找到了:

params.minMajorAxis = 55; params.maxMajorAxis = 75; params.minAspectRatio = 0.4;%1 =圆圈;0 =线;params.rotation = 35; params.rotationSpan = 10; params.randomize = 0;%详尽的搜索params.numbest = 30;%(Number of chips = 26)
bestFits =椭圆形tection(edgemask,params);

削皮结果

等等...什么?BestFitscontains paramaters for 30 detections (a manual count informs me that there are 26 ellipses wholly contained in the image), but when visualizing the results, it appears that there are far fewer. What's going on?

事实证明,该算法容易多次报告相同的椭圆形。(如果我拖动这些青色椭圆,下面还有其他一致的椭圆!)

试验,我发现很有用overspecifythe number of ellipses I wanted to detect, then to pare the results in post-processing. Consider:

params.smoothStddev = 0.5; params.numBest = 1000;%(Number of chips = 26)bestFits =椭圆形tection(edgemask,params);MinCenterDistance = 10;MinScore = 30;Maxaspectratio = 0.6;bestFits = pareellipsefits(bestfits,...minCenterDistance, minScore, maxAspectRatio);

I wrotepareresults至allow filtering by minimum center distance (to disallow overlapping detections), minimum score, and maximum aspect ratio. Using that paring approach, I can request far more detections (params.numBest = 1000) than I really want, then discard "bad" results. I'm pretty pleased with the way it's working!

A Note on Visualizing the Results

Finally, I also wroteVisualizeEllipses打电话drawellipse直接在输出上椭圆曲

笔记

In the interest of making this post a bit shorter (I know...too late!), I didn't post all of the code. If anyone would like to see it, I'm happy to share. Just drop me an email at:

char (cumsum ([98 17 -11 -47 -10 7 7 4 45 12 19 -12 15 -8 3 -7 8 -69 53 12 -2]))

此外,在交易所上还有其他一些文件,这些文件声称可以促进椭圆检测。如果您想在随后的帖子中看到那些,请给我发表评论!

一些建议

马丁的实施使用高斯过滤fspecial。不再推荐该语法;较新imgaussfilt更有效。另外,打电话给fspecialrequires aninteger第二个论点('hsize') parameter. (I added a call to圆形的()in my version.) Finally, since getting good results requires tuning a number of parameters, this function is just乞讨对于代码生成的应用程序来领导它;这也许是另一天的项目!

谢谢您,马丁...找到此功能,并弄清楚如何使用它,为我的图像处理库提供了一个有价值的工具。

As always, I welcome your思想和评论




Published with MATLAB® R2022a

|
  • print
  • send email

评论

要发表评论,请点击此处登录到您的的帐户帐户或一个新。。。