Example #1
0
def test_find_edge():
    import mahotas as mh
    f = np.zeros((32,48))
    f[:,16:] = 255
    f = mh.gaussian_filter(f,4)
    fs = sobel(f)
    assert np.all(fs[:,15] > 0)
def test_sobel():
    I = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0],
                  [0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0]])
    E = sobel(I)
    r, c = I.shape
    for y, x in zip(*np.where(E)):
        N = [I[y, x]]
        if y > 0: N.append(I[y - 1, x])
        if x > 0: N.append(I[y, x - 1])
        if y < (r - 1): N.append(I[y + 1, x])
        if x < (c - 1): N.append(I[y, x + 1])
        assert len(set(N)) > 1
Example #3
0
def extract1(img, solution):
    labeled, n_regions = solution
    nborders = 0.
    filter = np.ones((3,3), dtype=labeled.dtype)
    output = np.zeros(labeled.shape, bool)
    sobelvalues = sobel(img, just_filter=True)

    for i in xrange(n_regions):
        for j in xrange(i+1, n_regions):
            output.fill(False)
            border = _borders.borders(labeled, filter, output, i, j, 0)
            if border is not None:
                yield border_features(img, labeled, sobelvalues, i, j, border)
Example #4
0
def test_sobel():
    I = np.array([
            [0,0,0,1,0,0],
            [0,0,0,1,0,0],
            [0,0,1,1,1,0],
            [0,0,1,1,1,0],
            [0,0,0,0,0,0]])
    E = sobel(I)
    r,c = I.shape
    for y,x in zip(*np.where(E)):
        N = [I[y,x]]
        if y > 0: N.append(I[y-1,x])
        if x > 0: N.append(I[y,x-1])
        if y < (r-1): N.append(I[y+1,x])
        if x < (c-1): N.append(I[y,x+1])
        assert len(set(N)) > 1
def test_sobel_shape():
    A = np.arange(100 * 100)
    A = (A % 15)
    A = A.reshape((100, 100))
    assert sobel(A).shape == A.shape
    assert sobel(A, just_filter=True).shape == A.shape
def test_3d_error():
    f = np.zeros((32, 16, 3))
    with pytest.raises(ValueError):
        sobel(f)
def test_zero_images():
    assert np.isnan(sobel(np.zeros((16, 16)))).sum() == 0
    assert sobel(np.zeros((16, 16)), just_filter=True).sum() == 0
def test_sobel_zeros():
    A = np.zeros((15, 100))
    assert sobel(A).shape == A.shape
    assert sobel(A).sum() == 0
Example #9
0
def test_3d_error():
    import mahotas as mh
    f = np.zeros((32,16,3))
    sobel(f)
Example #10
0
def test_sobel_shape():
    A = np.arange(100*100)
    A = (A % 15)
    A = A.reshape((100,100))
    assert sobel(A).shape == A.shape
    assert sobel(A, just_filter=True).shape == A.shape
Example #11
0
def test_zero_images():
    assert np.isnan(sobel(np.zeros((16,16)))).sum() == 0
    assert sobel(np.zeros((16,16)), just_filter=True).sum() == 0
Example #12
0
def test_sobel_zeros():
    A = np.zeros((15,100))
    assert sobel(A).shape == A.shape
    assert sobel(A).sum() == 0
Example #13
0
def test_3d_error():
    f = np.zeros((32,16,3))
    sobel(f)
Example #14
0
def test_3d_error():
    f = np.zeros((32,16,3))
    sobel(f)
Example #15
0
def test_sobel_shape():
    A = np.zeros((100,100))
    assert sobel(A).shape == A.shape
Example #16
0
def edgefeatures(protproc):
    """
    values = edgefeatures(protproc)
       where protproc contains the pre-processed fluorescence image.
       Pre-processed means that the image has been cropped and had
      Features calculated include:
      (The following feature descriptions were added here by T. Zhao
      according to the reference
      "R. F. Murphy, M. Velliste, and G. Porreca (2003) Robust Numerical
      Features for Description and Classification of Subcellular Location
      Patterns in Fluorescence Microscope Images. J. VLSI Sig. Proc. 35:
      311-321.")
      1. Fraction of above-threshold pixels along edge
      2. Measure of edge gradient intensity homogenerity
      3. Measure of edge direction homogenerity 1
      4. Measure of edge direction homogenerity 2
      5. Measure of edge direction difference

    07 Mar 99 - M.V. Boland

    Corrections by M. Velliste 1/20/02
    1) corrected the way non-zero edge pixels are selected (see below).
    2) corrected the homogeneity feature so that it is based on a
       histogram of edge magnitudes (see below).
    3) Made some comments below about differences between Matlab 5 &
       6, and provided a fix for version 5 so that it would give
       consistent results.

    M.Velliste June 2, 2002: added SLF names
    Ported to Python by Luis Pedro Coelho
    """

    binimg = (protproc > 0)
    edges = sobel(protproc)
    A = edges.sum()/binimg.sum()
    #A = bwarea(edge(imageproc,'canny',[]))/bwarea(im2bw(imageproc)) ;

    # Directional edge filters
    N = np.array([
            [ 1, 1, 1],
            [ 0, 0, 0],
            [-1,-1,-1]],
            np.float_)
    W = np.array([
            [ 1, 0,-1],
            [ 1, 0,-1],
            [ 1, 0,-1]],
            np.float_)

    # Calculation of the gradient from two orthogonal directions
    protproc = asarray(protproc.copy(), np.float_)
    iprocN = ndimage.convolve(protproc,N)
    iprocW = ndimage.convolve(protproc,W)

    # Calculate the magnitude and direction of the gradient
    iprocmag = np.sqrt(iprocN**2. + iprocW**2.)
    iproctheta = np.arctan2(iprocN, iprocW)

    # Change by MV:
    # Identify pixels in iprocmag that are not 0
    #  (i.e. iprocN and iprocW were both 0). Before
    # this was incorrectly based on identifying non-zero
    # pixels in iproctheta, which does remove zero-magnitude
    # edges, but it also removes edges that face exactly east.
    v = iproctheta.ravel()
    v_mag = iprocmag.ravel()
    v = v[v_mag > 0]
    v_mag = v_mag[v_mag > 0]

    if v.size == 0:
        return zeros(5)

    # Histogram the gradient directions
    h,_ = histogram(v,8)

    # max/min ratio
    maxidx=argmax(h)
    hmax=h[maxidx]
    hmin=h.min()

    if hmin > 0:
        maxminratio = hmax/hmin
    else:
        maxminratio = 0


    # Difference between bins of histogram at angle and angle+pi
    #  In general, objects have an equal number of pixels at an angle
    #  and that angle+pi. The differences are normalized to the sum of
    #  the two directions.
    diff = abs(h[:4]-h[4:])/abs(h[:4]+h[4:])
    diff[abs(h[:4]-h[4:])==0] = 0


    h[maxidx] = 0
    hnextmax = h.max()
    maxnextmaxratio=hmax/hnextmax

    sumdiff=diff.sum()

    # Measure of edge homogeneity - what fraction of edge pixels are in
    #  the first two bins of the histogram.
    # Change by MV: Made it be based on edge magnitude histogram. Was
    # incorrectly based on edge direction histogram before.
    h_mag,_ = histogram(v_mag,4)
    homogeneity = h_mag[0]/h_mag.sum()

    return array([A,homogeneity,maxminratio,maxnextmaxratio,sumdiff])