Source code for node_indexing

# This file is part of Sympathy for Data.
# Copyright (c) 2018, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.

import numpy as np

from sympathy.api import node
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
from sympathy.api.nodeconfig import adjust
from sympathy.api.exceptions import SyConfigurationError
from sympathy.api.exceptions import SyDataError
from sympathy.api.exceptions import NoDataError

from sylib.imageprocessing.image import ImagePort


[docs] class ImageIndexLookup(node.Node): author = 'Mathias Broxvall' icon = 'image_index_lookup.svg' description = ( 'Takes an integer image and perform a table lookup against a table\n' 'indices. It maps each integer value in the input image against the\n' 'row that has that index. For each non index column in the table it\n' 'creates one channel of output data with the value at that row.\n' 'colums in that row it creates one channel.\n\n' 'Can be used for palette lookup or for operating on superpixel ' 'labels.\n') tags = Tags(Tag.ImageProcessing.ImageManipulation) name = 'Image index lookup' nodeid = 'com.sympathyfordata.imageanalysis.image_index_lookup' __doc__ = description parameters = node.parameters() parameters.set_string( 'index column', value='', label='Index column', editor=node.editors.combo_editor(edit=False), description='Column names with indices to match') inputs = Ports([ ImagePort('Input image', name='labels'), Port.Table( 'Table with palette values, each column becomes one channel', name='palette'), ]) outputs = Ports([ ImagePort('Output image', name='output'), ]) def adjust_parameters(self, node_context): try: adjust(node_context.parameters['index column'], node_context.input['palette']) except NoDataError: pass def execute(self, node_context): labels = node_context.input['labels'].get_image() palette = node_context.input['palette'] index_column = node_context.parameters['index column'].value if index_column == '': raise SyConfigurationError( 'Parameter index column have not been configured') if index_column not in palette: raise SyDataError( 'Missing column "{}" in input data'.format(index_column)) labels = labels[:, :, 0] indices = palette[index_column] max_val = np.maximum(np.max(labels), np.max(indices)) min_val = np.minimum(np.min(labels), np.min(indices)) labels = labels - min_val indices = indices - min_val val_range = max_val - min_val + 1 im = None i = 0 for col in palette.cols(): if col.name == index_column: continue data = col.data mapping = np.zeros(val_range, dtype=data.dtype) mapping[indices] = data res = mapping[labels] if im is None: im = np.zeros(labels.shape + (len(palette.cols())-1,), dtype=res.dtype) im[:, :, i] = res i = i + 1 node_context.output['output'].set_image(im)