Beispiel #1
0
class OverlayItem(object):
    """
    A Item that holds some scalar or multichannel _data and their drawing related settings.
    OverlayItems are held by the OverlayMgr
    """
    def __init__(self,
                 data,
                 color=0,
                 alpha=0.4,
                 colorTable=None,
                 autoAdd=False,
                 autoVisible=False,
                 linkColorTable=False,
                 autoAlphaChannel=True,
                 min=None,
                 max=None):
        #whether this overlay can be displayed in 3D using
        #extraction of meshes
        self.displayable3D = False
        #if this overlay can be shown in 3D, the list of labels
        #that should be suppressed (not shown)
        self.backgroundClasses = set()
        self.smooth3D = True

        self._data = DataAccessor(data)
        self.linkColorTable = linkColorTable
        self.colorTable = colorTable
        self.defaultColor = color
        self.linkColor = False
        self.colorGetter = None
        self.colorGetterArguments = None
        self.alpha = alpha
        self.autoAlphaChannel = autoAlphaChannel
        self.channel = 0
        self.name = "Unnamed Overlay"
        self.key = "Unknown Key"
        self.autoAdd = autoAdd
        self.autoVisible = autoVisible
        self.references = []
        self.min = min
        self.max = max
        self.overlayMgr = None

    def __getitem__(self, args):
        return self._data[args]

    def __setitem__(self, args, data):
        self._data[args] = data

    def __getattr__(self, name):
        if name == "color":
            return self.getColor()
        elif name == "dtype":
            return self._data.dtype
        elif name == "shape":
            return self._data.shape
        elif name == "dataItemImage":
            return self.overlayMgr.dataItem
        else:
            raise AttributeError, name

    def getColorTab(self):
        return self.colorTable

    def getSubSlice(self, offsets, sizes, num, axis, time=0, channel=0):
        return self._data.getSubSlice(offsets, sizes, num, axis, time, channel)

    def setSubSlice(self, offsets, data, num, axis, time=0, channel=0):
        self._data.setSubSlice(offsets, data, num, axis, time, channel)

    def getColor(self):
        if self.linkColor is False:
            return self.defaultColor
        else:
            return self.colorGetter()

    def setColorGetter(self, colorGetter, colorGetterArguments):
        self.colorGetter = colorGetter
        self.colorGetterArguments = colorGetterArguments
        self.linkColor = True

    def getRef(self):
        ref = OverlayItemReference(self)
        ref.visible = self.autoVisible
        self.references.append(ref)
        return ref

    def remove(self):
        for r in self.references:
            r.remove()
        self.references = []
        self._data = None

    def changeKey(self, newKey):
        if self.overlayMgr is not None:
            self.overlayMgr.changeKey(self.key, newKey)

    def setData(self, data):
        self.overlayItem._data = data

    @classmethod
    def normalizeForDisplay(cls, data):
        import numpy
        dmin = numpy.min(data)
        data = data - dmin
        dmax = numpy.max(data)
        data = 255 * data / dmax
        return data

    @classmethod
    def qrgb(cls, r, g, b):
        return long(0xff << 24) | ((r & 0xff) << 16) | (
            (g & 0xff) << 8) | (b & 0xff)

    @classmethod
    def qgray(cls, r, g, b):
        return (r * 11 + g * 16 + b * 5) / 32

    @classmethod
    def createDefaultColorTable(cls,
                                type,
                                levels=256,
                                transparentValues=set()):
        typeCap = type.capitalize()
        colorTab = []
        if (typeCap == "GRAY"):
            for i in range(levels):
                if i in transparentValues:
                    colorTab.append(0L)
                else:
                    colorTab.append(OverlayItem.qgray(
                        i, i, i))  # see qGray function in QtGui
        else:
            #RGB
            import numpy
            from ilastik.core.randomSeed import RandomSeed
            seed = RandomSeed.getRandomSeed()
            if seed is not None:
                numpy.random.seed(seed)
            for i in range(levels):
                if i in transparentValues:
                    colorTab.append(0L)
                else:
                    colorTab.append(
                        OverlayItem.qrgb(
                            numpy.random.randint(255),
                            numpy.random.randint(255), numpy.random.randint(
                                255)))  # see gRGB function in QtGui
        return colorTab

    @classmethod
    # IMPORTANT: BE AWARE THAT CHANGING THE COLOR TABLE MAY AFFECT TESTS THAT WORK WITH GROUND TRUTH
    # DATA FROM EXPORTED OVERLAYS. TYPICALLY, ONLY THE DATA AND NOT THE COLOR TABLE OF AN OVERLAY IS
    # COMPARED BUT BETTER MAKE SURE THAT THIS IS INDEED THE CASE.
    def createDefault16ColorColorTable(cls):
        sublist = []
        sublist.append(OverlayItem.qrgb(69, 69, 69))  # dark grey
        sublist.append(OverlayItem.qrgb(255, 0, 0))
        sublist.append(OverlayItem.qrgb(0, 255, 0))
        sublist.append(OverlayItem.qrgb(0, 0, 255))

        sublist.append(OverlayItem.qrgb(255, 255, 0))
        sublist.append(OverlayItem.qrgb(0, 255, 255))
        sublist.append(OverlayItem.qrgb(255, 0, 255))
        sublist.append(OverlayItem.qrgb(255, 105, 180))  #hot pink!

        sublist.append(OverlayItem.qrgb(102, 205, 170))  #dark aquamarine
        sublist.append(OverlayItem.qrgb(165, 42, 42))  #brown
        sublist.append(OverlayItem.qrgb(0, 0, 128))  #navy
        sublist.append(OverlayItem.qrgb(255, 165, 0))  #orange

        sublist.append(OverlayItem.qrgb(173, 255, 47))  #green-yellow
        sublist.append(OverlayItem.qrgb(128, 0, 128))  #purple
        sublist.append(OverlayItem.qrgb(192, 192, 192))  #silver
        sublist.append(OverlayItem.qrgb(240, 230, 140))  #khaki
        colorlist = []
        colorlist.append(long(0))
        colorlist.extend(sublist)

        import numpy
        from ilastik.core.randomSeed import RandomSeed
        seed = RandomSeed.getRandomSeed()
        if seed is not None:
            numpy.random.seed(seed)
        for i in range(17, 256):
            color = OverlayItem.qrgb(numpy.random.randint(255),
                                     numpy.random.randint(255),
                                     numpy.random.randint(255))
            colorlist.append(color)

        return colorlist
