def set_canvas_sizes(pixel_size_x, pixel_size_y): """Sets the canvases in the network to pixel size (square image)""" canvases = network.canvases for canvas in canvases: canvas.inputSize.dimensions.value = ivec2(pixel_size_x, pixel_size_y) inviwo_utils.update()
def main(pixel_dim, clip, num_random, plane): #Setup app = inviwopy.app network = app.network cam = network.EntryExitPoints.camera cam.nearPlane = 6.0 cam.farPlane = 1000.0 canvases = inviwopy.app.network.canvases for canvas in canvases: canvas.inputSize.dimensions.value = ivec2(pixel_dim, pixel_dim) inviwo_utils.update() random_lfs = create_random_lf_cameras( num_random, (180, 35), 1, interspatial_distance=0.5, look_up = vec3(0, 1, 0)) time_accumulator = (0.0, 0.0, 0.0) for lf in random_lfs: if clip: _, clip_type = random_clip_lf(network, lf) elif plane: random_plane_clip(network, lf) time_taken = lf.view_array(cam, save=False, should_time=True) time_accumulator = welford.update( time_accumulator, time_taken) if clip: restore_clip(network, clip_type) mean, variance, _ = welford.finalize(time_accumulator) print("Time taken per grid, average {:4f}, std_dev {:4f}".format( mean, math.sqrt(variance)))
def main(save_main_dir, pixel_dim, clip, num_random, plane): #Setup app = inviwopy.app network = app.network cam = network.EntryExitPoints.camera cam.nearPlane = 6.0 cam.farPlane = 1000.0 canvases = inviwopy.app.network.canvases for canvas in canvases: canvas.inputSize.dimensions.value = ivec2(pixel_dim, pixel_dim) inviwo_utils.update() if not os.path.isdir(save_main_dir): pathlib.Path(save_main_dir).mkdir(parents=True, exist_ok=True) #Save a number of random light fields random_lfs = create_random_lf_cameras(num_random, (180, 35), 1, interspatial_distance=0.5, look_up=vec3(0, 1, 0)) for lf in random_lfs: if clip: _, clip_type = random_clip_lf(network, lf) elif plane: random_plane_clip(network, lf) save_lf(lf, save_main_dir) if clip: restore_clip(network, clip_type)
def move_to_centre(self, cam=inviwopy.app.network.MeshClipping.camera): cam_ar = self.calculate_camera_array() central = ((self.spatial_rows // 2) + self.spatial_rows * (self.spatial_cols // 2)) (look_from, look_to) = cam_ar[central] cam.lookFrom = look_from cam.lookTo = look_to cam.lookUp = self.look_up inviwo_utils.update()
def main(save_dir, image_name, pixel_dim, tf_location): if not os.path.exists(save_dir): os.makedirs(save_dir) save_loc = os.path.join(save_dir, image_name) inviwopy.app.waitForPool() network = inviwopy.app.network canvases = network.canvases for canvas in canvases: canvas.inputSize.dimensions.value = ivec2(pixel_dim, pixel_dim) tf = network.VolumeRaycaster.isotfComposite.transferFunction tf.load(tf_location) # Update the network inviwo_utils.update() canvas.snapshot(save_loc) inviwopy.app.closeInviwoApplication() return 0
def generate_random_tfs(base_tf_names, network, num_random, should_view=False): ivw_tf = network.VolumeRaycaster.isotfComposite.transferFunction tf_values = [] if len(base_tf_names) > 0: for base_tf_name in base_tf_names: ivw_tf.load(base_tf_name) tf_values.append(ivw_tf.getValues()) for _ in range(num_random): tf_values.append(modify_tf(ivw_tf)) if should_view: inviwo_utils.update() sleep(0.2) else: tf_values.append(ivw_tf.getValues()) for _ in range(num_random): tf_values.append(modify_tf(ivw_tf)) if should_view: inviwo_utils.update() sleep(0.2) return tf_values
def main(pixel_dim): #Setup app = inviwopy.app network = app.network cam = network.EntryExitPoints.camera cam.lookUp = vec3(0, 1, 0) cam.nearPlane = 6.0 cam.farPlane = 1000.0 canvases = inviwopy.app.network.canvases for canvas in canvases: canvas.inputSize.dimensions.value = ivec2(pixel_dim, pixel_dim) inviwo_utils.update() # Create a light field camera at the current camera position lf_camera_here = LightFieldCamera(cam.lookFrom, cam.lookTo, cam.lookUp, interspatial_distance=0.5) #Preview the lf camera array lf_camera_here.view_array(cam, save=False, should_time=True)
def main(config): app = inviwopy.app network = app.network # Resize the canvas to improve rendering speed, only affects visual output if config["should_resize"]: ivw_helpers.set_canvas_sizes(128, 128) # Find the list of volumes and tfs form the corresponding directories volume_names = get_all_files_in_dir( config["volume_dir"], None, True) ivw_volume = get_volume(network, config) for volume_name in volume_names: if config["should_use_numpy_vol"]: ivw_volume.properties.location.value = volume_name else: ivw_volume.filename.value = volume_name ivw_volume.reload.press() inviwo_utils.update() sleep(1.2)
cam = network.EntryExitPoints.camera start = time.clock() scale = 1; d = 15 steps = 120 cam.lookTo = vec3(0,0,0) cam.lookUp = vec3(0,1,0) for i in range(0, steps): r = (2 * math.pi * i) / (steps-1) x = d*math.sin(r) z = -d*math.cos(r) cam.lookFrom = vec3(x*scale,3*scale,z*scale) inviwo_utils.update() # Needed for canvas to update for i in range(0, steps): r = (2 * math.pi * i) / (steps-1) x = 1.0*math.sin(r) z = 1.0*math.cos(r) cam.lookUp = vec3(x*scale,z*scale,0) inviwo_utils.update() # Needed for canvas to update end = time.clock() fps = 2*steps / (end - start) fps = round(fps,3) print("Frames per second: " + str(fps))
cam = network.EntryExitPoints.camera start = time.clock() scale = 1 d = 15 steps = 120 cam.lookTo = vec3(0, 0, 0) cam.lookUp = vec3(0, 1, 0) for i in range(0, steps): r = (2 * math.pi * i) / (steps - 1) x = d * math.sin(r) z = -d * math.cos(r) cam.lookFrom = vec3(x * scale, 3 * scale, z * scale) inviwo_utils.update() # Needed for canvas to update for i in range(0, steps): r = (2 * math.pi * i) / (steps - 1) x = 1.0 * math.sin(r) z = 1.0 * math.cos(r) cam.lookUp = vec3(x * scale, z * scale, 0) inviwo_utils.update() # Needed for canvas to update end = time.clock() fps = 2 * steps / (end - start) fps = round(fps, 3) print("Frames per second: " + str(fps)) print("Time per frame: " + str(round(1000 / fps, 1)) + " ms")
def save_looking_to_hdf5_group( sample_index, h5_canvas_list, camera, config): """Saves lf information to hdf5 group with images over columns then rows Keyword arguments: h5_canvas_list - tuples of groups and canvas numbers in a list for example (colour_group, 0) """ overall_start_time = time() cam = inviwopy.app.network.MeshClipping.camera accumulator_list = [] chs = config['channels'] # Gathers accumulators for welford mean and var for i, group_tuple in enumerate(h5_canvas_list): group = group_tuple[0] mean_shape = (1, ) + group["mean"].shape[1:] mean = np.zeros(mean_shape, np.float32) accumulator = (0, mean, 0) accumulator_list.append(accumulator) start_time = time() cam.lookFrom = camera[0] cam.lookTo = camera[1] cam.lookUp = camera[2] inviwo_utils.update() time_taken = time() - start_time sleep(1) for i, group_tuple in enumerate(h5_canvas_list): #assuming that there is two canvases group = group_tuple[0] canvas = ivw_helpers.get_canvas_by_id( inviwopy.app.network, group_tuple[1]) w_canvas = ivw_helpers.get_canvas_by_id( inviwopy.app.network, group_tuple[2]) assert (canvas.inputSize.customInputDimensions.value[0] // config["spatial_cols"] == group.attrs["lf_shape"][-1]), \ "canvas size x {} is not pixel dimension {} of h5".format( canvas.inputSize.customInputDimensions.value[0] // config["spatial_cols"], group.attrs["lf_shape"][-1] ) assert (canvas.inputSize.customInputDimensions.value[1] // config["spatial_rows"] == group.attrs["lf_shape"][-2]), \ "canvas size y {} is not pixel dimension {} of h5".format( canvas.inputSize.customInputDimensions.value[1] // config["spatial_rows"], group.attrs["lf_shape"][-2] ) total_im_data = ivw_helpers.get_image(canvas) group['camera_extrinsics'][sample_index] = \ [cam.lookFrom.x, cam.lookFrom.y, cam.lookFrom.z, cam.lookTo.x, cam.lookTo.y, cam.lookTo.z, cam.lookUp.x, cam.lookUp.y, cam.lookUp.z] # Inviwo stores data in a different indexing to regular # Store as descending y: C, H, W for idx in range(64): y_start = config["pixel_dim_y"] * (idx // 8) y_end = y_start + (config["pixel_dim_y"]) x_start = config["pixel_dim_x"] * (idx % 8) x_end = x_start + (config["pixel_dim_x"]) im_data = total_im_data[x_start:x_end, y_start:y_end] im_data = np.flipud(np.swapaxes(im_data, 0, 2)) im_data = im_data[::-1, ::-1, ...] group['images'][sample_index, idx] = im_data[:chs, ...] group['timing'][sample_index] = time_taken accumulator_list[i] = welford.update( accumulator_list[i], np.asarray(im_data[:chs, ...], dtype=np.float32)) total_im_data = ivw_helpers.get_image(w_canvas) # Inviwo stores data in a different indexing to regular # Store as descending y: C, H, W for idx in range(64): y_start = config["pixel_dim_y"] * (idx // 8) y_end = y_start + (config["pixel_dim_y"]) x_start = config["pixel_dim_x"] * (idx % 8) x_end = x_start + (config["pixel_dim_x"]) im_data = total_im_data[x_start:x_end, y_start:y_end] im_data = np.flipud(np.swapaxes(im_data, 0, 2)) im_data = im_data[::-1, ::-1, ...] group['warped'][sample_index, idx] = im_data[:chs, ...] for i, group_tuple in enumerate(h5_canvas_list): group = group_tuple[0] mean, var, _ = welford.finalize(accumulator_list[i]) group['mean'][sample_index, :, :, :] = mean group['var'][sample_index, :, :, :] = var print("Finished writing LF {0} in {1:.2f}".format( sample_index, time() - overall_start_time)) sleep(1)
def view_array(self, cam=inviwopy.app.network.MeshClipping.camera, save=False, save_dir=os.path.expanduser('~'), should_time=False): """Move the inviwo camera through the array for the current workspace. Keyword arguments: cam -- the camera to move through the light field array save -- save the images to png files (default False) save_dir -- the main directory to save the png images to (default home) """ if not os.path.isdir(save_dir): raise ValueError("save_dir is not a valid directory.") print("Viewing array for lf camera with:") print(self) # Save the current camera position prev_cam_look_from = cam.lookFrom prev_cam_look_to = cam.lookTo prev_cam_look_up = cam.lookUp cam.lookUp = self.look_up if should_time: start_time = time() for idx, val in enumerate(self.calculate_camera_array()): (look_from, look_to) = val cam.lookFrom = look_from cam.lookTo = look_to inviwo_utils.update() row_num, col_num = self.get_row_col_number(idx) if save: #Loop over canvases in the workspace canvases = inviwopy.app.network.canvases same_names = any([ (canvases[i].displayName == canvases[i + 1].displayName) for i in range(len(canvases) - 1) ]) #The identifier names could be different identifier_same_names = False if same_names: identifier_same_names = any([ (canvases[i].identifier == canvases[i + 1].identifier) for i in range(len(canvases) - 1) ]) for canvas_idx, canvas in enumerate(canvases): if same_names and identifier_same_names: file_name = ('Canvas_' + str(canvas_idx) + '_' + str(row_num) + str(col_num) + '.png') elif identifier_same_names: file_name = (canvas.displayName + str(row_num) + str(col_num) + '.png') else: file_name = (canvas.identifier + str(row_num) + str(col_num) + '.png') if 'Depth' in file_name: str_list = list(file_name) str_list[-4:] = list('.npy') file_name = ''.join(str_list) full_save_dir = os.path.abspath(save_dir) file_path = os.path.join(full_save_dir, file_name) print('Saving to: ' + file_path) np.save(file_path, np.flipud(np.transpose( canvas.image.depth.data)), fix_imports=False) else: full_save_dir = os.path.abspath(save_dir) file_path = os.path.join(full_save_dir, file_name) print('Saving to: ' + file_path) canvas.snapshot(file_path) else: #Smooths the viewing process if not should_time: print('Viewing position ({}, {})'.format(row_num, col_num)) sleep(0.1) canvas = inviwopy.app.network.canvases[0] pixel_dim = canvas.inputSize.dimensions.value[0] if save: metadata_filename = os.path.join(full_save_dir, 'metadata.csv') with open(metadata_filename, 'w') as f: self.print_metadata(cam, pixel_dim, f) # Reset the camera to original position time_taken = 0 if should_time: time_taken = time() - start_time print("Overall time taken to render grid was {:4f}".format( time_taken)) print() cam.lookFrom = prev_cam_look_from cam.lookTo = prev_cam_look_to cam.lookUp = prev_cam_look_up return time_taken
def save_to_hdf5_group(self, sample_index, h5_canvas_list, config): """Saves lf information to hdf5 group with images over columns then rows Keyword arguments: h5_canvas_list - tuples of groups and canvas numbers in a list for example (colour_group, 0) """ overall_start_time = time() cam = inviwopy.app.network.MeshClipping.camera cam.lookUp = self.look_up accumulator_list = [] # Gathers accumulators for welford mean and var for i, group_tuple in enumerate(h5_canvas_list): group = group_tuple[0] mean_shape = (1, ) + group["mean"].shape[1:] mean = np.zeros(mean_shape, np.float32) accumulator = (0, mean, 0) accumulator_list.append(accumulator) for idx, val in enumerate(self.calculate_camera_array()): (look_from, look_to) = val start_time = time() cam.lookFrom = look_from cam.lookTo = look_to inviwo_utils.update() time_taken = time() - start_time #row_num, col_num = self.get_row_col_number(idx) for i, group_tuple in enumerate(h5_canvas_list): #assuming that there is one canvas group = group_tuple[0] canvas = inviwopy.app.network.canvases[group_tuple[1]] im_data = ivw_helpers.get_image(canvas) # Inviwo stores data in a different indexing to regular im_data = np.flipud(np.swapaxes(im_data, 0, 2)) im_data = im_data[::-1, ::-1, ...] assert (canvas.inputSize.customInputDimensions.value[0] == group.attrs["lf_shape"][-1]), \ "canvas size x {} is not pixel dimension {} of h5".format( canvas.inputSize.customInputDimensions.value[0], group.attrs["lf_shape"][-1] ) assert (canvas.inputSize.customInputDimensions.value[1] == group.attrs["lf_shape"][-2]), \ "canvas size y {} is not pixel dimension {} of h5".format( canvas.inputSize.customInputDimensions.value[1], group.attrs["lf_shape"][-2] ) group['images'][sample_index, idx] = im_data group['timing'][sample_index, idx] = time_taken group['camera_extrinsics'][sample_index, idx] = \ [cam.lookFrom.x, cam.lookFrom.y, cam.lookFrom.z, cam.lookTo.x, cam.lookTo.y, cam.lookTo.z, cam.lookUp.x, cam.lookUp.y, cam.lookUp.z] accumulator_list[i] = welford.update( accumulator_list[i], np.asarray(im_data, dtype=np.float32)) for i, group_tuple in enumerate(h5_canvas_list): group = group_tuple[0] mean, var, _ = welford.finalize(accumulator_list[i]) group['mean'][sample_index, :, :, :] = mean group['var'][sample_index, :, :, :] = var print("Finished writing LF {0} in {1:.2f} to {2}".format( sample_index, time() - overall_start_time, h5_canvas_list[0][0].name))
def render_inviwo_frame(self): inviwo_utils.update() network = inviwopy.app.network canvas = network.canvases[0] im = canvas.image.colorLayers[0].data return im.copy()
#Inviwo Python script import inviwopy import ivw.utils as inviwo_utils from time import time app = inviwopy.app network = app.network cam = network.MeshClipping.camera lg = network.LookingGlassEntryExitPoints lg.individual_view.value = True start_time = time() for i in range(45): lg.view.value = i inviwo_utils.update() print(time() - start_time) start_time = time() lg.individual_view.value = False inviwo_utils.update() print(time() - start_time)
def render_inviwo_frame(self): inviwo_utils.update() network = inviwopy.app.network outport = network.VolumeRaycaster.getOutport("outport") return outport.getData().colorLayers[0].data[:, :, :3]