mask_transform.py
Go to the documentation of this file.
00001 # --------------------------------------------------------
00002 # Deformable Convolutional Networks
00003 # Copyright (c) 2017 Microsoft
00004 # Licensed under The Apache-2.0 License [see LICENSE for details]
00005 # Written by Haozhi Qi, Yi Li, Guodong Zhang
00006 # --------------------------------------------------------
00007 
00008 import numpy as np
00009 
00010 
00011 def intersect_box_mask(ex_box, gt_box, gt_mask):
00012     """
00013     This function calculate the intersection part of a external box
00014     and gt_box, mask it according to gt_mask
00015     Args:
00016         ex_box: external ROIS
00017         gt_box: ground truth boxes
00018         gt_mask: ground truth masks, not been resized yet
00019     Returns:
00020         regression_target: logical numpy array
00021     """
00022     x1 = max(ex_box[0], gt_box[0])
00023     y1 = max(ex_box[1], gt_box[1])
00024     x2 = min(ex_box[2], gt_box[2])
00025     y2 = min(ex_box[3], gt_box[3])
00026     if x1 > x2 or y1 > y2:
00027         return np.zeros((21, 21), dtype=bool)
00028     w = x2 - x1 + 1
00029     h = y2 - y1 + 1
00030     ex_starty = y1 - ex_box[1]
00031     ex_startx = x1 - ex_box[0]
00032 
00033     inter_maskb = gt_mask[y1:y2+1 , x1:x2+1]
00034     regression_target = np.zeros((ex_box[3] - ex_box[1] + 1, ex_box[2] - ex_box[0] + 1))
00035     regression_target[ex_starty: ex_starty + h, ex_startx: ex_startx + w] = inter_maskb
00036 
00037     return regression_target
00038 
00039 
00040 def mask_overlap(box1, box2, mask1, mask2):
00041     """
00042     This function calculate region IOU when masks are
00043     inside different boxes
00044     Returns:
00045         intersection over unions of this two masks
00046     """
00047     x1 = max(box1[0], box2[0])
00048     y1 = max(box1[1], box2[1])
00049     x2 = min(box1[2], box2[2])
00050     y2 = min(box1[3], box2[3])
00051     if x1 > x2 or y1 > y2:
00052         return 0
00053     w = x2 - x1 + 1
00054     h = y2 - y1 + 1
00055     # get masks in the intersection part
00056     start_ya = y1 - box1[1]
00057     start_xa = x1 - box1[0]
00058     inter_maska = mask1[start_ya: start_ya + h, start_xa:start_xa + w]
00059 
00060     start_yb = y1 - box2[1]
00061     start_xb = x1 - box2[0]
00062     inter_maskb = mask2[start_yb: start_yb + h, start_xb:start_xb + w]
00063 
00064     assert inter_maska.shape == inter_maskb.shape
00065 
00066     inter = np.logical_and(inter_maskb, inter_maska).sum()
00067     union = mask1.sum() + mask2.sum() - inter
00068     if union < 1.0:
00069         return 0
00070     return float(inter) / float(union)


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30