"""Deterministic synthetic pairs with correspondence ground truth.""" import cv2 import numpy as np CASE_NAMES = ['identity', 'rotation_scale', 'perspective_dark', 'reflection', 'local_warp', 'local_dark', 'local_perspective'] def make_case(reference, name): h, w = reference.shape[:2] yy, xx = np.indices((h,w), dtype=np.float32) step = max(1, round(w/50)) grid = np.stack([xx[::step,::step], yy[::step,::step]], -1).reshape(-1,2) radius = np.linalg.norm((grid-[w/2,h/2]) / [w,h], axis=1) grid = grid[(radius>.17)&(radius<.43)] matrix = np.eye(3) if name != 'identity' and not name.startswith('local_') or name == 'local_perspective': matrix[:2] = cv2.getRotationMatrix2D((w/2,h/2),11,.94) matrix[:2,2] += [.022*w,-.018*h] if name in ('perspective_dark', 'local_perspective'): matrix[2,:2] = [.06/w, -.04/h] if name.startswith('local_'): r = np.hypot(xx/w-.5, yy/h-.5) dx = .009*w*np.sin(yy/(.09*h))*np.exp(-((r-.3)/.18)**2) dy = .007*h*np.sin(xx/(.11*w))*np.exp(-((r-.3)/.18)**2) moving = cv2.remap(reference, xx+dx, yy+dy, cv2.INTER_LINEAR) gx, gy = grid[:,0].astype(int), grid[:,1].astype(int) truth_points = grid + np.stack([dx[gy,gx],dy[gy,gx]],-1) else: moving = reference truth_points = grid moving = cv2.warpPerspective(moving,matrix,(w,h)) moving_points = cv2.perspectiveTransform(grid[None],matrix)[0] if name in ('perspective_dark','local_dark','local_perspective'): moving = np.rint(moving*.55).astype(np.uint8) if name == 'reflection': cv2.circle(moving,(round(.69*w),round(.32*h)),round(.065*w),(255,255,255),-1) moving = cv2.imdecode(cv2.imencode('.jpg',moving,[cv2.IMWRITE_JPEG_QUALITY,93])[1],cv2.IMREAD_COLOR) return moving, truth_points.astype(np.float32), moving_points def mapping_errors(mapping, reference_points, moving_points): predicted = cv2.remap(mapping,reference_points[:,0,None],reference_points[:,1,None],cv2.INTER_LINEAR)[:,0] return np.linalg.norm(predicted-moving_points,axis=1)