Python 从掩模RCNN中的掩模矩阵(布尔矩阵)查找掩模角点坐标(矩形形状)?

Python 从掩模RCNN中的掩模矩阵(布尔矩阵)查找掩模角点坐标(矩形形状)?,python,deep-learning,mask,Python,Deep Learning,Mask,在我的项目中,我尝试在数据集中检测商店标志。我用的是面具RCNN。图像大小为512x512。 results=model.detect([image],verbose=1) r=结果[0] 蒙版=r[“蒙版”] rois=r['rois']如果您知道旋转角度,只需旋转bbox角点,例如在角点上使用cv2.warpAffine。如果你不这样做,那么你可以像这样或多或少地找到极值 H,W = array.shape left_edges = np.where(array.any(axis=1),

在我的项目中,我尝试在数据集中检测商店标志。我用的是面具RCNN。图像大小为512x512。

results=model.detect([image],verbose=1)
r=结果[0]
蒙版=r[“蒙版”]

rois=r['rois']
如果您知道旋转角度,只需旋转bbox角点,例如在角点上使用cv2.warpAffine。如果你不这样做,那么你可以像这样或多或少地找到极值

H,W = array.shape
left_edges = np.where(array.any(axis=1),array.argmax(axis=1),W+1)
flip_lr = cv2.flip(array,1) #1 horz vert 0
right_edges = W-np.where(flip_lr.any(axis=1),flip_lr.argmax(axis=1),W+1)
top_edges = np.where(array.any(axis=0),array.argmax(axis=0),H+1)
flip_ud = cv2.flip(array,0) #1 horz vert 0
bottom_edges = H - np.where(flip_ud.any(axis=0),flip_ud.argmax(axis=0),H+1)
leftmost = left_edges.min()
rightmost = right_edges.max()
topmost = top_edges.min()
bottommost = bottom_edges.max()

你的bbox有四个角(最左边、最上面、(最右边、最下面),这是我尝试过的一个例子。顺便说一句,如果你发现自己在像素上循环,你应该知道几乎总是有一个numpy矢量化操作,它会做得更快

如果您知道旋转角度,只需旋转bbox角点,例如在角点上使用cv2.warpAffine。如果你不这样做,那么你可以像这样或多或少地找到极值

H,W = array.shape
left_edges = np.where(array.any(axis=1),array.argmax(axis=1),W+1)
flip_lr = cv2.flip(array,1) #1 horz vert 0
right_edges = W-np.where(flip_lr.any(axis=1),flip_lr.argmax(axis=1),W+1)
top_edges = np.where(array.any(axis=0),array.argmax(axis=0),H+1)
flip_ud = cv2.flip(array,0) #1 horz vert 0
bottom_edges = H - np.where(flip_ud.any(axis=0),flip_ud.argmax(axis=0),H+1)
leftmost = left_edges.min()
rightmost = right_edges.max()
topmost = top_edges.min()
bottommost = bottom_edges.max()

你的bbox有四个角(最左边、最上面、(最右边、最下面),这是我尝试过的一个例子。顺便说一句,如果你发现自己在像素上循环,你应该知道几乎总是有一个numpy矢量化操作,它会做得更快

透视变换可用于此问题:

  • 从检测到的口罩中查找商店标志的角点。(src点)
  • 所需矩形框点(dst点)
  • 使用
    cv2.getPerspectiveTransform
    cv2.warpPerspective
  • 对于角点检测,我们可以使用
    cv2.findContours
    cv2.approxPolyDP

    在二值图像中查找轮廓

    contours, _ = cv2.findContours(r['masks'][:,:,0].astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    然后使用以下公式从轮廓近似矩形:

    cv2.approxPolyDP
    中的关键点是
    epsilon
    (近似精度参数)。矩形点检测的自定义阈值(如下)


    透视变换可用于此问题:

  • 从检测到的口罩中查找商店标志的角点。(src点)
  • 所需矩形框点(dst点)
  • 使用
    cv2.getPerspectiveTransform
    cv2.warpPerspective
  • 对于角点检测,我们可以使用
    cv2.findContours
    cv2.approxPolyDP

    在二值图像中查找轮廓

    contours, _ = cv2.findContours(r['masks'][:,:,0].astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    然后使用以下公式从轮廓近似矩形:

    cv2.approxPolyDP
    中的关键点是
    epsilon
    (近似精度参数)。矩形点检测的自定义阈值(如下)