SAP 冲突算法在OCR中的应用

碰撞检测问题:

在做 APP 名片扫描通 scanzen应用过程中,有一种问题总是困扰着我,如下图

行成算法中,间架结构很分散的字符(如名字)中的联通区(如、撇、捺)影响成行。


处理:

解决这个问题,简单的想法就是根据联通区是否相交来消除这些(撇、捺、细小联通区),

简单的做法是这样的

for( each rectangel r1)
	for( each rectangle r2)

		if(BoxIntersect(r1,r2))
		{
			merge(r1,r2)
		}


但这复杂度高了点,very cpu demanding

正如某兄所解说的:

The DoBoxesIntersect above is a good pairwise solution. However, if you have a lot of boxes, you still have an O(N^2) problem, and you might find you need to do something on top of that like what Kaj refers to. (In the 3D collision detection literature, this is known as having both a broad-phase and a narrow-phase algorithm. We'll do something really fast to find all possible pairs of overlaps, and then something more expensive to see if our possible pairs are actual pairs.)

The broad-phase algorithm I've used before is "sweep-and-prune"; for 2D, you'd maintain two sorted lists of the start and end of each box. As long as box movement is not >> box scale from frame to frame, the order of these lists isn't going to change much, and so you can use bubble or insertion sort to maintain it. The book "Real-Time Rendering" has a nice writeup on optimizations you can do, but it boils down to O(N+K) time in the broad phase, for N boxes, K of which overlap, and with excellent real-world performance if you can afford N^2 booleans to keep track of which pairs of boxes are intersecting from frame-to-frame. You then have O(N+K^2) time overall, which is << O(N^2) if you have many boxes but only a few overlaps.

方案:

虽然是闭门造车,但google是必须的。

google:fast collission  detect -->http://www.gamedev.net/topic/589881-super-fast-collision-detection/

google:  sweep and prune (SAP)  http://www.codercorner.com/SAP.pdf

发现做任何事都不是一个人在战斗,做2D物理引擎的都会用到这个算法,做3D游戏的称呼这个算法为 逐维递归分组法 。



Sweep and Prune:
1.将物体的AABB分离到两个坐标轴上。得到若干个区间。
2.根据区间的终点坐标由小到大排序。
3.逐个遍历排序结果,当遇到一个区间的起始点的时候,就将这个区间放到一个列表中;当遇到一个区间的终点时,就将这个区间从列表中清除。
 当在列表中存在区间,而又遇到一个新区间的起始点时,则遇到的区间与列表中的所有区间重叠。
4.如果一对物体在两个坐标轴上的区间都重叠,那么他们的AABB相叠。


结果:

检测到相交后,将两个联通区归并成一个处理(或者丢弃小的联通区),分行结果如下。名字处理的很正常了


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值