Ejemplo n.º 1
0
 def _run(self, meta_image, container):
     image = meta_image.image
     img_labels = self._constrained_watershed(
         image,
         container.img_labels,
         filter_size=self.params['gauss_filter_size'])
     return ccore.ImageMaskContainer(image, img_labels, False, True, True)
Ejemplo n.º 2
0
    def seededExpandedRegion(self,
                             image,
                             label_image,
                             srg_type,
                             label_number,
                             region_statistics_array=0,
                             expansion_size=1,
                             sep_expansion_size=0,
                             norm_min=0,
                             norm_max=255):

        if label_number is None:
            label_number = label_image.max() + 1

        image = self.normalize(image, norm_min, norm_max)
        image = ccore.numpy_to_image(image, copy=True)
        limage = ccore.numpy_to_image(label_image, copy=True)

        img_labels = ccore.seeded_region_expansion(image, limage, srg_type,
                                                   label_number,
                                                   region_statistics_array,
                                                   expansion_size,
                                                   sep_expansion_size)

        return ccore.ImageMaskContainer(image, img_labels, False, True, True)
Ejemplo n.º 3
0
    def _run(self, meta_image, container):
        image = meta_image.image
        if self.params['expansion_size'] > 0 or self.params['shrinking_size'] > 0:

            nr_objects = container.img_labels.getMinmax()[1] + 1
            if self.params['shrinking_size'] > 0:
                img_labelsA = ccore.seeded_region_shrinking(image,
                                                            container.img_labels,
                                                            nr_objects,
                                                            self.params['shrinking_size'])
            else:
                img_labelsA = container.img_labels

            if self.params['expansion_size'] > 0:
                img_labelsB = ccore.seeded_region_expansion(image,
                                                            container.img_labels,
                                                            ccore.SrgType.KeepContours,
                                                            nr_objects,
                                                            0,
                                                            self.params['expansion_size'],
                                                            0)
            else:
                img_labelsB = container.img_labels
            img_labels = ccore.substractImages(img_labelsB, img_labelsA)
            return ccore.ImageMaskContainer(image, img_labels, False, True, True)
        else:
            raise ValueError("Parameters are not valid. Requirements: 'expansion_size' > 0 and/or "
                             "'shrinking_size' > 0")
Ejemplo n.º 4
0
    def _run(self, meta_image):
        image = meta_image.image

        img_prefiltered = self.prefilter(image)
        
        img_bin1 = self.threshold(img_prefiltered, self.params['latwindowsize'], self.params['latlimit'])

        if self.params['holefilling']:
            ccore.fill_holes(img_bin1, False)

        if self.params['lat2']:
            img_bin2 = self.threshold(img_prefiltered, self.params['latwindowsize2'],
                                      self.params['latlimit2'])

            # replacement for not working ccore.projectImage
            img_bin = numpy.zeros((img_bin2.height, img_bin2.width),
                                 dtype=meta_image.format)
            img_bin = ccore.numpy_to_image(img_bin, copy=True)
            ccore.zproject(img_bin, [img_bin1, img_bin2], ccore.ProjectionType.MaxProjection)
        else:
            img_bin = img_bin1

        if self.params['watershed_distance']:
            img_bin = self.correct_segmetation(img_prefiltered, img_bin, 
                                               self.params['watershed_dynamic'],
                                               self.params['watershed_used_distance'])
            
#        if self.params['shapewatershed']:
#            img_bin = self.correct_segmetation(img_prefiltered, img_bin,
#                                               self.params['latwindowsize'],
#                                               self.params['shapewatershed_gausssize'],
#                                               self.params['shapewatershed_maximasize'],
#                                               self.params['shapewatershed_minmergesize'],
#                                               kind='shape')
#        if self.params['intensitywatershed']:
#            img_bin = self.correct_segmetation(img_prefiltered, img_bin,
#                                               self.params['latwindowsize'],
#                                               self.params['intensitywatershed_gausssize'],
#                                               self.params['intensitywatershed_maximasize'],
#                                               self.params['intensitywatershed_minmergesize'],
#                                               kind='intensity')

        container = ccore.ImageMaskContainer(image, img_bin, self.params['removeborderobjects'])
 
        # calculate offset: mean on the background region, as given by the segmentation result
        # no locality: simply a global mean on the image. 
        np_image = image.toArray(True)
        np_img_bin = img_bin.toArray(True)
        offset = np_image[np_img_bin==0].mean()
        
        self.postprocessing(container, self.params['postprocessing'],
                            (self.params['postprocessing_roisize_min'], self.params['postprocessing_roisize_max']),
                            (self.params['postprocessing_intensity_min'], self.params['postprocessing_intensity_max']),
                            offset=offset)

        return container
