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 split(self, img, imbin, alpha=0.5, dynval=2): pdb.set_trace() img_cc = ccore.numpy_to_image(img, copy=True) imbin_cc = ccore.numpy_to_image(imbin.astype(np.dtype('uint8')), copy=True) # inversion imbin_inv = ccore.linearRangeMapping(imbin_cc, 255, 0, 0, 255) # distance function of the inverted image imDist = ccore.distanceTransform(imbin_inv, 2) # gradient of the image imGrad = ccore.externalGradient(img_cc, 1, 8) im1 = imDist.toArray() im2 = imGrad.toArray() im1 = im1.astype(np.dtype('float32')) im2 = im2.astype(np.dtype('float32')) temp = im1 + alpha * im2 minval = temp.min() maxval = temp.max() if maxval==minval: return temp = 254 / (maxval - minval) * (temp - minval) temp = temp.astype(np.dtype('uint8')) temp_cc = ccore.numpy_to_image(temp, copy=True) ws = ccore.watershed_dynamic_split(temp_cc, dynval) res = ccore.infimum(ws, imbin_cc) return res
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)
def seededRegion(self, image, labels, mask, range=(-1, 1), norm_min=0, norm_max=255, remove_borderobjects=False): mask = SeededMask(mask) labels = mask(labels, range) # imsave("/home/hoefler/images/%s" %self.fff.replace(".lsm", ".png"), # labels) image = self.normalize(image, norm_min, norm_max) image = ccore.numpy_to_image(image, copy=True) labels = ccore.numpy_to_image(labels.astype(np.int16), copy=True) return ccore.ImageMaskContainer(image, labels, remove_borderobjects, True, True)
def apply_zselection(self): if type(self.oZSliceOrProjection) == types.TupleType: method, zbegin, zend, zstep = self.oZSliceOrProjection images = [img.image for img in self._zslices][(zbegin-1):zend:zstep] # single images don't carry the dtype dtype = img.format if method == "maximum": method_const = ccore.ProjectionType.MaxProjection elif method == "minimum": method_const = ccore.ProjectionType.MinProjection elif method == "average": method_const = ccore.ProjectionType.MeanProjection self.logger.debug("* applying %s Z-Projection to stack of %d images..." %(method, len(images))) imgprj = numpy.zeros((images[0].height, images[0].width), dtype=dtype) imgprj = ccore.numpy_to_image(numpy.zeros((images[0].height, images[0].width), dtype=dtype), copy=True) ccore.zproject(imgprj, images, method_const) # overwrite the first MetaImage found with the projected image data meta_image = self._zslices[0] meta_image.set_image(imgprj) else: self.oZSliceOrProjection = int(self.oZSliceOrProjection) self.logger.debug("* selecting z-slice %d..." % self.oZSliceOrProjection) try: meta_image = self._zslices[self.oZSliceOrProjection-1] except IndexError: raise IndexError("Invalid z-slice selection for channel %s" %self.NAME) self.meta_image = copy.copy(meta_image)
def threshold(self, image, params): image = self.normalize(image, params.norm_min, params.norm_max) smoothed = gaussianFilter(image, params. median_radius) label_image = PrimaryMask(params.mask)(smoothed, **params._asdict()) # imsave("/home/hoefler/images/%s" %self.fff.replace(".lsm", ".png"), # label_image) image = ccore.numpy_to_image(image, copy=True) label_image = ccore.numpy_to_image( label_image.astype(np.int16), copy=True) cnt = ccore.ImageMaskContainer(image, label_image, False, True, True) # need watershed_mask for other color channels return cnt
def prepare_raw_image(self, channel): self.create_nc4() desc = '[P %s, T %05d, C %s]' % (self.P, self._iCurrentT, channel.strChannelId) if self._create_nc or self._reuse_nc: grp = self._dataset.groups[self.NC_GROUP_RAW] var = grp.variables[channel.strChannelId] frame_idx = self._frames_to_idx[self._iCurrentT] if len(var.valid.shape) == 0: frame_valid = var.valid else: frame_valid = var.valid[frame_idx] if self._reuse_nc and frame_valid: coordinate = Coordinate(position=self.P, time=self._iCurrentT, channel=channel.strChannelId, zslice=1) meta_image = MetaImage(image_container=None, coordinate=coordinate) img = ccore.numpy_to_image(var[frame_idx], copy=True) meta_image.set_image(img) meta_image.set_raw_image(img) channel.meta_image = meta_image self._logger.debug('Raw image %s loaded from nc4 file.' % desc) else: channel.apply_zselection() channel.normalize_image() channel.apply_registration() if self._create_nc: img = channel.meta_image.image grp = self._dataset.groups[self.NC_GROUP_RAW] var = grp.variables[channel.strChannelId] var[frame_idx] = img.toArray(copy=False) self.nc_valid_set(var, frame_idx, 1) self._logger.debug('Raw image %s written to nc4 file.' % desc) if self._hdf5_create and self._hdf5_include_raw_images: meta = self._meta_data w = meta.real_image_width h = meta.real_image_height f = self._hdf5_file if 'images' in f: images = f['images'] else: images = f.create_dataset('pre_images', (meta.dim_c, meta.dim_t, meta.dim_z, w, h), 'uint8', compression='szip', chunks=(1,1,meta.dim_z,w,h)) dt = h5py.special_dtype(vlen=str) channels = f.create_dataset('channel_names', (meta.dim_c,), dt) for idx in range(meta.dim_c): channels[idx] = self._idx_to_channels[idx] frame_idx = self._frames_to_idx[self._iCurrentT] channel_idx = self._channels_to_idx[channel.strChannelId] img = channel.meta_image.image array = img.toArray(copy=False) print array.shape, (meta.dim_c, meta.dim_t, meta.dim_z, w, h), frame_idx, channel_idx images[channel_idx, frame_idx, 0] = array
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
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 prefilter_new(self, img, rec_size=20, se_size=3): img_cc = ccore.numpy_to_image(img, copy=True) im1 = ccore.diameter_open(img_cc, rec_size, 8) im2 = ccore.diameter_close(im1, int(rec_size / 2), 8) #im1 = self.morpho_rec(img, rec_size) #im2 = self.morpho_rec2(im1, int(rec_size / 2)) se = morphology.disk(se_size) im3 = morphology.closing(im2.toArray(), se) return im3
def split(self, img, imbin, alpha=0.5, dynval=2): pdb.set_trace() img_cc = ccore.numpy_to_image(img, copy=True) imbin_cc = ccore.numpy_to_image(imbin.astype(np.dtype('uint8')), copy=True) # inversion imbin_inv = ccore.linearRangeMapping(imbin_cc, 255, 0, 0, 255) # distance function of the inverted image imDist = ccore.distanceTransform(imbin_inv, 2) # gradient of the image imGrad = ccore.externalGradient(img_cc, 1, 8) im1 = imDist.toArray() im2 = imGrad.toArray() im1 = im1.astype(np.dtype('float32')) im2 = im2.astype(np.dtype('float32')) temp = im1 + alpha * im2 minval = temp.min() maxval = temp.max() if maxval == minval: return temp = 254 / (maxval - minval) * (temp - minval) temp = temp.astype(np.dtype('uint8')) temp_cc = ccore.numpy_to_image(temp, copy=True) ws = ccore.watershed_dynamic_split(temp_cc, dynval) res = ccore.infimum(ws, imbin_cc) return res
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
def apply_zselection(self): if type(self.oZSliceOrProjection) == types.TupleType: method, zbegin, zend, zstep = self.oZSliceOrProjection images = [img.image for img in self._zslices][(zbegin - 1):zend:zstep] # single images don't carry the dtype dtype = img.format if method == "maximum": method_const = ccore.ProjectionType.MaxProjection elif method == "minimum": method_const = ccore.ProjectionType.MinProjection elif method == "average": method_const = ccore.ProjectionType.MeanProjection self.logger.debug( "* applying %s Z-Projection to stack of %d images..." % (method, len(images))) imgprj = numpy.zeros((images[0].height, images[0].width), dtype=dtype) imgprj = ccore.numpy_to_image(numpy.zeros( (images[0].height, images[0].width), dtype=dtype), copy=True) ccore.zproject(imgprj, images, method_const) # overwrite the first MetaImage found with the projected image data meta_image = self._zslices[0] meta_image.set_image(imgprj) else: self.oZSliceOrProjection = int(self.oZSliceOrProjection) self.logger.debug("* selecting z-slice %d..." % self.oZSliceOrProjection) try: meta_image = self._zslices[self.oZSliceOrProjection - 1] except IndexError: raise IndexError("Invalid z-slice selection for channel %s" % self.NAME) self.meta_image = copy.copy(meta_image)
def diameter_tophat(self, img, max_size): img_cc = ccore.numpy_to_image(img, copy=True) diam_close_cc = ccore.diameter_close(img_cc, max_size, 8) diam_close = diam_close_cc.toArray() res = diam_close - img return res
def diameter_close(self, img, max_size): img_cc = ccore.numpy_to_image(img, copy=True) res_cc = ccore.diameter_close(img_cc, max_size, 8) res = res_cc.toArray() return res
def _predict_image_with_ilastik(self, image_): import ilastik from ilastik.core.dataMgr import DataMgr, DataItemImage from ilastik.modules.classification.core.featureMgr import FeatureMgr from ilastik.modules.classification.core.classificationMgr import ClassificationMgr from ilastik.modules.classification.core.features.featureBase import FeatureBase from ilastik.modules.classification.core.classifiers.classifierRandomForest import ClassifierRandomForest from ilastik.modules.classification.core.classificationMgr import ClassifierPredictThread from ilastik.core.volume import DataAccessor import numpy, h5py dataMgr = DataMgr() # Transform input image to ilastik convention s # 3D = (time,x,y,z,channel) # 2D = (time,1,x,y,channel) # Note, this work for 2D images right now. Is there a need for 3D image_.shape = (1,1) + image_.shape # Check if image_ has channels, if not add singelton dimension if len(image_.shape) == 4: image_.shape = image_.shape + (1,) # Add data item di to dataMgr di = DataItemImage('') di.setDataVol(DataAccessor(image_)) dataMgr.append(di, alreadyLoaded=True) fileName = self.params["ilastik_classifier"] ilastik_class = self.params["ilastik_class_selector"] hf = h5py.File(fileName,'r') temp = hf['classifiers'].keys() # If hf is not closed this leads to an error in win64 and mac os x hf.close() del hf classifiers = [] for cid in temp: cidpath = 'classifiers/' + cid classifiers.append(ClassifierRandomForest.loadRFfromFile(fileName, str(cidpath))) dataMgr.module["Classification"]["classificationMgr"].classifiers = classifiers # Restore user selection of feature items from hdf5 featureItems = [] f = h5py.File(fileName,'r') for fgrp in f['features'].values(): featureItems.append(FeatureBase.deserialize(fgrp)) f.close() del f fm = FeatureMgr(dataMgr, featureItems) # Create FeatureMgr # Compute features fm.prepareCompute(dataMgr) fm.triggerCompute() fm.joinCompute(dataMgr) # Predict with loaded classifier classificationPredict = ClassifierPredictThread(dataMgr) classificationPredict.start() classificationPredict.wait() if ilastik_class >= classificationPredict._prediction[0].shape[-1]: raise RuntimeError('ilastik output class not valid...') # Produce output image and select the probability map probMap = (classificationPredict._prediction[0][0,0,:,:, ilastik_class] * 255).astype(numpy.uint8) img_out = ccore.numpy_to_image(probMap, True) return img_out
def threshold(self, img_in, *args): np_img = img_in.toArray(True) return ccore.numpy_to_image((np_img > 128).astype(numpy.uint8), True)
def apply_segmentation(self, channel, primary_channel=None): self.create_nc4() valid = False desc = '[P %s, T %05d, C %s]' % (self.P, self._iCurrentT, channel.strChannelId) name = channel.NAME.lower() if self._create_nc or self._reuse_nc: grp = self._dataset.groups[self.NC_GROUP_LABEL] grp = grp.groups[name] frame_idx = self._frames_to_idx[self._iCurrentT] if self._reuse_nc: for region_name in channel.lstAreaSelection: var = grp.variables[region_name] if len(var.valid.shape) == 0: frame_valid = var.valid else: frame_valid = var.valid[frame_idx] if frame_valid: img_label = ccore.numpy_to_image(var[frame_idx], copy=True) img_xy = channel.meta_image.image container = ccore.ImageMaskContainer(img_xy, img_label, False, True, True) channel.dctContainers[region_name] = container valid = True else: valid = False break if not valid: channel.apply_segmentation(primary_channel) if self._create_nc: for region_name in channel.lstAreaSelection: var = grp.variables[region_name] container = channel.dctContainers[region_name] var[frame_idx] = \ container.img_labels.toArray(copy=False) self.nc_valid_set(var, frame_idx, 1) self._logger.debug('Label images %s written to nc4 file.' %\ desc) else: self._logger.debug('Label images %s loaded from nc4 file.' %\ desc) if self._hdf5_create and self._hdf5_include_label_images: meta = self._meta_data w = meta.real_image_width h = meta.real_image_height f = self._hdf5_file if 'labeled_images' in f: labeled_images = f['labeled_images'] else: nr_labels = len(self._regions_to_idx2) dt = h5py.special_dtype(vlen=str) names = f.create_dataset('label_names', (nr_labels, 2), dt) for tpl, idx in self._regions_to_idx2.iteritems(): names[idx] = tpl labeled_images = f.create_dataset('label_images', (nr_labels, meta.dim_t, meta.dim_z, w, h), 'int32', compression='szip', chunks=(1,1,meta.dim_z,w,h)) frame_idx = self._frames_to_idx[self._iCurrentT] for region_name in channel.lstAreaSelection: idx = self._regions_to_idx2[(channel.NAME, region_name)] container = channel.dctContainers[region_name] array = container.img_labels.toArray(copy=False) labeled_images[idx, frame_idx, 0] = numpy.require(array, 'int32')