我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用cv2.DIST_L2。
def update(self,frame,events): falloff = self.falloff img = frame.img pts = [denormalize(pt['norm_pos'],frame.img.shape[:-1][::-1],flip_y=True) for pt in events.get('gaze_positions',[]) if pt['confidence']>=self.g_pool.min_data_confidence] overlay = np.ones(img.shape[:-1],dtype=img.dtype) # draw recent gaze postions as black dots on an overlay image. for gaze_point in pts: try: overlay[int(gaze_point[1]),int(gaze_point[0])] = 0 except: pass out = cv2.distanceTransform(overlay,cv2.DIST_L2, 5) # fix for opencv binding inconsitency if type(out)==tuple: out = out[0] overlay = 1/(out/falloff+1) img[:] = np.multiply(img, cv2.cvtColor(overlay,cv2.COLOR_GRAY2RGB), casting="unsafe")
def __init__(self, blob_labels, blob_list): binary = (blob_labels >= 255).astype(np.uint8) dist, labels = cv2.distanceTransformWithLabels(binary, cv2.DIST_L2, 3) newlabels = 255*np.ones(blob_labels.shape, np.uint8) # plt.imshow(dist) # plt.show() # plt.imshow(labels) # plt.show() used_labels = set() for i, pk in enumerate(blob_list): label = labels[int(pk.x), int(pk.y)] #assert(label not in used_labels) if label in used_labels: raise RuntimeError("Adjacent blobs connected by diagonal. Cannot handle it!") used_labels.add(label) mask = (labels == label) newlabels[mask] = i self.labels = newlabels
def __call__(self, roi_slice, num): blob_labels = self.blob_labels[roi_slice] # everthing not pertaining to blobs becomes 1 binary = (blob_labels >= 255).astype(np.uint8) blob_mask = (blob_labels == num) dist, voronoilabels = cv2.distanceTransformWithLabels(binary, cv2.DIST_L2, 3) vl = set(voronoilabels[blob_mask]) if len(vl) != 1: print 'WARNING: In MaskVoronoiLocal, more than one label pertains to the area of the blob.' return np.ones(blob_labels.shape, np.bool) vl = vl.pop() # plt.imshow(binary) # plt.colorbar() # plt.show() # plt.imshow(voronoilabels) # plt.colorbar() # plt.show() return voronoilabels == vl
def dist_mask(mask, max_dist=10): mask = mask.astype(np.uint8) def get_dist(m): d = cv2.distanceTransform(m, cv2.DIST_L2, maskSize=3) d[d > max_dist] = max_dist return d / max_dist dist = get_dist(mask) - get_dist(1 - mask) # TODO - check in the notebook # TODO - what is the proper power? #pow = 0.5 #dist[dist > 0] = dist[dist > 0] ** pow #dist[dist < 0] = -((-dist[dist < 0]) ** pow) return (1 + dist) / 2 # from 0 to 1