Exemplo n.º 1
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)
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo 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['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
Exemplo 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)
Exemplo n.º 6
0
    def __call__(self, meta_image):
        stopwatch_total = StopWatch()
        stopwatch = StopWatch()

        #print "moo123"
        #image = meta_image.image
        #print type(image), image.getMinmax()
        #meta_image.setImageXY(convertImageMinMax(image))

        image = meta_image.image
        #print type(image), image.getMinmax()
        width, height = meta_image.width, meta_image.height
        #print width, height, self.strPathOutDebug

        if self.bSpeedup:
            imgTmp1 = ccore.Image(width, height)
            ccore.binImage(image, imgTmp1, 2)
            width /= 2
            height /= 2
            imgTmp2 = ccore.Image(width, height)
            ccore.scaleImage(imgTmp1, imgTmp2, "no")
            image = imgTmp2

#        if self.bDebugMode:
#            strPathOutDebug = self.strPathOutDebug
#            ccore.writeImage(image,
#                             os.path.join(strPathOutDebug,
#                                          meta_image.format("00raw.jpg", bC=True)),
#                             self.strImageOutCompression)


        img_prefiltered = ccore.disc_median(image,
                                            self.iMedianRadius)
        self._logger.debug("         --- median ok, %s" %
                            stopwatch.current_interval())

        stopwatch.reset()
        #print self.bDebugMode, self.strPathOutDebug
#        if self.bDebugMode:
#            ccore.writeImage(img_prefiltered,
#                             os.path.join(strPathOutDebug,
#                                          meta_image.format("00pre.jpg", bC=True)),
#                             self.strImageOutCompression)

        imgBin = ccore.window_average_threshold(img_prefiltered,
                                                self.iLatWindowSize,
                                                self.iLatLimit)
        self._logger.debug("         --- local threshold ok, %s" %
                            stopwatch.current_interval())

        if self.hole_filling:
            ccore.fill_holes(imgBin, False)

        stopwatch.reset()
        #self._logger.debug("         --- local threshold2 %s %s" % (self.iLatWindowSize2, )
        if not self.iLatWindowSize2 is None and not self.iLatLimit2 is None:
            imgBin2 = ccore.window_average_threshold(img_prefiltered,
                                                     self.iLatWindowSize2,
                                                     self.iLatLimit2)
            imgBin = ccore.projectImage([imgBin, imgBin2], ccore.ProjectionType.MaxProjection)
            self._logger.debug("         --- local threshold2 ok, %s" %
                                stopwatch.current_interval())

        stopwatch.reset()
#        if self.bDebugMode:
#            strPathOutDebug = self.strPathOutDebug
#            ccore.writeImage(imgBin,
#                             os.path.join(strPathOutDebug,
#                                          meta_image.format("01bin.jpg", bC=True)),
#                             self.strImageOutCompression)
#        else:
#            strPathOutDebug = ""

        if self.bDoShapeWatershed:
            imgBin = ccore.segmentation_correction_shape(img_prefiltered,
                                                         imgBin,
                                                         self.iLatWindowSize,
                                                         self.iGaussSizeShape,
                                                         self.iMaximaSizeShape,
                                                         self.iMinMergeSize)

        if self.bDoIntensityWatershed:
            imgBin = ccore.segmentation_correction_intensity(img_prefiltered,
                                                             imgBin,
                                                             self.iLatWindowSize,
                                                             self.iGaussSizeIntensity,
                                                             self.iMaximaSizeIntensity,
                                                             self.iMinMergeSize)

        self._logger.debug("         --- segmentation ok, %s" %
                            stopwatch.current_interval())

        stopwatch.reset()
        if self.bSpeedup:
            width, height = meta_image.width, meta_image.height
            imgTmpBin = ccore.Image(width, height)
            ccore.scaleImage(imgBin, imgTmpBin, "no")
            imgBin = imgTmpBin

            image = meta_image.image

        container = ccore.ImageMaskContainer(image,
                                             imgBin,
                                             self.bRemoveBorderObjects)

        self._logger.debug("         --- container ok, %s" %
                            stopwatch.current_interval())

        stopwatch.reset()
        # post-processing
        #print self.bPostProcessing, self.lstPostprocessingFeatureCategories
        if self.bPostProcessing:

            # extract features
            for strFeature in self.lstPostprocessingFeatureCategories:
                container.applyFeature(strFeature)
            dctObjects = container.getObjects()

            lstGoodObjectIds = []
            lstRejectedObjectIds = []

            for iObjectId in dctObjects.keys()[:]:
                dctObjectFeatures = dctObjects[iObjectId].getFeatures()
                if not eval(self.strPostprocessingConditions, dctObjectFeatures):
                    if self.bPostProcessDeleteObjects:
                        del dctObjects[iObjectId]
                        container.delObject(iObjectId)
                    lstRejectedObjectIds.append(iObjectId)
                else:
                    lstGoodObjectIds.append(iObjectId)
            self._logger.debug("         --- post-processing ok, %s" %
                                stopwatch.current_interval())
        else:
            lstGoodObjectIds = container.getObjects().keys()
            lstRejectedObjectIds = []

        container.lstGoodObjectIds = lstGoodObjectIds
        container.lstRejectedObjectIds = lstRejectedObjectIds

#        if self.bDebugMode:
#            container.markObjects(ccore.RGBValue(0,255,0), False, False)
#            #container.markObjects(lstGoodObjectIds, ccore.RGBValue(0,255,0), False, False)
#            #container.markObjects(lstRejectedObjectIds, ccore.RGBValue(255,0,0), False, False)
#            container.exportRGB(os.path.join(strPathOutDebug,
#                                              meta_image.format("03Contour.jpg", bC=True)),
#                                 self.strImageOutCompression)
#
#            # reset the container RGB
#            container.eraseRGB()
#            container.combineExtraRGB([7],[1])
        self._logger.debug("         total time: %s" %
                            stopwatch_total.current_interval())
        return container