Beispiel #1
0
def __extract_local_histogram(image, mask=slice(None), bins=19, rang="image", cutoffp=(0.0, 100.0), size=None, footprint=None, output=None, mode="ignore", origin=0):
    """
    Internal, single-image version of @see local_histogram
    
    Note: Values outside of the histograms range are not considered.
    Note: Mode constant is not available, instead a mode "ignore" is provided.
    Note: Default dtype of returned values is float.
    """
    if "constant" == mode:
        raise RuntimeError('boundary mode not supported')
    elif "ignore" == mode:
        mode = "constant"
    if 'image' == rang:
        rang = tuple(numpy.percentile(image[mask], cutoffp))
    elif not 2 == len(rang):
        raise RuntimeError('the rang must contain exactly two elements or the string "image"')
        
    _, bin_edges = numpy.histogram([], bins=bins, range=rang)
    output, _ = _get_output(numpy.float if None == output else output, image, shape = [bins] + list(image.shape))

    # threshold the image into the histogram bins represented by the output images first dimension, treat last bin separately, since upper border is inclusive
    for i in range(bins - 1):
        output[i] = (image >= bin_edges[i]) & (image < bin_edges[i + 1])
    output[-1] = (image >= bin_edges[-2]) & (image <= bin_edges[-1])

    # apply the sum filter to each dimension, then normalize by dividing through the sum of elements in the bins of each histogram
    for i in range(bins):
        output[i] = sum_filter(output[i], size=size, footprint=footprint, output=None, mode=mode, cval=0.0, origin=origin)
    divident = numpy.sum(output, 0)
    divident[0 == divident] = 1
    output /= divident
    
    # Notes on modes:
    # mode=constant with a cval outside histogram range for the histogram equals a mode=constant with a cval = 0 for the sum_filter
    # mode=constant with a cval inside  histogram range for the histogram has no equal for the sum_filter (and does not make much sense)
    # mode=X for the histogram equals mode=X for the sum_filter

    # treat as multi-spectral image which intensities to extracted
    return __extract_feature(__extract_intensities, [h for h in output], mask)