Ejemplo n.º 5
0
    def threshold(self,
                  image,
                  median_radius,
                  window_size,
                  min_contrast,
                  remove_borderobjects,
                  fill_holes,
                  norm_min=0,
                  norm_max=255,
                  use_watershed=True,
                  seeding_size=5,
                  outline_smoothing=1,
                  *args,
                  **kw):

        image = self.normalize(image, norm_min, norm_max)
        image = ccore.numpy_to_image(image, copy=True)

        image_smoothed = ccore.gaussianFilter(image, median_radius)

        label_image = ccore.window_average_threshold(image_smoothed,
                                                     window_size, min_contrast)

        if fill_holes:
            ccore.fill_holes(label_image)

        if outline_smoothing >= 1:
            label_image = outlineSmoothing(label_image.toArray(),
                                           outline_smoothing)
            label_image = ccore.numpy_to_image(label_image, copy=True)

        if use_watershed:
            label_image = label_image.toArray().copy()
            label_image = watershed(label_image, seeding_size=seeding_size)
            label_image = ccore.numpy_to_image(label_image.astype(np.int16),
                                               copy=True)

            return ccore.ImageMaskContainer(image, label_image,
                                            remove_borderobjects, True, True)
        else:
            # a static type system sucks!
            return ccore.ImageMaskContainer(image, label_image,
                                            remove_borderobjects)
Ejemplo n.º 6
0
    def _run(self, meta_image, container_prim, container_sec):
        image = meta_image.image
        if not self.params['reverse']:
            img_labels = ccore.substractImages(container_prim.img_labels, container_sec.img_labels)
        else:
            img_labels = ccore.substractImages(container_sec.img_labels, container_prim.img_labels)

        #array = img_labels.toArray()
        #array = numpy.abs(array)
        #img_labels = ccore.numpy_to_image(array, copy=True)
        return ccore.ImageMaskContainer(image, img_labels, False, True, True)
Ejemplo n.º 7
0
    def _run(self, meta_image, container):
        image = meta_image.image

        img_prefiltered = ccore.disc_median(image, self.params['presegmentation_median_radius'])
        t = int(ccore.get_otsu_threshold(img_prefiltered) * self.params['presegmentation_alpha'])
        img_bin = ccore.threshold_image(img_prefiltered, t)
        img_labels = ccore.segmentation_propagate(img_prefiltered, img_bin,
                                                  container.img_labels,
                                                  self.params['lambda'],
                                                  self.params['delta_width'])
        return ccore.ImageMaskContainer(image, img_labels, False, True, True)
Ejemplo n.º 8
0
    def _run(self, meta_image, container):
        image = meta_image.image
        if self.params['shrinking_size'] > 0:
            nr_objects = container.img_labels.getMinmax()[1] + 1
            img_labels = ccore.seeded_region_shrinking(
                image, container.img_labels, nr_objects,
                self.params['shrinking_size'])
        else:
            img_labels = container.img_labels

        return ccore.ImageMaskContainer(image, img_labels, False, True, True)
Ejemplo n.º 9
0
    def _run(self, meta_image, container):
        image = meta_image.image
        if self.params['expansion_size'] > 0:
            nr_objects = container.img_labels.getMinmax()[1] + 1
            img_labels = ccore.seeded_region_expansion(
                image, container.img_labels, ccore.SrgType.KeepContours,
                nr_objects, 0, self.params['expansion_size'], 0)
        else:
            img_labels = container.img_labels

        return ccore.ImageMaskContainer(image, img_labels, False, True, True)
Ejemplo n.º 10
0
    def _run(self, meta_image):
        image = meta_image.image

        coords = dict(
            plate=meta_image.image_container.current_plate,
            pos=meta_image.coordinate.position,
            time=meta_image.coordinate.time,
            zslice=meta_image.coordinate.zslice,
            channel=meta_image.coordinate.channel,
        )

        main_folder = self.params['segmentation_folder']
        #FIXME: This is useful enought to put into an reusable function, maybe in utils?
        locator = self.params["loader_regex"] % coords
        locator_split = locator.split('/')
        locator_match = '/'
        for loc in locator_split[:-1]:
            try:
                match_candidates = os.listdir(main_folder + locator_match)
                if len(match_candidates) == 0:
                    raise RuntimeError
            except:
                raise RuntimeError('No files found in ' + main_folder +
                                   locator_match)
            match_results = [
                m.group() for l in match_candidates
                for m in [re.search(loc, l)] if m
            ]
            if len(match_results) != 1:
                raise RuntimeError('Could not match ' + match_candidates[0] +
                                   ' with ' + loc)
            locator_match += match_results[0] + '/'

        match_candidates = os.listdir(main_folder + locator_match)

        match_results = [
            m.group() for l in match_candidates
            for m in [re.search(locator_split[-1], l)] if m
        ]
        if len(match_results) == 0:
            raise RuntimeError('Could not match ', match_candidates[0], 'with',
                               locator_split[-1])

        match_result = match_results[0]

        img = ccore.readImage(main_folder + locator_match + match_result)
        #        img_pre = SegmentationPluginPrimary.prefilter(self, img, 2)
        #        img_bin = SegmentationPluginPrimary.threshold(self, img_pre, 20, 3)

        container = ccore.ImageMaskContainer(image, img, False)
        return container
