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)
def getBackgroundROI(self, primary_name): imgIn = ccore.readImageUInt16(primary_name) imgConv = ccore.conversionTo8Bit(imgIn, 2**15, 2**15 + 4096, 0, 255) imgSeg = ccore.window_average_threshold(imgConv, 50, 3) imgDil = ccore.dilate(imgSeg, 20, 8) imgMask = ccore.threshold(imgDil, 1, 255, 255, 0) return imgMask
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)
def threshold(self, img_in, size, limit): img_out = ccore.window_average_threshold(img_in, size, limit) return img_out
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