예제 #1
0
def synth_controls(img, step=None, directions=False):
    image = glimpse.helpers.strip_path(img.path)
    basename = os.path.join(CG_PATH, 'svg-synth', image)
    controls = []
    # Load svg
    path = basename + '.svg'
    if os.path.isfile(path):
        svg = glimpse.svg.parse_svg(path, imgsz=img.cam.imgsz)
        if 'points' in svg or 'lines' in svg or 'points-auto' in svg:
            scam = glimpse.helpers.read_json(basename + '-synth.json')
            simg = glimpse.Image(basename + '-synth.JPG', cam=scam)
            if not directions:
                depth = glimpse.Raster.read(basename + '-depth.tif')
                scale = depth.n / img.cam.imgsz
            # NOTE: Ignoring parallax potential
        if 'points-auto' in svg:
            # Length-2 paths traced from image to synthetic image
            uv = np.vstack([x[0] for x in svg['points-auto'].values()])
            suv = np.vstack([x[1] for x in svg['points-auto'].values()])
            d = 1 if directions else depth.sample(suv * scale)
            xyz = simg.cam.invproject(suv, directions=directions, depth=d)
            points = glimpse.optimize.Points(cam=img.cam,
                                             uv=uv,
                                             xyz=xyz,
                                             directions=directions,
                                             correction=False)
            controls.append(points)
        if 'points' in svg:
            # Length-2 paths traced from image to synthetic image
            uv = np.vstack([x[0] for x in svg['points'].values()])
            suv = np.vstack([x[1] for x in svg['points'].values()])
            d = 1 if directions else depth.sample(suv * scale)
            xyz = simg.cam.invproject(suv, directions=directions, depth=d)
            points = glimpse.optimize.Points(cam=img.cam,
                                             uv=uv,
                                             xyz=xyz,
                                             directions=directions,
                                             correction=False)
            controls.append(points)
        if 'lines' in svg:
            for layer in svg['lines'].values():
                # Group with paths named 'image*' and 'synth*'
                uvs = [layer[key] for key in layer if key.find('image') == 0]
                suvs = [layer[key] for key in layer if key.find('synth') == 0]
                depths = [
                    1 if directions else depth.sample(suv * scale)
                    for suv in suvs
                ]
                xyzs = [
                    simg.cam.invproject(suv, directions=directions, depth=d)
                    for suv, d in zip(suvs, depths)
                ]
                lines = glimpse.optimize.Lines(cam=img.cam,
                                               uvs=uvs,
                                               xyzs=xyzs,
                                               step=step,
                                               directions=directions,
                                               correction=False)
                controls.append(lines)
    return controls
예제 #2
0
	def __init__(self,savedir,imagedir,basemodelpath,basedict,imgpoints,worldpoints,mask):
		self.imagepaths =  glob.glob(os.path.join(imagedir,'*.jpg'),recursive=True)
		self.basecam = glimpse.Camera.from_json(basemodelpath)
		self.imagepoints = imgpoints
		self.worldpoints = worldpoints
		self.refimg = glimpse.Image(path=self.imagepaths[0],exif=glimpse.Exif(self.imagepaths[0]),cam= self.basecam)
		self.savedir = savedir
		print(self.basecam)
		self.mask = np.load(mask)