Beispiel #2
0
    def test_sum_filter(self):
        i = numpy.array([[1, 2, 3], [3, 4, 5], [5, 6, 7]])

        # test reaction to size parameter
        r = sum_filter(i, size=1)
        numpy.testing.assert_allclose(r, i)

        e = numpy.array([[10, 14, 8], [18, 22, 12], [11, 13, 7]])
        r = sum_filter(i, size=2, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        e = numpy.array([[10, 18, 14], [21, 36, 27], [18, 30, 22]])
        r = sum_filter(i, size=3, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        e = numpy.array([[36, 36, 36], [36, 36, 36], [36, 36, 36]])
        r = sum_filter(i, size=5, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        r = sum_filter(i, size=10, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        # test reaction to footprint parameter
        fp = numpy.array([[1]])
        r = sum_filter(i, footprint=fp)
        numpy.testing.assert_allclose(r, i)

        fp = numpy.array([[1, 1], [1, 1]])
        e = numpy.array([[10, 14, 8], [18, 22, 12], [11, 13, 7]])
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        fp = numpy.array([[1, 1]])
        e = numpy.array([[3, 5, 3], [7, 9, 5], [11, 13, 7]])
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        fp = numpy.array([[1], [1]])
        e = numpy.array([[4, 6, 8], [8, 10, 12], [5, 6, 7]])
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        fp = numpy.array([[1, 0], [1, 0], [0, 1]])
        e = numpy.array([[5, 7, 3], [10, 13, 8], [8, 10, 12]])
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        fp = numpy.array([[1, 0], [0, 1], [0, 1]])
        e = numpy.array([[6, 8, 0], [11, 14, 3], [9, 11, 5]])
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        numpy.testing.assert_allclose(r, e)

        # test border treatment modes
        i = numpy.array([[1, 3, 4], [2, 2, 2]])
        fp = numpy.array([[1, 0, 1]])

        e = 6
        r = sum_filter(i, footprint=fp, mode='mirror')
        self.assertAlmostEqual(r[0, 0], e, msg='mirror mode failed')

        e = 4
        r = sum_filter(i, footprint=fp, mode='reflect')
        self.assertAlmostEqual(r[0, 0], e, msg='reflect mode failed')

        e = 7
        r = sum_filter(i, footprint=fp, mode='wrap')
        self.assertAlmostEqual(r[0, 0], e, msg='wrap mode failed')

        e = 4
        r = sum_filter(i, footprint=fp, mode='nearest')
        self.assertAlmostEqual(r[0, 0], e, msg='nearest mode failed')

        e = 3
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        self.assertAlmostEqual(r[0, 0], e, msg='constant mode failed')

        e = 12
        r = sum_filter(i, footprint=fp, mode='constant', cval=9)
        self.assertAlmostEqual(r[0, 0], e, msg='constant mode failed')

        fp = numpy.array([[1, 0, 0], [0, 0, 0]])

        e = 3
        r = sum_filter(i, footprint=fp, mode='mirror')
        self.assertAlmostEqual(r[0, 0], e, msg='mirror mode failed')

        e = 1
        r = sum_filter(i, footprint=fp, mode='reflect')
        self.assertAlmostEqual(r[0, 0], e, msg='reflect mode failed')

        e = 4
        r = sum_filter(i, footprint=fp, mode='wrap')
        self.assertAlmostEqual(r[0, 0], e, msg='wrap mode failed')

        e = 1
        r = sum_filter(i, footprint=fp, mode='nearest')
        self.assertAlmostEqual(r[0, 0], e, msg='nearest mode failed')

        e = 0
        r = sum_filter(i, footprint=fp, mode='constant', cval=0)
        self.assertAlmostEqual(r[0, 0], e, msg='constant mode failed')

        e = 9
        r = sum_filter(i, footprint=fp, mode='constant', cval=9)
        self.assertAlmostEqual(r[0, 0], e, msg='constant mode failed')
Beispiel #3
0
 def test_sum_filter(self):
     i = numpy.array(
         [[1,2,3],
          [3,4,5],
          [5,6,7]])
     
     # test reaction to size parameter
     r = sum_filter(i, size=1)
     numpy.testing.assert_allclose(r, i)
     
     e = numpy.array(
         [[10,14, 8],
          [18,22,12],
          [11,13, 7]])
     r = sum_filter(i, size=2, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     e = numpy.array(
         [[10,18,14],
          [21,36,27],
          [18,30,22]])
     r = sum_filter(i, size=3, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     e = numpy.array(
         [[36,36,36],
          [36,36,36],
          [36,36,36]])
     r = sum_filter(i, size=5, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     r = sum_filter(i, size=10, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     # test reaction to footprint parameter
     fp = numpy.array(
         [[1]])
     r = sum_filter(i, footprint=fp)
     numpy.testing.assert_allclose(r, i)
     
     fp = numpy.array(
         [[1,1],
          [1,1]])
     e = numpy.array(
         [[10,14, 8],
          [18,22,12],
          [11,13, 7]])
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     fp = numpy.array(
         [[1, 1]])
     e = numpy.array(
         [[ 3, 5, 3],
          [ 7, 9, 5],
          [11,13, 7]])
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     fp = numpy.array(
         [[1],
          [1]])
     e = numpy.array(
         [[ 4, 6, 8],
          [ 8,10,12],
          [ 5, 6, 7]])
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     fp = numpy.array(
         [[1, 0],
          [1, 0],
          [0, 1]])
     e = numpy.array(
         [[ 5, 7, 3],
          [10,13, 8],
          [ 8,10,12]])
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     fp = numpy.array(
         [[1, 0],
          [0, 1],
          [0, 1]])
     e = numpy.array(
         [[ 6, 8, 0],
          [11,14, 3],
          [ 9,11, 5]])
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     numpy.testing.assert_allclose(r, e)
     
     # test border treatment modes
     i = numpy.array(
         [[1,3,4],
          [2,2,2]])
     fp = numpy.array(
         [[1,0,1]])
     
     e = 6
     r = sum_filter(i, footprint=fp, mode='mirror')
     self.assertAlmostEqual(r[0,0], e, msg='mirror mode failed')
     
     e = 4
     r = sum_filter(i, footprint=fp, mode='reflect')
     self.assertAlmostEqual(r[0,0], e, msg='reflect mode failed')
     
     e = 7
     r = sum_filter(i, footprint=fp, mode='wrap')
     self.assertAlmostEqual(r[0,0], e, msg='wrap mode failed')
     
     e = 4
     r = sum_filter(i, footprint=fp, mode='nearest')
     self.assertAlmostEqual(r[0,0], e, msg='nearest mode failed')
     
     e = 3
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     self.assertAlmostEqual(r[0,0], e, msg='constant mode failed')
     
     e = 12
     r = sum_filter(i, footprint=fp, mode='constant', cval=9)
     self.assertAlmostEqual(r[0,0], e, msg='constant mode failed')
     
     fp = numpy.array(
         [[1,0,0],
          [0,0,0]])
     
     e = 3
     r = sum_filter(i, footprint=fp, mode='mirror')
     self.assertAlmostEqual(r[0,0], e, msg='mirror mode failed')
     
     e = 1
     r = sum_filter(i, footprint=fp, mode='reflect')
     self.assertAlmostEqual(r[0,0], e, msg='reflect mode failed')
     
     e = 4
     r = sum_filter(i, footprint=fp, mode='wrap')
     self.assertAlmostEqual(r[0,0], e, msg='wrap mode failed')
     
     e = 1
     r = sum_filter(i, footprint=fp, mode='nearest')
     self.assertAlmostEqual(r[0,0], e, msg='nearest mode failed')
     
     e = 0
     r = sum_filter(i, footprint=fp, mode='constant', cval=0)
     self.assertAlmostEqual(r[0,0], e, msg='constant mode failed')
     
     e = 9
     r = sum_filter(i, footprint=fp, mode='constant', cval=9)
     self.assertAlmostEqual(r[0,0], e, msg='constant mode failed')        
Beispiel #4
0
def count_neighbor(image, structure):
    'returns image, where the current value of the voxel is the number of its neighbors'
    image = image.astype(numpy.bool)
    sumimage = sum_filter(image, footprint=structure, mode="constant", cval=0.0, output=numpy.uint)
    sumimage[~image] = 0
    return sumimage - image