def loadBXDLutFromString(lut, ctf):
    """
	Load a BXD format lut from a given string
	"""
    lut = lut[6:]
    start, end = struct.unpack("ff", lut[0:8])
    lut = lut[8:]
    Logging.info("The palette is in range %d-%d" % (start, end), kw="ctf")
    j = 0

    # start = int(start)
    # end = int(end)

    handle = vtkbxd.vtkHandleColorTransferFunction()
    handle.SetInputString(lut, len(lut))
    handle.LoadColorTransferFunctionFromString(ctf, start, end)

    # k = ( len(lut) / 3 ) - 1
    # reds = lut[0: k + 1]
    # greens = lut[k + 1: 2 * k + 2]
    # blues = lut[(2 * k) + 2: 3 * k + 3]

    # j = 0
    # for i in range(start, end + 1):
    # 	red = ord(reds[j])
    # 	green = ord(greens[j])
    # 	blue = ord(blues[j])

    # 	red /= 255.0
    # 	green /= 255.0
    # 	blue /= 255.0
    # 	ctf.AddRGBPoint(i, red, green, blue)
    # 	j += 1

    return
예제 #2
0
def lutToString(ctf, luttype="ImageJ"):
    """
	Write a lut to a string
	"""
    stringOfLUT = ""
    if ctf is None:
        return stringOfLUT

    minval, maxval = ctf.GetRange()
    if luttype == "ImageJ":
        perColor = maxval / 255
    else:
        perColor = 1

    if luttype == "BioImageXD":
        stringOfLUT = "BXDLUT"
        Logging.info("Adding to BXDLUT structure the minval=%f, maxval=%f" %
                     (minval, maxval),
                     kw="ctf")

        stringOfLUT += struct.pack("f", minval)
        stringOfLUT += struct.pack("f", maxval)

    size = maxval - minval + 1
    if size < ctf.GetSize():
        size = ctf.GetSize()

    handle = vtkbxd.vtkHandleColorTransferFunction()
    handle.ColorTransferFunctionToString(ctf, int(perColor), int(size))
    ctfstr = handle.GetOutputString()
    for i in xrange(0, ctfstr.GetNumberOfTuples()):
        stringOfLUT += chr(ctfstr.GetValue(i))

    #for col in range(0, 3):
    #	for i in range(0, int(maxval) + 1, int(perColor)):
    #		val = [0, 0, 0]

    #		ctf.GetColor(i, val)
    #		red, green, blue = val
    #		red *= 255
    #		green *= 255
    #		blue *= 255
    #		red = int(red)
    #		green = int(green)
    #		blue = int(blue)
    #		if red < 0:
    #			red = 0
    #		if blue < 0:
    #			blue = 0
    #		if green < 0:
    #			green = 0
    #		if red >= 255:
    #			red = 255
    #		if green >= 255:
    #			green = 255
    #		if blue >= 255:
    #			blue = 255
    #		color = [red, green, blue]
    #		stringOfLUT += chr(color[col])
    return stringOfLUT
예제 #3
0
def loadBXDLutFromString(lut, ctf):
    """
	Load a BXD format lut from a given string
	"""
    lut = lut[6:]
    start, end = struct.unpack("ff", lut[0:8])
    lut = lut[8:]
    Logging.info("The palette is in range %d-%d" % (start, end), kw="ctf")
    j = 0

    #start = int(start)
    #end = int(end)

    handle = vtkbxd.vtkHandleColorTransferFunction()
    handle.SetInputString(lut, len(lut))
    handle.LoadColorTransferFunctionFromString(ctf, start, end)

    #k = ( len(lut) / 3 ) - 1
    #reds = lut[0: k + 1]
    #greens = lut[k + 1: 2 * k + 2]
    #blues = lut[(2 * k) + 2: 3 * k + 3]

    #j = 0
    #for i in range(start, end + 1):
    #	red = ord(reds[j])
    #	green = ord(greens[j])
    #	blue = ord(blues[j])

    #	red /= 255.0
    #	green /= 255.0
    #	blue /= 255.0
    #	ctf.AddRGBPoint(i, red, green, blue)
    #	j += 1

    return
