Esempio n. 1
0
def multithreshold(img,ignore_zeros=True,firstThreshold=20,nrThresholds=5):
    '''
    labeled,N = multithreshold(img, ignore_zeros = True)

    Performs multi thresholding (which is a form of oversegmentation).

    labeled is of the same size and type as img and contains different labels for 
    the N detected objects (the return of this function is  similar to that of scipy.ndimage.label())

    @param img: The input image
    @param ignore_zeros: Don't take zero pixels into account
    '''
    output=numpy.zeros_like(img)
    if ignore_zeros:
        pmin=nonzeromin(img)
    else:
        pmin=img.min()
    thresholds=pmin+firstThreshold+(img.max()-pmin-firstThreshold)//nrThresholds*numpy.arange(nrThresholds)
    Ts=[majority_filter(img>T) for T in thresholds]
    obj_count = 0
    Ts.append(Ts[0]*0)
    labeled0,N0=ndimage.label(Ts[0])
    for T1 in Ts[1:]:
        labeled1,N1=ndimage.label(T1)
        for obj in xrange(N0):
            binimg=(labeled0 == (obj+1))
            objects1=(labeled1*binimg)
            if not objects1.any() or ndimage.label(objects1)[1] == 1:
                obj_count += 1
                output[binimg]=obj_count
        labeled0=labeled1
        N0=N1

    return output,obj_count
Esempio n. 2
0
def threshold_segment(dna, threshold_method="otsu", smooth=None, median_size=5, min_obj_size=2500):
    """
    labeled = threshold_method(dna, threshold_method='otsu',median_size=5,min_obj_size=2500)

    Simple threshold-based segmentation

    Params
    ------
        * dna: either a pyslic.Image or a DNA image
        * threshold_method: thresholding method to use. Can be either a function or 
            string which denotes the name of a function in pyslic.imageprocessing.thresholding (default: otsu)
        * smooth: either None (no smoothing) or a sigma value for a gaussian blur (default: None)
        * median_size: median filter size (default: 5). Set to None to skip filtering.
        * min_obj_size: minimum object size (default: 2500)
    """
    if type(dna) == Image:
        with loadedimage(dna):
            return threshold_segment(dna.get("dna"), threshold_method, smooth, median_size, min_obj_size)
    if smooth is not None:
        dna = ndimage.gaussian_filter(dna, smooth)
    T = threshold(dna, threshold_method)
    binimg = dna > T
    if median_size is not None:
        binimg = majority_filter(binimg, median_size)
    L, N = ndimage.label(binimg)
    if N == 0:
        return L
    sizes = numpy.array(ndimage.sum(binimg, L, numpy.arange(N + 1)))
    for oid in numpy.where(sizes < min_obj_size)[0]:
        L[L == oid] = 0
    L, N = ndimage.label(L != 0)
    return L
Esempio n. 3
0
 def preprocessimg(img):
     if len(img.shape) > 2:
         assert len(img.shape) == 3, "Cannot handle images of more than 3 dimensions."
         if options.get('3d.mode','perslice') == 'perslice':
             nr_slices=img.shape[0]
             out_proc=img.copy()
             out_res=img.copy()
             for z in xrange(nr_slices):
                 proc,res=preprocessimg(img[z])
                 out_proc[z]=proc
                 out_res[z]=res
             return out_proc,out_res
         else:
             raise Exception('pyslic.preprocessimg: Do not know how to handle 3d.mode: %s' % options['3d.mode'])
     if do_bgsub:
         regions = image.regions
         img = img.copy()
         if regions is not None:
             rid = (regionid if regionid is not None else 1)
             if options.get('bgsub.way','ml') == 'ml':
                 img *= (regions == rid)
                 img = bgsub(img, options)
             else:
                 img = bgsub(img, options)
                 img *= (cropimg == rid)
         else:
             if regionid:
                 warn('Selecting a region different from 1 for an image without region information')
             img = bgsub(img, options)
     imgscaled = stretch(img, 255)
     T = thresholdfor(imgscaled,options)
     mask = (imgscaled > T)
     mask = majority_filter(mask)
     residual = img.copy()
     img *= mask
     residual *= ~mask
     return img,residual
Esempio n. 4
0
 def preprocessimg(img):
     if len(img.shape) > 2:
         assert len(img.shape) == 3, "Cannot handle images of more than 3 dimensions."
         if options.get("3d.mode", "perslice") == "perslice":
             nr_slices = img.shape[0]
             out_proc = img.copy()
             out_res = img.copy()
             for z in xrange(nr_slices):
                 proc, res = preprocessimg(img[z])
                 out_proc[z] = proc
                 out_res[z] = res
             return out_proc, out_res
         else:
             raise Exception("pyslic.preprocessimg: Do not know how to handle 3d.mode: %s" % options["3d.mode"])
     if do_bgsub:
         regions = image.regions
         img = img.copy()
         if regions is not None:
             if options.get("bgsub.way", "ml") == "ml":
                 img *= regions == regionid
                 img = bgsub(img, options)
             else:
                 img = bgsub(img, options)
                 img *= cropimg == regionid
         else:
             if regionid:
                 warn("Selecting a region different from 1 for an image without region information")
             img = bgsub(img, options)
     imgscaled = stretch(img, 255)
     T = thresholdfor(imgscaled, options)
     mask = imgscaled > T
     mask = majority_filter(mask)
     residual = img.copy()
     img *= mask
     residual *= ~mask
     return img, residual