What coding techniques would allow me to to differentiate between circles, rectangles, and triangles in black and white image bitmaps?
4 回答
您可以训练人工神经网络对形状进行分类:P
If noise is low enough to extract curves, approximations can be used: for each shape select parameters giving least error (he method of least squares may help here) and then compare these errors...
If the image is noisy, I would consider Hough transform - it may be used to detect shapes with small count of parameters, like circle (harder for rectangles and triangles).
只是我头脑中的一个想法:逐行,逐像素扫描(像素)图像。如果遇到第一个白色像素(假设它具有黑色背景),则将其位置作为起点,并在每个方向上查看围绕它的八个像素,以寻找下一个白色像素。如果您找到相邻的第二个像素,您可以在这两个像素之间建立一个方向矢量。
现在重复此操作,直到矢量的方向发生变化(或变化高于某个阈值)。将更改前的最后一点保留为第一行的端点,并为下一行重复该过程。
然后计算两条线之间的角度并存储。现在追踪第三行。计算第二条和第三条线之间的角度。
如果两个角都是矩形,你可能会找到一个矩形,否则你可能会找到一个三角形。如果你找不到任何直线,你可以断定你找到了一个圆。
我知道该算法有点粗略,但我认为(经过一些改进)如果您的图像质量不太差(太多噪音,线条间隙等),它可能会起作用。
您正在寻找Hough 变换。对于实现,请尝试AForge.NET框架。它包括圆和线霍夫变换。