# 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()
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) # ids, xy, observer_mask, vrthz, vrthz_sigma, flotation params = glimpse.helpers.read_pickle( os.path.join(points_path, basename + '.pkl')) # ---- Load DEM ---- # dem, dem_sigma dem, dem_sigma = dem_interpolant(t, return_sigma=True) # Crop DEM (for lower memory use)
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 ---- # basename = 'AKJNC_20120508_191103C'
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( figsize=tuple(img.cam.imgsz / 100), dpi=100 * 0.25, frameon=False)
# ---- 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,
] for image in images: basename = os.path.join(cg.CG_PATH, 'svg-synth', image) # Skip if output exists if os.path.isfile(basename + '.png'): continue print(image) # Prepare image cam = cg.load_calibrations(image, station=True, camera=True, image=True, viewdir=True, merge=True, file_errors=False) img = glimpse.Image(basename + '.JPG', cam=cam) I = img.read() if I.ndim > 2: I = glimpse.helpers.rgb_to_gray(I).astype(np.uint8) # Prepare synthetic image cam = glimpse.helpers.read_json(basename + '-synth.json') simg = glimpse.Image(basename + '-synth.JPG', cam=cam) sI = simg.read() if sI.ndim > 2: smask = (sI[:, :, 0] != 127).astype(np.uint8) sI = glimpse.helpers.rgb_to_gray(sI).astype(np.uint8) else: smask = (sI != 127).astype(np.uint8) depth = glimpse.Raster.read(basename + '-depth.tif') depth_sigma = glimpse.Raster.read(basename + '-depth_stderr.tif') depth_sigma.Z[np.isnan(depth_sigma.Z)] = 0