Ejemplo n.º 11
0
 def _run(self, meta_image, container):
     image = meta_image.image
     if self.params['expansion_size'] > 0 and self.params['expansion_size'] > self.params['separation_size']:
         nr_objects = container.img_labels.getMinmax()[1] + 1
         img_labels = ccore.seeded_region_expansion(image,
                                                    container.img_labels,
                                                    ccore.SrgType.KeepContours,
                                                    nr_objects,
                                                    0,
                                                    self.params['expansion_size'],
                                                    self.params['separation_size'])
         img_labels = ccore.substractImages(img_labels, container.img_labels)
         return ccore.ImageMaskContainer(image, img_labels, False, True, True)
     else:
         raise ValueError("Parameters are not valid. Requirements: 'expansion_size' > 0 and "
                          "'expansion_size' > 'separation_size'")
Ejemplo n.º 12
0
    def _run(self, meta_image):
        image = meta_image.image

        img_prefiltered = self.prefilter(image)
        img_bin = self.threshold(img_prefiltered, self.params['latwindowsize'],
                                 self.params['latlimit'])

        if self.params['holefilling']:
            ccore.fill_holes(img_bin, False)

        if self.params['lat2']:
            img_bin2 = self.threshold(img_prefiltered,
                                      self.params['latwindowsize2'],
                                      self.params['latlimit2'])
            img_bin = ccore.projectImage([img_bin, img_bin2],
                                         ccore.ProjectionType.MaxProjection)

        if self.params['shapewatershed']:
            img_bin = self.correct_segmetation(
                img_prefiltered,
                img_bin,
                self.params['latwindowsize'],
                self.params['shapewatershed_gausssize'],
                self.params['shapewatershed_maximasize'],
                self.params['shapewatershed_minmergesize'],
                kind='shape')
        if self.params['intensitywatershed']:
            img_bin = self.correct_segmetation(
                img_prefiltered,
                img_bin,
                self.params['latwindowsize'],
                self.params['intensitywatershed_gausssize'],
                self.params['intensitywatershed_maximasize'],
                self.params['intensitywatershed_minmergesize'],
                kind='intensity')

        container = ccore.ImageMaskContainer(
            image, img_bin, self.params['removeborderobjects'])

        self.postprocessing(container, self.params['postprocessing'],
                            (self.params['postprocessing_roisize_min'],
                             self.params['postprocessing_roisize_max']),
                            (self.params['postprocessing_intensity_min'],
                             self.params['postprocessing_intensity_max']))

        return container
Ejemplo n.º 13
0
    def _run(self, meta_image):
        image = meta_image.image

        img_prefiltered = self.prefilter(image)
        img_bin1 = self.threshold(img_prefiltered, self.params['latwindowsize'], self.params['latlimit'])

        if self.params['holefilling']:
            ccore.fill_holes(img_bin1, False)

        if self.params['lat2']:
            img_bin2 = self.threshold(img_prefiltered, self.params['latwindowsize2'],
                                      self.params['latlimit2'])

            # replacement for not working ccore.projectImage
            img_bin = numpy.zeros((img_bin2.height, img_bin2.width),
                                 dtype=meta_image.format)
            img_bin = ccore.numpy_to_image(img_bin, copy=True)
            ccore.zproject(img_bin, [img_bin1, img_bin2], ccore.ProjectionType.MaxProjection)
        else:
            img_bin = img_bin1


        if self.params['shapewatershed']:
            img_bin = self.correct_segmetation(img_prefiltered, img_bin,
                                               self.params['latwindowsize'],
                                               self.params['shapewatershed_gausssize'],
                                               self.params['shapewatershed_maximasize'],
                                               self.params['shapewatershed_minmergesize'],
                                               kind='shape')
        if self.params['intensitywatershed']:
            img_bin = self.correct_segmetation(img_prefiltered, img_bin,
                                               self.params['latwindowsize'],
                                               self.params['intensitywatershed_gausssize'],
                                               self.params['intensitywatershed_maximasize'],
                                               self.params['intensitywatershed_minmergesize'],
                                               kind='intensity')

        container = ccore.ImageMaskContainer(image, img_bin, self.params['removeborderobjects'])

        self.postprocessing(container, self.params['postprocessing'],
                            (self.params['postprocessing_roisize_min'], self.params['postprocessing_roisize_max']),
                            (self.params['postprocessing_intensity_min'], self.params['postprocessing_intensity_max']))

        return container