def write_calibration(camera, model, fit, group=0, suffix='', station=None): # model.set_cameras(fit.params) i = model.group_indices[group][0] # Save means means = [item.value for item in fit.params.values()] model.set_cameras(means) # (fmm, cmm, k, p, sensorsz) model.cams[i].write(path=os.path.join('cameras', camera + suffix + '.json'), attributes=('fmm', 'cmm', 'k', 'p', 'sensorsz'), indent=4, flat_arrays=True) # (xyz) if station: xyz0 = cg.load_calibrations(station_estimate=station, merge=True)['xyz'] print('xyz deviation:', model.cams[i].xyz - xyz0) model.cams[i].write(path=os.path.join('stations', station + suffix + '.json'), attributes=['xyz'], indent=4, flat_arrays=True) # Save standard errors stderrs = [item.stderr for item in fit.params.values()] if fit.errorbars: model.set_cameras(stderrs) # (fmm, cmm, k, p) model.cams[i].write(path=os.path.join('cameras', camera + suffix + '_stderr.json'), attributes=('fmm', 'cmm', 'k', 'p'), indent=4, flat_arrays=True) # (xyz) if station: model.cams[i].write(path=os.path.join( 'stations', station + suffix + '_stderr.json'), attributes=['xyz'], indent=4, flat_arrays=True) model.reset_cameras()
# ---- Load DEM interpolant ---- dem_interpolant = glimpse.helpers.read_pickle(dem_interpolant_path) 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)
# ---- Constants ---- # STATION = 'AK10b' DEM_PATH = '/volumes/science-b/data/columbia/_new/ifsar/hae.tif' DEM_ZLIM = (1, np.inf) DDEG = 0.05 # --- Load DEM ---- # dem = glimpse.Raster.read(DEM_PATH) dem.crop(zlim=DEM_ZLIM) # --- Compute horizon ---- # eop = cg.load_calibrations(station_estimate=STATION, merge=True) # Stamp null circle into DEM around camera dem.fill_circle(center=eop['xyz'], radius=100) # Sample horizon in swath in front of camera heading = eop['viewdir'][0] headings = np.arange(heading - 90, heading + 90, step=DDEG) # 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,
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]] box = glimpse.helpers.intersect_boxes(np.row_stack(( cam_box, dem_grid.box2d, ortho_grid.box2d)))
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'
if meta['station'] in stations] 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)
] # ---- Validation synths (calibrated camera) ---- for image in images: basename = os.path.join('images', image) need = [os.path.isfile(basename + suffix + '.json') for suffix in suffixes] missing = [ not os.path.isfile(basename + suffix + '-synth.JPG') for suffix in suffixes ] if not any(np.logical_and(need, missing)): continue # Load base image img_path = cg.find_image(image) calibration = cg.load_calibrations(image=image, merge=True) img = glimpse.Image(img_path, cam=calibration) path = basename + '.JPG' if not os.path.isfile(path): bw = glimpse.helpers.rgb_to_gray(img.read()).astype(np.uint8) img.write(path=path, I=clahe.apply(bw)) # 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)
cartesian_path = 'points-cartesian' cylindrical_path = 'points-cylindrical' # ---- 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(
images = [ glimpse.helpers.strip_path(path) for path in glob.glob(os.path.join('svg-synth', '*.svg')) ] 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: