import cv2 import numpy as np def shift_2d_replace(data, dx, dy, constant=False): shifted_data = np.roll(data, dx, axis=1) if dx < 0: shifted_data[:, dx:] = constant elif dx > 0: shifted_data[:, 0:dx] = constant shifted_data = np.roll(shifted_data, dy, axis=0) if dy < 0: shifted_data[dy:, :] = constant elif dy > 0: shifted_data[0:dy, :] = constant return shifted_data def soft_edge_error(pred, gt, radius=1): abs_diff=[] for i in range(-radius, radius + 1): for j in range(-radius, radius + 1): abs_diff.append(np.abs(shift_2d_replace(gt, i, j, 0) - pred)) return np.minimum.reduce(abs_diff) def get_boundaries(disp, th=1., dilation=10): edges_y = np.logical_or(np.pad(np.abs(disp[1:, :] - disp[:-1, :]) > th, ((1, 0), (0, 0))), np.pad(np.abs(disp[:-1, :] - disp[1:, :]) > th, ((0, 1), (0, 0)))) edges_x = np.logical_or(np.pad(np.abs(disp[:, 1:] - disp[:, :-1]) > th, ((0, 0), (1, 0))), np.pad(np.abs(disp[:, :-1] - disp[:,1:]) > th, ((0, 0), (0, 1)))) edges = np.logical_or(edges_y, edges_x).astype(np.float32) if dilation > 0: kernel = np.ones((dilation, dilation), np.uint8) edges = cv2.dilate(edges, kernel, iterations=1) return edges