def salScore2votes(salImSeq, sp): """ Convert saliency score to votes Input: salImSeq: (n, h, w) where n > 1: float. FG>0, BG=0. score in [0,1]. sp: (n,h,w): 0-indexed regions, #regions <= numsp Output: votes: (k,) where k < numsp*n """ n, h, w = salImSeq.shape numsp = np.max(sp) + 1 votes = np.zeros((numsp * n, ), dtype=np.float) startInd = 0 for i in range(n): sp1 = sp[i].reshape(-1) val1 = salImSeq[i].reshape(-1) sizeOut = np.max(sp1) + 1 # assign average score of pixels to a superpixel sumScore = utils.my_accumarray(sp1, val1, sizeOut, 'plus') count = utils.my_accumarray(sp1, np.ones(sp1.shape), sizeOut, 'plus') votes[startInd:startInd + sizeOut] = sumScore / count startInd += sizeOut votes = votes[:startInd] return votes
def remove_low_energy_blobs(maskSeq, binTh, relSize=0.6, relEnergy=None, target=None): """ Input: maskSeq: (n, h, w) where n > 1: float. FG>0, BG=0. Not thresholded. binTh: binary threshold for maskSeq for finding blobs: [0, max(maskSeq)] relSize: [0,1]: size of FG blobs to keep compared to largest one Only used if relEnergy is None. relEnergy: Ideally it should be <= binTh. Kill blobs whose: (total energy <= relEnergy * numPixlesInBlob) If relEnergy is given, relSize is not used. target: value to which set the low energy blobs to. Default: binTh-epsilon. Must be less than binTh. Output: maskSeq: (n, h, w) where n > 1: float. FG>0, BG=0. Not thresholded. It has same values as input, except the low energy blobs where its value is target. """ sTime = time.time() if target is None: target = binTh - 1e-5 for i in range(maskSeq.shape[0]): mask = (maskSeq[i] > binTh).astype(np.uint8) if np.sum(mask) == 0: continue sp1, num = ndimage.label(mask) # 0 in sp1 is same as 0 in mask i.e. BG count = utils.my_accumarray(sp1, np.ones(sp1.shape), num + 1, 'plus') if relEnergy is not None: sumScore = utils.my_accumarray(sp1, maskSeq[i], num + 1, 'plus') destroyFG = sumScore[1:] < relEnergy * count[1:] else: sizeLargestBlob = np.max(count[1:]) destroyFG = count[1:] < relSize * sizeLargestBlob destroyFG = np.concatenate(([False], destroyFG)) maskSeq[i][destroyFG[sp1]] = target eTime = time.time() print('Removing low energy blobs finished: %.2f s' % (eTime - sTime)) return maskSeq
def get_region_boxes(sp): """ Get bounding boxes for each superpixel image Input: sp: (h,w): 0-indexed regions, #regions <= numsp Output: boxes: (numsp, 4) : (xmin, ymin, xmax, ymax) """ x = np.arange(0, sp.shape[1]) y = np.arange(0, sp.shape[0]) xv, yv = np.meshgrid(x, y) sizeOut = np.max(sp) + 1 sp1 = sp.reshape(-1) xv = xv.reshape(-1) yv = yv.reshape(-1) spxmin = utils.my_accumarray(sp1, xv, sizeOut, 'min') spymin = utils.my_accumarray(sp1, yv, sizeOut, 'min') spxmax = utils.my_accumarray(sp1, xv, sizeOut, 'max') spymax = utils.my_accumarray(sp1, yv, sizeOut, 'max') boxes = np.hstack((spxmin.reshape(-1, 1), spymin.reshape(-1, 1), spxmax.reshape(-1, 1), spymax.reshape(-1, 1))) return boxes