2014-05-16 48 views
1

請檢查該代碼算法how to find area of rectangle which is covering another rectangle通過@Arun [R告訴什麼是錯在它爲什麼我的代碼不刪除內部矩形

我的輸出 enter image description here

爲什麼不刪除裏面的矩形其他

cdef class Ray: 
    cdef public: 
     Point2D p 
     Point2D q 
     list points 

cdef class Boundbox: 
    cdef public: 
     Point2D minP 
     Point2D maxP 
     int componentID 

     int getComponentID(self): 
      return self.componentID 
def __numeric_compare_by_x(self,Boundbox a,Boundbox b): 
    cdef Point2D tempA,tempB 
    tempA=a.minP 
    tempB=b.minP 
    return tempA.x-tempB.x 

def __numeric_compare_by_y(self,Boundbox a,Boundbox b): 
    cdef Point2D tempA,tempB 
    tempA=a.minP 
    tempB=b.minP 
    return tempA.y-tempB.y 

def isBoundbox_inside(self,b1,b2): 
    if((b1.minP.x<=b2.minP.x and b1.minP.y<=b2.minP.y)and(b1.maxP.x>=b2.maxP.x and b1.maxP.y>=b2.maxP.y)): 
     return True 
    return False 

def boundboxFilter(self,boundbox): 

    cdef: 
     int i 
     int minx,miny,maxx,maxy 
     list pointList=[] 
     Boundbox p,b1,b2 

    #for i in xrange(len(boundbox)): 
    # pointList.append(boundbox[i]) 

    pointList=boundbox 
    pointList.sort(cmp=self.__numeric_compare_by_x) 

    stack=[] 
    stack.append(pointList[0]) 

    for p in pointList[1:]: 
     top=len(stack)-1 
     b1=stack[top] 
     b2=p 
     if(not(self.isBoundbox_inside(b1,b2))): 
      stack.append(b2) 

    pointList=stack 
    pointList.sort(cmp=self.__numeric_compare_by_y) 

    stack=[] 
    stack.append(pointList[0]) 
    for p in pointList[1:]: 
     top=len(stack)-1 
     b1=stack[top] 
     b2=p 
     if(not(self.isBoundbox_inside(b1,b2))): 
      stack.append(b2) 

    return stack 
+1

你可以展示一個更簡單的例子,也許正好兩個矩形,包含和一個被刪除?很難理解這麼複雜的輸入是怎麼回事 – Nicolas78

+0

我的輸入是所有矩形的[minx,miny,maxx,maxy]列表,如圖所示 –

+2

也許你可以嘗試調整[層次結構功能](http: //opencvpython.blogspot.ch/2013/01/contours-5-hierarchy.html)在OpenCV而不是使用自定義算法? – Raoul

回答

1

問題在於您的過濾邏輯。

outsiders = [] 
for rect in pointList: 
    if not any(is_inside(rect, box) for box in pointlist if box is not rect): 
     outsiders.append(rect) 

is_inside爲真,如果rect裏面box。您需要檢查每個矩形與其他所有矩形,並將其保存,如果它不在其中。你當然可以調整它的表現。

另一種需要較少比較的方法是考慮整個列表,並刪除其他人發現的矩形。但是,你必須非常小心的索引,並在列表中間彈出的東西也很昂貴。

+0

是否有任何有效的方式來完成這項工作 –

+0

@Rahol從以前的評論可能會更好的選擇 –

相關問題