def lutToString(ctf, luttype="ImageJ"):
    """
	Write a lut to a string
	"""
    stringOfLUT = ""
    if ctf is None:
        return stringOfLUT

    minval, maxval = ctf.GetRange()
    if luttype == "ImageJ":
        perColor = maxval / 255
    else:
        perColor = 1

    if luttype == "BioImageXD":
        stringOfLUT = "BXDLUT"
        Logging.info("Adding to BXDLUT structure the minval=%f, maxval=%f" % (minval, maxval), kw="ctf")

        stringOfLUT += struct.pack("f", minval)
        stringOfLUT += struct.pack("f", maxval)

    size = maxval - minval + 1
    if size < ctf.GetSize():
        size = ctf.GetSize()

    handle = vtkbxd.vtkHandleColorTransferFunction()
    handle.ColorTransferFunctionToString(ctf, int(perColor), int(size))
    ctfstr = handle.GetOutputString()
    for i in xrange(0, ctfstr.GetNumberOfTuples()):
        stringOfLUT += chr(ctfstr.GetValue(i))

        # for col in range(0, 3):
        # 	for i in range(0, int(maxval) + 1, int(perColor)):
        # 		val = [0, 0, 0]

        # 		ctf.GetColor(i, val)
        # 		red, green, blue = val
        # 		red *= 255
        # 		green *= 255
        # 		blue *= 255
        # 		red = int(red)
        # 		green = int(green)
        # 		blue = int(blue)
        # 		if red < 0:
        # 			red = 0
        # 		if blue < 0:
        # 			blue = 0
        # 		if green < 0:
        # 			green = 0
        # 		if red >= 255:
        # 			red = 255
        # 		if green >= 255:
        # 			green = 255
        # 		if blue >= 255:
        # 			blue = 255
        # 		color = [red, green, blue]
        # 		stringOfLUT += chr(color[col])
    return stringOfLUT
예제 #5
0
    def execute(self, inputs, update=0, last=0):
        """
		Execute the filter with given inputs and return the output
		"""
        if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs):
            return None

        bitDepth = 8
        minVal = 0.0
        maxVal = 255.0
        image = self.getInput(1)
        self.vtkfilter.SetInput(image)
        self.vtkfilter.SetClampOverflow(self.parameters["ClampOverflow"])
        output = self.vtkfilter.GetOutput()

        for param in [
                "Float", "Double", "Int", "UnsignedInt", "Long",
                "UnsignedLong", "Short", "UnsignedShort", "Char",
                "UnsignedChar"
        ]:
            if self.parameters[param]:
                eval("self.vtkfilter.SetOutputScalarTypeTo%s()" % param)
                output.Update()
                minVal, maxVal = output.GetScalarRange()

                self.eventDesc = "Casting the image to datatype %s" % param
                if param in ["Short", "UnsignedShort"]:
                    bitDepth = 16
                if param in ["Int", "UnsignedInt", "Float"]:
                    bitDepth = 32
                if param in ["Double", "Long", "UnsignedLong"]:
                    bitDepth = 64

        settings = self.dataUnit.getSettings()
        settings.set("BitDepth", bitDepth)

        if self.parameters["RescaleCTF"]:
            ctf = settings.get("ColorTransferFunction")
            scaledCtf = vtk.vtkColorTransferFunction()
            handleCtf = vtkbxd.vtkHandleColorTransferFunction()
            handleCtf.ScaleColorTransferFunction(ctf, scaledCtf, minVal,
                                                 maxVal)
            settings.set("ColorTransferFunction", scaledCtf)

        #if update:
        self.vtkfilter.Update()
        return self.vtkfilter.GetOutput()
