def from_labels(self, bgout=None, exclude=None, binary=True, pbinary=True, inplace=False, neighbors=4, **pmangerkwds): """ Get morphological labels from gray or binary image. Parameters ---------- exclude : 0, 1, 255, 'w', 'b' Exclude white, black or specified integer from labels. For example, 'b' will prevent black pixels from being labeled. bgout : Valid canvas array/color Background image of resulting canvas. If exclude, then there will be unlabled regions. bgout='r' will overlay the labels onto a red background. By default, self.background is used. binary : bool Use binary image; else use grayimage. pbinary : bool Use self.pbinary to generate thresholded image; else, use self.threshfcn (implicit thresholding function) to binarize. Only valid if binary = True Notes ----- Use binary=False with caution. Many grayimages would lead to tends of thousands of labels due to minute color changes in each pixel. Whitle skimage.label can handle this, pyparty will slow down severaly trying to make so many particles from labels. """ if binary: if pbinary: image = self.pbinary #PBINARY NOT self.binaryimage if len(self.particles) == 0: logger.warn('from_labels() recieved "pbinary=True", but ' 'no particles are stored. Use "pbinary=False" to ' 'use implicit thresholding function.') else: image = self.binaryimage else: image = self.grayimage logger.warn('Labels from grayimage can be very slow (fix coming)') if exclude is None: # scikit api doesn't accept None labels = morphology.label(image, neighbors) else: # Parse various cases if exclude == 'w' or exclude == 'white': if binary: exclude = 1 else: exclude = 255 elif exclude == 'b' or exclude == 'black': exclude = 0 labels = morphology.label(image, neighbors, background=exclude) pout = ParticleManager.from_labels(labels, **pmangerkwds) if inplace: self._particles = pout else: cout = Canvas.copy(self) cout.particles = pout if bgout is not None: cout.background = bgout return cout