Source code for node_colourspace
# This file is part of Sympathy for Data.
# Copyright (c) 2017, 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, exceptions
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
from sylib.imageprocessing.image import ImagePort
from sylib.imageprocessing.utility import table_to_image
[docs]
class GenericColorSpaceConversion(node.Node):
"""
Convert from one color space into another.
This is done by multiplying each pixel as a column vector
with the given input matrix (table). Number of columns of table must match
number of channels in input image. Number of rows will determine the
number of output channels
"""
name = 'Generic Color Space Conversion'
author = 'Mathias Broxvall'
icon = 'image_colorspace.svg'
description = ('Convert from one color space into another. This is done '
'by multiplying each pixel as a column vector with the '
'given input matrix (table). Number of columns of table '
'must match number of channels in input image. Number of '
'rows will determine the number of output channels ')
nodeid = 'com.sympathyfordata.imageanalysis.generic_colorspace_conversion'
tags = Tags(Tag.ImageProcessing.ImageManipulation)
parameters = node.parameters()
inputs = Ports([
ImagePort('image to perform colorspace conversion on',
name='image'),
Port.Custom('table', 'Table specifying conversion matrix',
name='table'),
])
outputs = Ports([
ImagePort('result', name='result'),
])
def execute(self, node_context):
image = node_context.input['image'].get_image()
table = node_context.input['table']
if int(image.sum()) == 0 or table.number_of_rows() == 0:
raise exceptions.SyDataError("Empty table")
kernel = table_to_image(table)
rows = kernel.shape[0]
im = np.zeros(image.shape[:2]+(rows,))
for im_ch in range(rows):
im[:, :, im_ch] = np.dot(image, kernel[im_ch, :])
node_context.output['result'].set_image(im)