예제 #6
0
def watershedPalette(ctfLowerBound,
                     ctfUpperBound,
                     ignoreColors=2,
                     filename=""):
    """
	Returns a randomly created CTF.
	"""
    try:
        ctf = vtk.vtkColorTransferFunction()
        if filename:
            loadLUT(filename, ctf)
    except:
        ctf = vtk.vtkColorTransferFunction()

    ctfFileSize = ctf.GetSize()
    if ctfFileSize > 0:
        for i in range(0, ignoreColors):
            color = ctf.GetColor(i)
            ctf.AddRGBPoint(ctf.GetSize(), color[0], color[1], color[2])

    for i in range(0, ignoreColors):
        ctf.AddRGBPoint(i, 0, 0, 0)

    if ctfLowerBound < 1:
        ctfLowerBound = 1

    if ctfFileSize > 0 and ctf.GetSize() > ctfLowerBound:
        ctfLowerBound = ctf.GetSize()

    handle = vtkbxd.vtkHandleColorTransferFunction()
    handle.CreateRandomColorTransferFunction(ctf, ctfLowerBound, ctfUpperBound,
                                             1.5)
    #for i in range(int(ctfLowerBound), int(ctfUpperBound)):
    #	red = 0
    #	green = 0
    #	blue = 0
    #	while red + green + blue < 1.5:
    #		red = random.random()
    #		green = random.random()
    #		blue = random.random()
    #	ctf.AddRGBPoint(float(i), float(red), float(green), float(blue))

    if ctf.GetSize() > ctfUpperBound:
        for i in xrange(ctfUpperBound, ctf.GetSize() - 1):
            ctf.RemovePoint(i)

    return ctf
def watershedPalette(ctfLowerBound, ctfUpperBound, ignoreColors=2, filename=""):
    """
	Returns a randomly created CTF.
	"""
    try:
        ctf = vtk.vtkColorTransferFunction()
        if filename:
            loadLUT(filename, ctf)
    except:
        ctf = vtk.vtkColorTransferFunction()

    ctfFileSize = ctf.GetSize()
    if ctfFileSize > 0:
        for i in range(0, ignoreColors):
            color = ctf.GetColor(i)
            ctf.AddRGBPoint(ctf.GetSize(), color[0], color[1], color[2])

    for i in range(0, ignoreColors):
        ctf.AddRGBPoint(i, 0, 0, 0)

    if ctfLowerBound < 1:
        ctfLowerBound = 1

    if ctfFileSize > 0 and ctf.GetSize() > ctfLowerBound:
        ctfLowerBound = ctf.GetSize()

    handle = vtkbxd.vtkHandleColorTransferFunction()
    handle.CreateRandomColorTransferFunction(ctf, ctfLowerBound, ctfUpperBound, 1.5)
    # for i in range(int(ctfLowerBound), int(ctfUpperBound)):
    # 	red = 0
    # 	green = 0
    # 	blue = 0
    # 	while red + green + blue < 1.5:
    # 		red = random.random()
    # 		green = random.random()
    # 		blue = random.random()
    # 	ctf.AddRGBPoint(float(i), float(red), float(green), float(blue))

    if ctf.GetSize() > ctfUpperBound:
        for i in xrange(ctfUpperBound, ctf.GetSize() - 1):
            ctf.RemovePoint(i)

    return ctf
	def execute(self, inputs, update = 0, last = 0):
		"""
		Execute the filter with given inputs and return the output
		"""			   
		if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs):
			return None

		bitDepth = 8
		minVal = 0.0
		maxVal = 255.0
		image = self.getInput(1)
		self.vtkfilter.SetInput(image)
		self.vtkfilter.SetClampOverflow(self.parameters["ClampOverflow"])
		output = self.vtkfilter.GetOutput()

		for param in ["Float","Double","Int","UnsignedInt","Long","UnsignedLong","Short","UnsignedShort","Char","UnsignedChar"]:
			if self.parameters[param]:
				eval("self.vtkfilter.SetOutputScalarTypeTo%s()"%param)
				output.Update()
				minVal,maxVal = output.GetScalarRange()
				
				self.eventDesc = "Casting the image to datatype %s"%param
				if param in ["Short", "UnsignedShort"]:
					bitDepth = 16
				if param in ["Int", "UnsignedInt", "Float"]:
					bitDepth = 32
				if param in ["Double", "Long", "UnsignedLong"]:
					bitDepth = 64

		settings = self.dataUnit.getSettings()
		settings.set("BitDepth", bitDepth)

		if self.parameters["RescaleCTF"]:
			ctf = settings.get("ColorTransferFunction")
			scaledCtf = vtk.vtkColorTransferFunction()
			handleCtf = vtkbxd.vtkHandleColorTransferFunction()
			handleCtf.ScaleColorTransferFunction(ctf,scaledCtf,minVal,maxVal)
			settings.set("ColorTransferFunction",scaledCtf)

		#if update:
		self.vtkfilter.Update()
		return self.vtkfilter.GetOutput()