检测在xcode中绘制路径是否为圆/矩形 [英] Detect if drawing a path is a circle/rectangle in xcode

查看:108
本文介绍了检测在xcode中绘制路径是否为圆/矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为绘图应用实现自动完成功能。一旦绘制了自由手对象,我想检测对象的类型(圆/矩形/三角形)并根据结果想要绘制相应的对象。

I want to implement a auto complete feature for a drawing app. Once a free hand object is drawn, I want to detect the type of object (circle/rectangle/triangle) and based on the result would want to plot a corresponding object.

我已经阅读了一些关于OpenCV的内容,但后来我需要将用户绘图实时转换为图像。我正在记录触摸绘制/跟踪的点数,并生成相应路径的 UIBeizerPath 。我该如何检测形状类型?

I have read up a bit about OpenCV but then I would need to convert the user drawing into an image at real time. I am recording the number of points plotted/traced by the touch and also generate a UIBeizerPath of the corresponding path. How do I go about to detecting the shape type?

推荐答案

您需要先分段数据点。谷歌在笔画分割中找到相关文章。一种简单快速的算法是计算每个数据点的前向斜率和后向斜率,然后计算前向斜率和后向斜率之间的转向角度。如果转弯角度大于某个角度阈值,那么您可以假设您的路径在那里急转弯。根据计算出的急转数,可以推断出这些点是代表三角形(有2个急转弯),四边形(有3个急转弯)还是其他东西。要推断数据点代表圆形或矩形,您需要进行额外的计算。例如,如果根本没有急转弯,请对数据点进行圆拟合,以查看拟合圆的最大误差是否小于某个公差。要输出矩形,您必须将直线拟合到每个数据点段,并检查拟合线是否彼此正交。

You need to segment the data points first. Google on "stroke segmentation" to find related articles. One simple and fast algorithm is to compute the forward slope and backward slope for each data point, then compute the turning angle between forward slope and backward slope. If the turning angle is greater than a certain angle threshold, then you can assume that your path takes a sharp turn there. From the number of sharp turns computed, you can infer whether the points represent a triangle (with 2 sharp turns), a quadrilateral (with 3 sharp turns) or something else. To infer that the data points represent a circle or a rectangle, you will need to do additional computation. For example, if there is no sharp turns at all, do a circle fitting to the data points to see if the maximum error to the fitted circle is smaller than a certain tolerance. To output a rectangle, you will have to fit straight lines to each segment of data points and check if the fitted lines are more or less orthogonal to each other.

这篇关于检测在xcode中绘制路径是否为圆/矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