예제 #3
0
def camera_motion_matches(camera,
                          size=None,
                          force_size=False,
                          station_calib=False,
                          camera_calib=False):
    """
    Returns all motion Matches objects available for a camera.

    Arguments:
        camera (str): Camera identifier
        size: Image scale factor (number) or image size in pixels (nx, ny)
        force_size (bool): Whether to force `size` even if different aspect ratio
            than original size.
        station_calib (bool): Whether to load station calibration. If `False`,
            falls back to the station estimate.
        camera_calib (bool): Whether to load camera calibrations. If `False`,
            falls back to the EXIF estimate.

    Returns:
        list: Image objects
        list: Matches objects
        list: Per-camera calibration parameters [{}, {'viewdir': True}, ...]
    """
    motion = glimpse.helpers.read_json(os.path.join(CG_PATH, 'motion.json'))
    sequences = [
        item['paths'] for item in motion if parse_image_path(
            item['paths'][0], sequence=True)['camera'] == camera
    ]
    all_images, all_matches, cam_params = [], [], []
    for sequence in sequences:
        paths = [find_image(path) for path in sequence]
        cams = [
            load_calibrations(path,
                              camera=camera_calib,
                              station=station_calib,
                              station_estimate=not station_calib,
                              merge=True) for path in paths
        ]
        images = [
            glimpse.Image(path, cam=cam) for path, cam in zip(paths, cams)
        ]
        matches = [
            load_motion_match(images[i], images[i + 1])
            for i in range(len(sequence) - 1)
        ]
        if size is not None:
            for match in matches:
                match.resize(size, force=force_size)
        all_images.extend(images)
        all_matches.extend(matches)
        cam_params.extend([dict()] + [dict(viewdir=True)] *
                          (len(sequence) - 1))
    return all_images, all_matches, cam_params
예제 #4
0
from os.path import join
os.environ['OMP_NUM_THREADS'] = '1'
#==============================================

DATA_DIR = "/home/dunbar/Research/helheim/data/observations"
DEM_DIR = os.path.join(DATA_DIR, 'dem')
MAX_DEPTH = 30e3

# ---- Prepare Observers ----

observerpath = ['stardot1','stardot2']
observers = []
for observer in observerpath:
    path = join(DATA_DIR,observer)
    campaths =  glob.glob(join(path,"*.JSON"))
    images = [glimpse.Image(path=campath.replace(".JSON",".jpg"),cam=campath) for campath in campaths]
    images.sort(key= lambda img: img.datetime)
    datetimes = np.array([img.datetime for img in images])
    for n, delta in enumerate(np.diff(datetimes)):
        if delta <= datetime.timedelta(seconds=0):
            secs = datetime.timedelta(seconds= n%5 + 1)
            images[n+1].datetime = images[n+1].datetime + secs
    diffs = np.array([dt.total_seconds() for dt in np.diff(np.array([img.datetime for img in images]))])
    negate = diffs[diffs <= 1].astype(np.int)
    [images.pop(_) for _ in negate]

    images = images[:int(len(images)/2)]
    print("Image set {} \n".format(len(images)))
    obs = glimpse.Observer(list(np.array(images)),cache=False)
    observers.append(obs)
#-------------------------
예제 #5
0
	"vxyz": np.array([10,-6,0]),
	"vxyz_sigma": np.array([6,6,0.1]),
	"axyz": np.array([0,0,0]),
	"axyz_sigma": np.array([3,3,0.02]),
	"n": 3500
}



DATA_DIR = "/home/dunbar/Research/helheim/data/observations"
observerpath = ['stardot2']
observers = []
for observer in observerpath:
    path = join(DATA_DIR,observer)
    campaths = glob(join(path,"*.JSON"))
    images = [glimpse.Image(path=campath.replace(".JSON",".jpg"),cam=glimpse.Camera.from_json(campath)) for campath in campaths]
    images.sort(key= lambda img: img.datetime)
    datetimes = np.array([img.datetime for img in images])
    for n, delta in enumerate(np.diff(datetimes)):
        if delta <= datetime.timedelta(seconds=0):
            secs = datetime.timedelta(seconds= n%5 + 1)
            images[n+1].datetime = images[n+1].datetime + secs
    diffs = np.array([dt.total_seconds() for dt in np.diff(np.array([img.datetime for img in images]))])
    negate = diffs[diffs <= 1].astype(np.int)
    [images.pop(_) for _ in negate]

   
images = np.array(images)
  
