# headings = np.arange(0, 360, step=DDEG) # AK12 only hxyz = dem.horizon(eop['xyz'], headings) # --- Format and save GeoJSON ---- # geo = geojson.FeatureCollection([ geojson.Feature(geometry=geojson.LineString(xyz.tolist())) for xyz in hxyz ]) geo = glimpse.helpers.ordered_geojson(geo) glimpse.helpers.write_geojson(geo, 'geojson/horizons/' + STATION + '.geojson', crs=32606, decimals=(5, 5, 0)) # --- Check result ---- # svg_path = glob.glob('svg/' + STATION + '_*.svg')[-1] img_path = cg.find_image(svg_path) cam_args = cg.load_calibrations(path=img_path, station_estimate=True, merge=True) img = glimpse.Image(img_path, cam=cam_args) geo = glimpse.helpers.read_geojson('geojson/horizons/' + STATION + '.geojson', crs=32606) lxyz = [coords for coords in glimpse.helpers.geojson_itercoords(geo)] luv = [img.cam.project(xyz, correction=True) for xyz in lxyz] img.plot() for uv in luv: matplotlib.pyplot.plot(uv[:, 0], uv[:, 1], color='red') img.set_plot_limits()
dem_padding = 200 # m # ---- Track points ---- for i_obs in range(0, len(observer_json)): # ---- Load observers ---- # observers observers = [] for station, basenames in observer_json[i_obs].items(): meta = cg.parse_image_path(basenames[0], sequence=True) service_calibration = cg.load_calibrations( station_estimate=meta['station'], station=meta['station'], camera=meta['camera'], merge=True, file_errors=False) datetimes = cg.paths_to_datetimes(basenames) # Use dummy Exif for speed service_exif = glimpse.Exif(cg.find_image(basenames[0])) images = [] for basename, t in zip(basenames, datetimes): calibration = glimpse.helpers.merge_dicts(service_calibration, cg.load_calibrations(image=basename, viewdir=basename, merge=True, file_errors=False)) path = cg.find_image(basename) image = glimpse.Image(path, cam=calibration, datetime=t, exif=service_exif) images.append(image) # NOTE: Determine sigma programmatically? observer = glimpse.Observer(images, cache=True, correction=True, sigma=0.3) observers.append(observer) # ---- Load track points ---- t = min([observer.datetimes[0] for observer in observers]) datestr = t.strftime('%Y%m%d') basename = str(i_obs)
# Prepare orthophotos ortho_paths = glob.glob(os.path.join(root, 'ortho', '*.tif')) ortho_paths += glob.glob(os.path.join(root, 'ortho-ifsar', 'data', '*.tif')) ortho_dates = [datetime.datetime.strptime(re.findall(r'([0-9]{8})', path)[0], '%Y%m%d') for path in ortho_paths] # ---- Control synths (ideal camera) ---- for image in images: print(image) start = timeit.default_timer() basename = os.path.join('svg-synth', image) if os.path.isfile(basename + '-synth.JPG'): continue # Load image img_path = cg.find_image(image) cam_args = cg.load_calibrations(image, station_estimate=True, station=True, merge=True, file_errors=False) img = glimpse.Image(img_path, cam=cam_args) img.cam.resize(img_size) # Select nearest dem and ortho img_date = datetime.datetime.strptime(cg.parse_image_path(image)['date_str'], '%Y%m%d') i_dem = np.argmin(np.abs(np.asarray(dem_dates) - img_date)) i_ortho = np.argmin(np.abs(np.asarray(ortho_dates) - img_date)) dem_path = dem_paths[i_dem] ortho_path = ortho_paths[i_ortho] # Load raster metadata dem_grid = glimpse.Grid.read(dem_path, d=grid_size) ortho_grid = glimpse.Grid.read(ortho_path, d=grid_size) # Intersect bounding boxes cam_box = img.cam.viewbox(50e3)[[0, 1, 3, 4]]
matplotlib.pyplot.imshow(I, alpha=0.5) m.plot(scale=20, width=1, selected='yellow') errors = (1 / scale) * np.linalg.norm(m.observed() - m.predicted(), axis=1) matplotlib.pyplot.title( glimpse.helpers.strip_path(img.path) + ' - ' + glimpse.helpers.strip_path(imgB.path) + '\n' + str(round(errors.mean(), 2)) + ', ' + str(round(errors.std(), 2))) img.set_plot_limits() img.cam.resize(1) imgB.cam.resize(1) m.resize(1) # ---- Check single image (svg) ---- # basename = 'AK10b_20120605_203759' img = glimpse.Image(path=cg.find_image(basename), cam=cg.load_calibrations(basename, station_estimate=True, merge=True)) controls = cg.svg_controls(img) svg_model = glimpse.optimize.Cameras(img.cam, controls, cam_params=dict(viewdir=True), group_params=group_params[-1]) svg_fit = svg_model.fit(full=True, group_params=group_params[:-1]) matplotlib.pyplot.figure() img.plot() svg_model.plot(svg_fit.params) img.set_plot_limits() # ---- Check undistorted image ---- #
for path in paths: meta = cg.parse_image_path(path, sequence=True) svg_keys = camera_keys.get(meta['camera'], keys) for suffix in suffixes: if not os.path.isfile(os.path.join( 'cameras', meta['camera'] + suffix + '.json')): continue basename = os.path.join('images', meta['basename'] + suffix) if os.path.isfile(basename + '.json'): continue print(meta['basename'] + suffix) # TODO: Use station xyz estimated for calib for non-fixed stations calibration = cg.load_calibrations(path, station_estimate=meta['station'], station=meta['station'], camera=meta['camera'] + suffix, merge=True, file_errors=False) img_path = cg.find_image(path) img = glimpse.Image(img_path, cam=calibration) controls = cg.svg_controls(img, keys=svg_keys, step=step) controls += cg.synth_controls(img, step=step) if not controls: print("No controls found") continue model = glimpse.optimize.Cameras( cams=img.cam, controls=controls, cam_params=dict(viewdir=True)) fit = model.fit(full=True) model.set_cameras(fit.params) img.cam.write(basename + '.json', attributes=('xyz', 'viewdir', 'fmm', 'cmm', 'k', 'p', 'sensorsz'), indent=4, flat_arrays=True) # Plot image with markup fig = matplotlib.pyplot.figure(
# ---- Load first image from each observer station ---- # images json = glimpse.helpers.read_json('observers.json', object_pairs_hook=collections.OrderedDict) start_images = [] progress = glimpse.helpers._progress_bar(max=len(json)) for observers in json: starts = [] for station, basenames in observers.items(): ids = cg.parse_image_path(basenames[0], sequence=True) cam_args = cg.load_calibrations(station=station, camera=ids['camera'], image=basenames[0], viewdir=basenames[0], merge=True, file_errors=False) path = cg.find_image(basenames[0]) starts.append(glimpse.Image(path, cam=cam_args)) start_images.append(tuple(starts)) progress.next() # ---- Load DEM interpolant ---- dem_interpolant = glimpse.helpers.read_pickle(dem_interpolant_path) # ---- Load canonical velocities (cartesian) ---- # vx, vx_sigma, vy, vy_sigma names = 'vx', 'vx_stderr', 'vy', 'vy_stderr' vx, vx_sigma, vy, vy_sigma = [glimpse.Raster.read( os.path.join('velocity', name + '.tif')) for name in names]
IMG_SIZE = 0.5 FIGURE_SIZE = 0.25 MAX_RATIO = 0.5 # For each motion sequence... motion = glimpse.helpers.read_json('motion.json') for d in motion: paths = np.asarray(d['paths']) # Skip if all files already exist basenames = [os.path.join('motion', paths[i] + '-' + paths[i + 1]) for i in range(len(paths) - 1)] nexists = np.sum([os.path.isfile(basename + '.pkl') for basename in basenames]) if nexists == len(paths) - 1: continue # Load images images = [glimpse.Image(cg.find_image(path)) for path in paths] # Compute sequential matches for img in images: img.cam.resize(IMG_SIZE) matches = cg.build_sequential_matches(images, match=dict(max_ratio=MAX_RATIO)) # For each motion pair... for i, control in enumerate(matches): # Skip if file exists if os.path.isfile(basenames[i] + '.pkl'): continue print(basenames[i]) # Initialize control control.resize(1) # Filter with RANSAC model = glimpse.optimize.Cameras( control.cams, control,