SAP 冲突算法在OCR中的应用

碰撞检测问题:

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

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

SAP 冲突算法在OCR中的应用_第1张图片

处理:

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

简单的做法是这样的

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游戏的称呼这个算法为 逐维递归分组法 。

SAP 冲突算法在OCR中的应用_第2张图片


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


结果:

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


你可能感兴趣的:(and,Detect,Sweep,prune,collission,Scanzen)