nearest_scan = np.searchsorted([laz.datetime for laz in lazfiles],images[0].datetime)
start = lazfiles[nearest_scan-5].datetime
예제 #6
0
from os.path import join
os.environ['OMP_NUM_THREADS'] = '1'
#==============================================

DATA_DIR = "/home/dunbar/Research/helheim/data/observations"
DEM_DIR = os.path.join(DATA_DIR, 'dem')
MAX_DEPTH = 30e3

# ---- Prepare Observers ----

observerpath = ['stardot1','stardot2']
observers = []
for observer in observerpath:
    path = join(DATA_DIR,observer)
    campaths =  glob.glob(join(path,"*.JSON"))
    images = [glimpse.Image(path=campath.replace(".JSON",".jpg"),cam=glimpse.Camera.from_json(campath)) for campath in campaths]
    images.sort(key= lambda img: img.datetime)
    datetimes = np.array([img.datetime for img in images])
    for n, delta in enumerate(np.diff(datetimes)):
        if delta <= datetime.timedelta(seconds=0):
            secs = datetime.timedelta(seconds= n%5 + 1)
            images[n+1].datetime = images[n+1].datetime + secs
    diffs = np.array([dt.total_seconds() for dt in np.diff(np.array([img.datetime for img in images]))])
    negate = diffs[diffs <= 1].astype(np.int)
    [images.pop(_) for _ in negate]

    images = images[:int(len(images)/2)]
    print("Image set {} \n".format(len(images)))
    obs = glimpse.Observer(list(np.array(images)),cache=False)
    observers.append(obs)
#-------------------------
예제 #7
0
 "f": [1793.9000490540586, 1338.3455527807855]
}







# --- Prepare anchor image ---




img = glimpse.Image(path=ANCHOR,cam=CAM_PARAMS)




