예제 #1
0
파일: test_thin.py 프로젝트: PhDCK/mahotas
def slow_thin(binimg):
    """
    This was the old implementation
    """
    from mahotas.bbox import bbox
    from mahotas._morph import hitmiss

    _struct_elems = []
    _struct_elems.append([
            [0,0,0],
            [2,1,2],
            [1,1,1]])
    _struct_elems.append([
            [2,0,0],
            [1,1,0],
            [1,1,2]])
    _struct_elems.append([
            [1,2,0],
            [1,1,0],
            [1,2,0]])
    _struct_elems.append([
            [1,1,2],
            [1,1,0],
            [2,0,0]])
    _struct_elems.append([
            [1,1,1],
            [2,1,2],
            [0,0,0]])
    _struct_elems.append([
            [2,1,1],
            [0,1,1],
            [0,0,2]])
    _struct_elems.append([
            [0,2,1],
            [0,1,1],
            [0,2,1]])
    _struct_elems.append([
            [0,0,2],
            [0,1,1],
            [2,1,1]])

    _struct_elems = [np.array(elem, np.uint8) for elem in _struct_elems]
    res = np.zeros_like(binimg)
    min0,max0,min1,max1 = bbox(binimg)

    r,c = (max0-min0,max1-min1)

    image_exp = np.zeros((r+2, c+2), np.uint8)
    imagebuf = np.zeros((r+2,c+2), np.uint8)
    prev = np.zeros((r+2,c+2), np.uint8)
    image_exp[1:r+1, 1:c+1] = binimg[min0:max0,min1:max1]
    while True:
        prev[:] = image_exp[:]
        for elem in _struct_elems:
            newimg = hitmiss(image_exp, elem, imagebuf)
            image_exp -= newimg
        if np.all(prev == image_exp):
            break
    res[min0:max0,min1:max1] = image_exp[1:r+1, 1:c+1]
    return res
예제 #2
0
def _compute_features(img):
    bimg = (img > 0)
    s00,s01,s10,s11 = bbox(bimg)
    if s00 > 0: s00 -= 1
    if s10 > 0: s10 -= 1
    bimg = bimg[s00:s01+1,s10:s11+1]
    hull = convexhull(bimg - pymorph.erode(bimg))
    Allfeats = np.r_[features.hullfeatures.hullfeatures(img,hull),features.hullfeatures.hullsizefeatures(img,hull)]
    return Allfeats[np.array([0,1,2,3,5,6,7],int)]
예제 #3
0
def dist_hausdorff_compare(segmented, ref):
    joint = np.histogram2d(ref.ravel(), segmented.ravel(),
                           np.arange(max(ref.max(), segmented.max()) + 2))[0]
    assignments = joint.argmax(0)[:segmented.max() + 1]
    #revassignments = joint.argmax(1)[:ref.max()+1]
    values = []
    hausdorff = []
    for i in xrange(1, int(segmented.max() + 1)):
        if assignments[i] == 0:
            values += ['bg']
            continue
        min1, max1, min2, max2 = bbox((ref == assignments[i])
                                      | (segmented == i))
        segc = (segmented[min1:max1, min2:max2] == i)
        refc = (ref[min1:max1, min2:max2] == assignments[i])
        dref = np.maximum(ndimage.distance_transform_edt(refc),
                          ndimage.distance_transform_edt(~refc))
        values.append(dref[segc != refc].sum() /
                      float(dref[segc | refc].sum()))
        hausdorff.append((dref * (segc != refc)).max())
    return values, hausdorff
예제 #4
0
def slow_thin(binimg):
    """
    This was the old implementation
    """
    from mahotas.bbox import bbox
    from mahotas._morph import hitmiss

    _struct_elems = []
    _struct_elems.append([[0, 0, 0], [2, 1, 2], [1, 1, 1]])
    _struct_elems.append([[2, 0, 0], [1, 1, 0], [1, 1, 2]])
    _struct_elems.append([[1, 2, 0], [1, 1, 0], [1, 2, 0]])
    _struct_elems.append([[1, 1, 2], [1, 1, 0], [2, 0, 0]])
    _struct_elems.append([[1, 1, 1], [2, 1, 2], [0, 0, 0]])
    _struct_elems.append([[2, 1, 1], [0, 1, 1], [0, 0, 2]])
    _struct_elems.append([[0, 2, 1], [0, 1, 1], [0, 2, 1]])
    _struct_elems.append([[0, 0, 2], [0, 1, 1], [2, 1, 1]])

    _struct_elems = [np.array(elem, np.uint8) for elem in _struct_elems]
    res = np.zeros_like(binimg)
    min0, max0, min1, max1 = bbox(binimg)

    r, c = (max0 - min0, max1 - min1)

    image_exp = np.zeros((r + 2, c + 2), np.uint8)
    imagebuf = np.zeros((r + 2, c + 2), np.uint8)
    prev = np.zeros((r + 2, c + 2), np.uint8)
    image_exp[1:r + 1, 1:c + 1] = binimg[min0:max0, min1:max1]
    while True:
        prev[:] = image_exp[:]
        for elem in _struct_elems:
            newimg = hitmiss(image_exp, elem, imagebuf)
            image_exp -= newimg
        if np.all(prev == image_exp):
            break
    res[min0:max0, min1:max1] = image_exp[1:r + 1, 1:c + 1]
    return res
예제 #5
0
def preprocessimage(image, regionid=None, crop=True, options = {}):
    """
    Preprocess the image

    image should be an Image object
    regionid should be an integer, which indexes into image.region

    options is a dictionary. The following options are accepted:

        + 'bgsub.way': One of
            - 'ml' (default) 
            - 'mb' and controls whether the region is selected prior to preprocessing
         + '3d.mode': One of
            - 'perslice' (default): process each slice separately
         + 'threshold.algorithm': One of
            - 'rc': Riddlar-Calvard
            - 'mean':  Image mean
    """
    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
    image.lazy_load()

    protein = image.channeldata['protein']
    dna = image.channeldata.get('dna')
    do_bgsub = True
    if 'bgsubprotein' in image.temp:
        protein = image.temp['bgsubprotein']
        do_bgsub = False
    if regionid is not None and 'region_ids' in image.temp:
        location = image.temp['region_ids'][regionid - 1]
        if location is None:
            image.channeldata['procprotein'] = \
                image.channeldata['resprotein'] = \
                image.channeldata['procdna'] = np.zeros((0,0), dtype=protein.dtype)
            return
        protein = protein[location]
        if dna is not None:
            dna = dna[location]
    image.channeldata['procprotein'],image.channeldata['resprotein'] = preprocessimg(protein)
    if dna is not None:
        image.channeldata['procdna'],_ = preprocessimg(dna)

    if crop:
        fullimage = (image.channeldata['procprotein'] > 0) | (image.channeldata['resprotein'] >0)
        if 'dna' in image.channeldata:
            fullimage |= (image.channeldata['procdna'] > 0)

        min1,max1,min2,max2 = bbox(fullimage)
        border = 2
        min1 = max(0, min1 - border)
        min2 = max(0, min2 - border)
        max1 += border
        max2 += border

        image.channeldata['procprotein'] = image.channeldata['procprotein'][min1:max1,min2:max2]
        image.channeldata['resprotein'] = image.channeldata['resprotein'][min1:max1,min2:max2]
        if 'dna' in image.channeldata:
            image.channeldata['procdna'] = image.channeldata['procdna'][min1:max1,min2:max2]