Beispiel #2
0
class OverlayItem(object):
    """
    A Item that holds some scalar or multichannel _data and their drawing related settings.
    OverlayItems are held by the OverlayMgr
    """
    def __init__(self, data, color = 0, alpha = 0.4, colorTable = None, autoAdd = False, autoVisible = False,  linkColorTable = False, autoAlphaChannel = True, min = None, max = None):
        #whether this overlay can be displayed in 3D using
        #extraction of meshes
        self.displayable3D = False
        #if this overlay can be shown in 3D, the list of labels
        #that should be suppressed (not shown)
        self.backgroundClasses = set()
        self.smooth3D = True
        
        self._data = DataAccessor(data)
        self.linkColorTable = linkColorTable
        self.colorTable = colorTable
        self.defaultColor = color
        self.linkColor = False
        self.colorGetter = None
        self.colorGetterArguments = None
        self.alpha = alpha
        self.autoAlphaChannel = autoAlphaChannel
        self.channel = 0
        self.name = "Unnamed Overlay"
        self.key = "Unknown Key"
        self.autoAdd = autoAdd
        self.autoVisible = autoVisible
        self.references = []
        self.min = min
        self.max = max
        self.overlayMgr = None

    def __getitem__(self, args):
        return self._data[args]
            
    def __setitem__(self, args, data):
        self._data[args] = data

    def __getattr__(self, name):
        if name == "color":
            return self.getColor()
        elif name == "dtype":
            return self._data.dtype
        elif name == "shape":
            return self._data.shape
        elif name == "dataItemImage":
            return self.overlayMgr.dataItem
        else:
            raise AttributeError, name

    def getColorTab(self):
        return self.colorTable
    
    def getSubSlice(self, offsets, sizes, num, axis, time = 0, channel = 0):
        return self._data.getSubSlice(offsets, sizes, num, axis, time, channel)

        
    def setSubSlice(self, offsets, data, num, axis, time = 0, channel = 0):
        self._data.setSubSlice(offsets, data, num, axis, time, channel)
                          
    
    def getColor(self):
        if self.linkColor is False:
            return self.defaultColor
        else:
            return self.colorGetter()
                          
    
    def setColorGetter(self,colorGetter, colorGetterArguments):
        self.colorGetter = colorGetter
        self.colorGetterArguments = colorGetterArguments
        self.linkColor = True
    
    
    def getRef(self):
        ref = OverlayItemReference(self)
        ref.visible = self.autoVisible
        self.references.append(ref)
        return ref
        
    def remove(self):
        for r in self.references:
            r.remove()
        self.references = []
        self._data = None


    def changeKey(self, newKey):
        if self.overlayMgr is not None:
            self.overlayMgr.changeKey(self.key, newKey)
        
    def setData(self,  data):
        self.overlayItem._data = data

    @classmethod
    def normalizeForDisplay(cls, data):
        import numpy
        dmin = numpy.min(data)
        data = data - dmin
        dmax = numpy.max(data)
        data = 255*data/dmax
        return data

    @classmethod
    def qrgb(cls, r, g, b):
        return long(0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)
    
    @classmethod
    def qgray(cls, r, g, b):
        return (r*11+g*16+b*5)/32
    
    @classmethod
    def createDefaultColorTable(cls, type, levels = 256, transparentValues = set()):
        typeCap = type.capitalize()
        colorTab = []
        if(typeCap == "GRAY"):
            for i in range(levels):
                if i in transparentValues:
                    colorTab.append(0L)
                else:
                    colorTab.append(OverlayItem.qgray(i, i, i)) # see qGray function in QtGui
        else:
            #RGB
            import numpy
            from ilastik.core.randomSeed import RandomSeed
            seed = RandomSeed.getRandomSeed()
            if seed is not None:
                numpy.random.seed(seed)
            for i in range(levels):
                if i in transparentValues:
                    colorTab.append(0L)
                else:
                    colorTab.append(OverlayItem.qrgb(numpy.random.randint(255),numpy.random.randint(255),numpy.random.randint(255))) # see gRGB function in QtGui
        return colorTab        
    
    @classmethod
    # IMPORTANT: BE AWARE THAT CHANGING THE COLOR TABLE MAY AFFECT TESTS THAT WORK WITH GROUND TRUTH 
    # DATA FROM EXPORTED OVERLAYS. TYPICALLY, ONLY THE DATA AND NOT THE COLOR TABLE OF AN OVERLAY IS
    # COMPARED BUT BETTER MAKE SURE THAT THIS IS INDEED THE CASE.
    def createDefault16ColorColorTable(cls):
        sublist = []
        sublist.append(OverlayItem.qrgb(69, 69, 69)) # dark grey
        sublist.append(OverlayItem.qrgb(255, 0, 0))
        sublist.append(OverlayItem.qrgb(0, 255, 0))
        sublist.append(OverlayItem.qrgb(0, 0, 255))
        
        sublist.append(OverlayItem.qrgb(255, 255, 0))
        sublist.append(OverlayItem.qrgb(0, 255, 255))
        sublist.append(OverlayItem.qrgb(255, 0, 255))
        sublist.append(OverlayItem.qrgb(255, 105, 180)) #hot pink!
        
        sublist.append(OverlayItem.qrgb(102, 205, 170)) #dark aquamarine
        sublist.append(OverlayItem.qrgb(165,  42,  42)) #brown        
        sublist.append(OverlayItem.qrgb(0, 0, 128)) #navy
        sublist.append(OverlayItem.qrgb(255, 165, 0)) #orange
        
        sublist.append(OverlayItem.qrgb(173, 255,  47)) #green-yellow
        sublist.append(OverlayItem.qrgb(128,0, 128)) #purple
        sublist.append(OverlayItem.qrgb(192, 192, 192)) #silver
        sublist.append(OverlayItem.qrgb(240, 230, 140)) #khaki
        colorlist = []
        colorlist.append(long(0))
        colorlist.extend(sublist)
        
        import numpy
        from ilastik.core.randomSeed import RandomSeed
        seed = RandomSeed.getRandomSeed()
        if seed is not None:
            numpy.random.seed(seed)        
        for i in range(17, 256):
            color = OverlayItem.qrgb(numpy.random.randint(255),numpy.random.randint(255),numpy.random.randint(255))
            colorlist.append(color)
            
        return colorlist