# --- Build points control ---
'''
#cliffimgpts = np.array([[5193, 549],[3101, 642]])#,[6153.0, 2297.0]])
#cliffworldpts = np.array([[408245.86,6695847.03,1560 ],[416067.22,6707259.97,988]])#,[394569.509, 6695550.678, 621.075]])



points = glimpse.optimize.Points(

cam=img.cam,
예제 #8
0
os.environ['OMP_NUM_THREADS'] = '1'
#==============================================

DATA_DIR = "/home/dunbar/Research/helheim/data/observations/"
DEM_DIR = os.path.join(DATA_DIR, 'dem')
MAX_DEPTH = 30e3

# ---- Prepare Observers ----

observerpath = ['stardot1', 'stardot2']
observers = []
for observer in observerpath:
    path = join(DATA_DIR, observer)
    campaths = glob.glob(join(path, "*.JSON"))
    images = [
        glimpse.Image(path=campath.replace(".JSON", ".jpg"), cam=campath)
        for campath in campaths
    ]
    print("Image set {} \n".format(len(images)))
    images.sort(key=lambda img: img.datetime)
    datetimes = np.array([img.datetime for img in images])
    for n, delta in enumerate(np.diff(datetimes)):
        if delta <= datetime.timedelta(seconds=0):
            secs = datetime.timedelta(seconds=n % 5 + 1)
            images[n + 1].datetime = images[n + 1].datetime + secs
    diffs = np.array([
        dt.total_seconds()
        for dt in np.diff(np.array([img.datetime for img in images]))
    ])
    negate = diffs[diffs <= 1].astype(np.int)
    [images.pop(_) for _ in negate]
예제 #9
0
	def getImages(self):
		self.images = []
		[self.images.append( glimpse.Image(path=imagepath,exif=glimpse.Exif(imagepath),cam=self.refimg.cam.copy()) ) for imagepath in self.imagepaths[1:]]
		self.images.sort(key= lambda img: img.datetime)
		print("Found {} Images \n".format(len(self.images)))
예제 #10
0
os.environ['OMP_NUM_THREADS'] = '1'
#==============================================

DATA_DIR = "/home/dunbar/Research/helheim/data/observations"
DEM_DIR = os.path.join(DATA_DIR, 'dem')
MAX_DEPTH = 30e3

# ---- Prepare Observers ----

observerpath = ['stardot1', 'stardot2']
observers = []
for observer in observerpath:
    path = join(DATA_DIR, observer)
    campaths = glob.glob(join(path, "*.JSON"))
    images = [
        glimpse.Image(path=campath.replace(".JSON", ".jpg"),
                      cam=glimpse.camera.Camera.from_json(campath))
        for campath in campaths
    ]
    images.sort(key=lambda img: img.datetime)
    datetimes = np.array([img.datetime for img in images])
    for n, delta in enumerate(np.diff(datetimes)):
        if delta <= datetime.timedelta(seconds=0):
            secs = datetime.timedelta(seconds=n % 5 + 1)
            images[n + 1].datetime = images[n + 1].datetime + secs
    diffs = np.array([
        dt.total_seconds()
        for dt in np.diff(np.array([img.datetime for img in images]))
    ])
    negate = diffs[diffs <= 1].astype(np.int)
    [images.pop(_) for _ in negate]
예제 #11
0
def camera_svg_controls(camera,
                        size=1,
                        force_size=False,
                        keys=None,
                        svgs=None,
                        correction=True,
                        step=None,
                        station_calib=False,
                        camera_calib=False,
                        synth=True):
    """
    Return all SVG control objects available for a camera.

    Arguments:
        camera (str): Camera identifer
        size: Image scale factor (number) or image size in pixels (nx, ny)
        force_size (bool): Whether to force `size` even if different aspect ratio
            than original size.
        keys (iterable): SVG layers to include
        svgs (iterable): SVG basenames to include
        correction: Whether control objects should use elevation correction (bool)
            or arguments to `glimpse.helpers.elevation_corrections()`
        station_calib (bool): Whether to load station calibration. If `False`,
            falls back to the station estimate.
        camera_calib (bool): Whether to load camera calibrations. If `False`,
            falls back to the EXIF estimate.

    Returns:
        list: Image objects
        list: Control objects (Points, Lines)
        list: Per-camera calibration parameters [{'viewdir': True}, ...]
    """
    paths = glob.glob(os.path.join(CG_PATH, 'svg', '*.svg'))
    if synth:
        paths += glob.glob(os.path.join(CG_PATH, 'svg-synth', '*.pkl'))
        paths += glob.glob(os.path.join(CG_PATH, 'svg-synth', '*.svg'))
    basenames = np.unique([glimpse.helpers.strip_path(path) for path in paths])
    images, controls, cam_params = [], [], []
    for basename in basenames:
        ids = parse_image_path(basename, sequence=True)
        if ids['camera'] == camera:
            calibration = load_calibrations(basename,
                                            camera=camera_calib,
                                            station=station_calib,
                                            station_estimate=not station_calib,
                                            merge=True)
            img_path = find_image(basename)
            img = glimpse.Image(img_path, cam=calibration)
            control = []
            if svgs is None or basename in svgs:
                control += svg_controls(img,
                                        keys=keys,
                                        correction=correction,
                                        step=step)
            if synth:
                control += synth_controls(img, step=None, directions=False)
            if control:
                for x in control:
                    x.resize(size, force=force_size)
                images.append(img)
                controls.extend(control)
                cam_params.append(dict(viewdir=True))
    return images, controls, cam_params
예제 #12
0
def load_images(station,
                services,
                use_exif=False,
                service_exif=False,
                anchors=False,
                viewdir=True,
                viewdir_as_anchor=False,
                file_errors=True,
                **kwargs):
    """
    Return list of calibrated Image objects.

    Any available station, camera, image, and viewdir calibrations are loaded
    and images with image calibrations are marked as anchors.

    Arguments:
        station (str): Station identifier
        services (iterable): Service identifiers
        use_exif (bool): Whether to parse image datetimes from EXIF (slower)
            rather than parsed from paths (faster)
        service_exif (bool): Whether to extract EXIF from first image (faster)
            or all images (slower) in service.
            If `True`, `Image.datetime` is parsed from path.
            Always `False` if `use_exif=True`.
        anchors (bool): Whether to include anchor images even if
            filtered out by `kwargs['snap']`
        **kwargs: Arguments to `glimpse.helpers.select_datetimes()`
    """
    if use_exif:
        service_exif = False
    # Sort services in time
    if isinstance(services, str):
        services = services,
    services = np.sort(services)
    # Parse datetimes of all candidate images
    paths_service = [
        glob.glob(
            os.path.join(IMAGE_PATH, station, station + '_' + service,
                         '*.JPG')) for service in services
    ]
    paths = np.hstack(paths_service)
    basenames = [glimpse.helpers.strip_path(path) for path in paths]
    if use_exif:
        exifs = [glimpse.Exif(path) for path in paths]
        datetimes = np.array([exif.datetime for exif in exifs])
    else:
        datetimes = paths_to_datetimes(basenames)
    # Select images based on datetimes
    indices = glimpse.helpers.select_datetimes(datetimes, **kwargs)
    if anchors:
        # Add anchors
        # HACK: Ignore any <image>-<suffix>.json files
        anchor_paths = glob.glob(
            os.path.join(CG_PATH, 'images', station + '_*[0-9].json'))
        anchor_basenames = [
            glimpse.helpers.strip_path(path) for path in anchor_paths
        ]
        if 'start' in kwargs or 'end' in kwargs:
            # Filter by start, end
            anchor_datetimes = np.asarray(paths_to_datetimes(anchor_basenames))
            inrange = glimpse.helpers.select_datetimes(
                anchor_datetimes,
                **glimpse.helpers.merge_dicts(kwargs, dict(snap=None)))
            anchor_basenames = np.asarray(anchor_basenames)[inrange]
        anchor_indices = np.where(np.isin(basenames, anchor_basenames))[0]
        indices = np.unique(np.hstack((indices, anchor_indices)))
    service_breaks = np.hstack((0, np.cumsum([len(x) for x in paths_service])))
    station_calibration = load_calibrations(station_estimate=station,
                                            station=station,
                                            merge=True,
                                            file_errors=False)
    images = []
    for i, service in enumerate(services):
        index = indices[(indices >= service_breaks[i])
                        & (indices < service_breaks[i + 1])]
        if not index.size:
            continue
        service_calibration = glimpse.helpers.merge_dicts(
            station_calibration,
            load_calibrations(path=paths[index[0]],
                              camera=True,
                              merge=True,
                              file_errors=file_errors))
        if service_exif:
            exif = glimpse.Exif(paths[index[0]])
        for j in index:
            basename = basenames[j]
            calibrations = load_calibrations(
                image=basename,
                viewdir=basename if viewdir else False,
                station_estimate=station,
                merge=False,
                file_errors=False)
            if calibrations['image']:
                calibration = glimpse.helpers.merge_dicts(
                    service_calibration, calibrations['image'])
                anchor = True
            else:
                calibration = glimpse.helpers.merge_dicts(
                    service_calibration,
                    dict(viewdir=calibrations['station_estimate']['viewdir']))
                anchor = False
            if viewdir and calibrations['viewdir']:
                calibration = glimpse.helpers.merge_dicts(
                    calibration, calibrations['viewdir'])
                if viewdir_as_anchor:
                    anchor = True
            if use_exif:
                exif = exifs[j]
            elif not service_exif:
                exif = None
            if KEYPOINT_PATH:
                keypoint_path = os.path.join(KEYPOINT_PATH, basename + '.pkl')
            else:
                keypoint_path = None
            image = glimpse.Image(path=paths[j],
                                  cam=calibration,
                                  anchor=anchor,
                                  exif=exif,
                                  datetime=None if use_exif else datetimes[j],
                                  keypoints_path=keypoint_path)
            images.append(image)
    return images