Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的代码没有删除内部矩形_Python_Algorithm_Opencv_Cython - Fatal编程技术网

Python 为什么我的代码没有删除内部矩形

Python 为什么我的代码没有删除内部矩形,python,algorithm,opencv,cython,Python,Algorithm,Opencv,Cython,请按照@Arun R的算法检查此代码中的错误 我的输出是 为什么它不删除其他文件中的矩形 cdef class Ray: cdef public: Point2D p Point2D q list points cdef class Boundbox: cdef public: Point2D minP Point2D maxP int componentID i

请按照@Arun R的算法检查此代码中的错误

我的输出是

为什么它不删除其他文件中的矩形

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
cdef类射线:
cdef公众:
点2D p
点2D q
列出要点
cdef类边界框:
cdef公众:
点2D最小值
点2D最大值
整数分量
int getComponentID(自):
返回self.componentID
定义数值比较x(自身、边界框a、边界框b):
cdef Point2D tempA,tempB
tempA=a.minP
tempB=b.minP
返回tempA.x-tempB.x
定义数值比较(自身、边界框a、边界框b):
cdef Point2D tempA,tempB
tempA=a.minP
tempB=b.minP
返回tempA.y-tempA.y
def isBoundbox_内部(自身、b1、b2):
如果((b1.minP.x=b2.maxP.y)):
返回真值
返回错误
def boundboxFilter(自身、边界框):
cdef:
int i
int minx,miny,maxx,maxy
列表点列表=[]
边界框p、b1、b2
#对于xrange中的i(len(边界框)):
#追加(边界框[i])
点列表=边界框
pointList.sort(cmp=self.\uuuu数值\uu比较\ux)
堆栈=[]
stack.append(点列表[0])
对于点列表[1:]中的p:
顶部=透镜(堆栈)-1
b1=堆栈[顶部]
b2=p
如果(不在(b1,b2)内的self.isBoundbox_):
stack.append(b2)
点列表=堆栈
pointList.sort(cmp=self.\u数值\u按y比较)
堆栈=[]
stack.append(点列表[0])
对于点列表[1:]中的p:
顶部=透镜(堆栈)-1
b1=堆栈[顶部]
b2=p
如果(不在(b1,b2)内的self.isBoundbox_):
stack.append(b2)
返回堆栈

问题在于过滤逻辑

outsiders = []
for rect in pointList:
    if not any(is_inside(rect, box) for box in pointlist if box is not rect):
        outsiders.append(rect)
如果
rect
框中,则
框中,则
内,则
为真。您需要对照所有其他矩形检查每个矩形,如果它不在其中任何一个矩形内,则将其保存。当然,您可以调整它的性能


另一种需要较少比较的方法是考虑整个列表,并删除其他内部发现的矩形。但是,你必须非常小心地进行索引,而且列表中间的东西也很贵。

你能举一个简单的例子,也许正好是两个矩形,包含的和要移除的矩形?很难理解如此复杂的输入是怎么回事我的输入是所有矩形的[minx,miny,maxx,maxy]列表,如图所示也许你可以尝试在OpenCV中进行调整,而不是使用自定义算法?我不是指参数列表,但是具体的输入值和预期的输出值。有什么有效的方法可以做到这一点吗job@Rahol根据之前的评论,这可能是更好的选择