Esempio n. 1
0
def multi_mask(img, *names, **kwargs):
    """ Return a list/tuple/generator of masks of an image, one for each unique
    label found in a colored img. EVERYTHING DOWN-CONVERTED TO UINT: NECESSARY
    FOR MORPHOLOGY.LABELS ANYWAY, ALTHOUGH POSSIBLY SHOULD DO RGB2GRAY OR 255
    LABELS IS MAX"""
    
    astype = kwargs.pop('astype', tuple)
    ignore = kwargs.pop('ignore', 'black')
    names = list(names)    
    
    # THIS WILL DRASTICALLY REDUCE NUMBER OF UNIQUE LABELS ALLOWED RIGHT? 
    # SINCE IT ONLY HAS 255 POSSIBLE VALUES?!
    if img.ndim == 3:
        img = putil.rgb2uint(img, warnmsg=True)
    
    # Labels requires on grayscale; npunique also doesn't play nice w/ rgb
    
    if ignore == 'black' or ignore == (0,0,0):
        ignore = 0

    elif ignore == 'white' or ignore == (1,1,1):
        ignore = 255
    
    unique = ptools.unique(img)
    
    if ignore not in unique:
        logger.warn("Ignore set to %s but was not found on image." % ignore)
    else:
        unique = [v for v in unique if v != ignore]
        
    names = _parse_names(names, unique)
            
    # Make the mask dict as generator
    out = ((str(names[idx]), (img==v)) for idx, v in enumerate(unique))
    return putil._parse_generator(out, astype)
Esempio n. 2
0
 def to_masks(self, astype=tuple):
     """ Return masks as tuple, list, dict or generator.
     
     Parameters
     ----------
     astype : container type (tuple, list, dict) or None
         Return masks in tuple, list etc... if None, generator. 
     """        
     gen_out = ( (self.names[idx], c.pbinary) for idx, c 
                     in enumerate(self.canvii) )
     return putil._parse_generator(gen_out, astype)
Esempio n. 3
0
    def transmute(self, attr=None, as_type=tuple):
        """ Return a container of names, attributes.  
        
        Parameters
        ----------
        attr : str or None
            Value to be returned paired to name.  Must be valid canvas 
            attribute, or None to return full canvas.

        astype : container type (tuple, list, dict) or None
            Return values in tuple, list etc... if None, generator. 
        """
        if not attr:
            gen_out = ( (self.names[idx], c) for idx, c 
                            in enumerate(self.canvii) )            
        else:
            if hasattr(attr, '__iter__'):
                raise NotImplementedError("Please select a single canvas"
                        "attribute or None to return the entire canvas.")            

            gen_out = ( (self.names[idx], getattr(c, attr)) for idx, c 
                        in enumerate(self.canvii) )

        return putil._parse_generator(gen_out, as_type)