def __init__(self, viewer: LayeredImageViewer, config: UnitedConfig): super().__init__(viewer, config) self.mode = Mode.SHOW self.radius = DEFAULT_RADIUS self.paint_central_pixel_cluster = True self.paint_dark_cluster = False self.paint_connected_component = True layers_properties = self.config.value('layers') self.mask_palette = Palette.from_names_rows_dict( layers_properties['mask']['palette']) self.mask_background_class = self.mask_palette.row_index_by_name( 'background') self.mask_foreground_class = self.mask_palette.row_index_by_name( 'foreground') self.tool_mask_palette = Palette.from_names_rows_dict( layers_properties['tool_mask']['palette']) self.tool_background_class = self.tool_mask_palette.row_index_by_name( 'background') self.tool_foreground_class = self.tool_mask_palette.row_index_by_name( 'foreground') self.tool_eraser_class = self.tool_mask_palette.row_index_by_name( 'eraser') self.tool_unconnected_component_class = self.tool_mask_palette.row_index_by_name( 'unconnected_component')
def create_colored_image_1(self): active_sub_window = self.mdi.activeSubWindow() if not isinstance(active_sub_window, LayeredImageViewerSubWindow): return layered_image = active_sub_window.viewer.data layered_image.print_layers() image = layered_image.layer_by_name('series').image print('stat', image.array.shape, image.array.min(), image.array.max(), image.array.dtype) print(np.unique(image.array)) norm = (image.array / image.array.max() * 255).astype(np.uint8) print('norm stat', norm.shape, norm.min(), norm.max(), norm.dtype) import colorsys palette_array = np.zeros((256, 4), dtype=np.uint8) for i in range(256): print('hsv', i, 60, 80) hsv_decimal = (i / 360, 60 / 100, 80 / 100) rgb_decimal = colorsys.hsv_to_rgb(*hsv_decimal) rgb = tuple(round(i * 255) for i in rgb_decimal) palette_array[i] = [*rgb, 255] print('res conv', palette_array[i]) palette = Palette(palette_array) indexed_image = VolumeImage(norm, palette, spatial=image.spatial) layered_image.add_layer_from_image(indexed_image, 'colored')
def show_chart(self): color_transfer_function = ColorTransferFunction() color_transfer_function.add_point_from_x_color( 0, np.array([255, 0, 0, 255])) color_transfer_function.add_point_from_x_color( 5, np.array([0, 255, 0, 50])) color_transfer_function.add_point_from_x_color( 15, np.array([0, 255, 0, 255])) color_transfer_function.add_point_from_x_color( 20, np.array([0, 0, 255, 255])) self.w = ColorTransferFunctionViewer(color_transfer_function) self.w.show() self.w.setGeometry(800, 500, 900, 300) # Create Palette from ColorTransferFunction from scipy import interpolate xp = [point.x for point in color_transfer_function.points] fp = [point.color_array for point in color_transfer_function.points] interpolator = interpolate.interp1d(xp, fp, axis=0, assume_sorted=True) result = interpolator(np.linspace(0, 20)) print('res', result.shape, '\n', result) palette_array = np.array(result.round(), dtype=np.uint8) print('rounded', palette_array) palette = Palette(palette_array)
def create_colored_image(self): active_sub_window = self.mdi.activeSubWindow() if not isinstance(active_sub_window, LayeredImageViewerSubWindow): return layered_image = active_sub_window.viewer.data layered_image.print_layers() image = layered_image.layer_by_name('series').image print('stat', image.array.shape, image.array.min(), image.array.max(), image.array.dtype) print(np.unique(image.array)) discrete_color_len = 2000 norm = (image.array / image.array.max() * (discrete_color_len - 1)).astype(np.int) print('norm stat', norm.shape, norm.min(), norm.max(), norm.dtype) color_transfer_function = ColorTransferFunction() # color_transfer_function.add_point_from_x_color(0, np.array([255, 76, 76, 255])) color_transfer_function.add_point_from_x_color( 0, np.array([0, 0, 0, 255])) color_transfer_function.add_point_from_x_color( 0.128 * discrete_color_len, np.array([255, 211, 98, 255])) color_transfer_function.add_point_from_x_color( 0.16 * discrete_color_len, np.array([152, 255, 108, 255])) color_transfer_function.add_point_from_x_color( 0.20 * discrete_color_len, np.array([8, 255, 144, 255])) color_transfer_function.add_point_from_x_color( 0.23 * discrete_color_len, np.array([221, 235, 255, 255])) color_transfer_function.add_point_from_x_color( 0.25 * discrete_color_len, np.array([119, 196, 255, 255])) color_transfer_function.add_point_from_x_color( 0.32 * discrete_color_len, np.array([225, 135, 255, 255])) color_transfer_function.add_point_from_x_color( 0.5 * discrete_color_len, np.array([120, 120, 255, 255])) color_transfer_function.add_point_from_x_color( discrete_color_len - 1, np.array([0, 0, 255, 255])) self.w = ColorTransferFunctionViewer(color_transfer_function) self.w.show() self.w.setGeometry(800, 500, 900, 300) # Create Palette from ColorTransferFunction from scipy import interpolate xp = [point.x for point in color_transfer_function.points] fp = [point.color_array for point in color_transfer_function.points] interpolator = interpolate.interp1d(xp, fp, axis=0, assume_sorted=True) result = interpolator(np.arange(discrete_color_len)) print('res', result.shape, '\n', result) palette_array = np.array(result.round(), dtype=np.uint8) print('rounded', palette_array) palette = Palette(palette_array) indexed_image = VolumeImage(norm, palette, spatial=image.spatial) layered_image.add_layer_from_image(indexed_image, 'colored')
def color_transfer_function_to_palette( color_transfer_function: ColorTransferFunction) -> Palette: xp = [point.x for point in color_transfer_function.points] fp = [point.color_array for point in color_transfer_function.points] interpolator = interpolate.interp1d(xp, fp, axis=0, assume_sorted=True) color_transfer_function_max_x = color_transfer_function.points[-1].x interpolated_colors_array = interpolator( np.arange(color_transfer_function_max_x + 1)) palette_array = np.array(interpolated_colors_array.round(), dtype=np.uint8) return Palette(palette_array)
def create_colored_image_3(self): active_sub_window = self.mdi.activeSubWindow() if not isinstance(active_sub_window, LayeredImageViewerSubWindow): return layered_image = active_sub_window.viewer.data layered_image.print_layers() image = layered_image.layer_by_name('series').image print('stat', image.array.shape, image.array.min(), image.array.max(), image.array.dtype) print(np.unique(image.array)) discrete_color_len = 2000 norm = (image.array / image.array.max() * (discrete_color_len - 1)).astype(np.int) print('norm stat', norm.shape, norm.min(), norm.max(), norm.dtype) import colorsys palette_array = np.zeros((discrete_color_len, 4), dtype=np.uint8) h_min = 0 h_max = 360 s_min = 70 s_max = 100 b = 80 h_range = h_max - h_min s_range = s_max - s_min color_range = h_range * s_range color_delta = color_range / discrete_color_len for i in range(discrete_color_len): v = color_delta * i h = h_min + (v // s_range) s = s_min + (v % s_range) # h = h_max - h print('i', i, ' hsv', h, s, b) hsv_decimal = (h / 360, s / 100, b / 100) rgb_decimal = colorsys.hsv_to_rgb(*hsv_decimal) rgb = tuple(round(c * 255) for c in rgb_decimal) # palette_array[discrete_color_len - i - 1] = [*rgb, 255] palette_array[i] = [*rgb, 255] print('res conv', palette_array[i]) palette = Palette(palette_array) indexed_image = VolumeImage(norm, palette, spatial=image.spatial) layered_image.add_layer_from_image(indexed_image, 'colored')
def create_colored_image_2(self): active_sub_window = self.mdi.activeSubWindow() if not isinstance(active_sub_window, LayeredImageViewerSubWindow): return layered_image = active_sub_window.viewer.data layered_image.print_layers() image = layered_image.layer_by_name('series').image print('stat', image.array.shape, image.array.min(), image.array.max(), image.array.dtype) print(np.unique(image.array)) norm = (image.array / image.array.max() * 511).astype(np.int) print('norm stat', norm.shape, norm.min(), norm.max(), norm.dtype) import colorsys palette_array = np.zeros((512, 4), dtype=np.uint8) h = 0 b = 80 i = 0 while i < 512: for s in range(60, 100): print('i', i, ' hsv', h, s, b) hsv_decimal = (h / 360, s / 100, b / 100) rgb_decimal = colorsys.hsv_to_rgb(*hsv_decimal) rgb = tuple(round(c * 255) for c in rgb_decimal) palette_array[i] = [*rgb, 255] print('res conv', palette_array[i]) i += 1 if i >= 512: break h += 24 palette = Palette(palette_array) indexed_image = VolumeImage(norm, palette, spatial=image.spatial) layered_image.add_layer_from_image(indexed_image, 'colored')
def overlay_sibling_dirs_images( self, data: Data, data_viewer_sub_windows: DataViewerSubWindow): if isinstance(data, LayeredImage): first_layer = data.layers[0] first_layer_image_name = first_layer.image_path.name layers_dir = first_layer.path.parent for new_layer_name, layer_properties in self.config_data[ 'layers'].items(): new_layer_image_path = layers_dir / new_layer_name / first_layer_image_name if new_layer_image_path.exists(): palette_property = layer_properties.get('palette') palette = palette_property and Palette.from_sparse_index_list( list(palette_property)) new_image = self.loading_manager.load_file( new_layer_image_path, palette=palette) new_image_layer = data.add_layer_from_image( new_image, new_layer_name) layer_opacity = layer_properties['opacity'] for data_viewer_sub_window in data_viewer_sub_windows: layered_image_viewer = data_viewer_sub_window.viewer layered_image_viewer.layer_view_by_model( new_image_layer).opacity = layer_opacity