Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

medial_axis function results with some double lines and small holes #2550

Open
yxdragon opened this issue Mar 1, 2017 · 1 comment
Open

Comments

@yxdragon
Copy link

yxdragon commented Mar 1, 2017

Description

[I use the medial_axis function, the result image has some double lines or small holes, then I count make a topology analysis!]
![demo picture] (http://data.imagepy.org/skebug.png "The medial_axis result and mine result")

I view the source code, and found the medial_axis function, sort by distance map, then use the skeletonize function's table to check if to remove the pixcel. But some times the sort cannot make sure the out-inside turn in extreme condition. So we need a new table for the media_axis. This is my code:

Build the table

from scipy.ndimage import label, generate_binary_structure

strc = generate_binary_structure(2, 2)

# check whether this pixcel can be removed
def check(n):
    a = [(n>>i) & 1 for i in range(8)]
    a.insert(4, 0) 
    ''' make the 3x3 unit
       0  1  2
       3  p  4
       5  6  7
    '''
    # if up, down, left, right all are 1, you cannot make a hole
    if a[1] & a[3] & a[5] & a[7]:return False
    a = np.array(a).reshape((3,3))
    # segments
    n = label(a, strc)[1]
    # if sum is 0, it is a isolate point, you cannot remove it.
    # if sum is 1, it is a terminal point, you cannot remove it.
    # if number of segments = 2, you cannot split the line.
    # if number of segments > 2, it is a node, you cannot remove it.
    return a.sum()>1 and n<2

lut = [check(n) for n in range(256)]
'''
like this:
array([[0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1],
       [0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
       [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
       [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1],
       [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
       [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
       [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0]])
'''

Remove the pixcels

def skel2d(data, idx, lut):
    h, w = data.shape
    data = data.flat
    for i in idx:
        if data[i]==0 : continue
        xy = [i-w-1, i-w, i-w+1,\
                i-1  ,            i+1  ,\
                i+w-1, i+w, i+w+1]

        c = sum([(data[xy[j]]==255)<<j for j in range(8)])
        if lut[c]:data[i] = 128

def mid_axis(img):
    dis = nimg.distance_transform_edt(img)
    idx = np.argsort(dis.flat).astype(np.int32)
    skel2d(img, idx, lut)

I think my code is right, but needs more test.
May I send a Pull Request after rewrite in cython?

Way to reproduce

[If reporting a bug, please include the following important information:]

  • [test image ] (http://data.imagepy.org/ske_bug.png "My Test Image")
  • [Ubuntu 14.0 ] Operating system and version
  • [ 2.7] Python version
  • [ 0.12.3] scikit-image version (run skimage.__version__)
@stefanv
Copy link
Member

stefanv commented Jul 15, 2017

@yxdragon Sorry for not noticing this issue sooner; can you give us a bit more information here? I'm trying to understand what is wrong, and how you propose we fix it.

@soupault soupault modified the milestones: 0.14, 0.15 May 23, 2018
@soupault soupault modified the milestones: 0.15, 0.16 Apr 20, 2019
@scikit-image scikit-image locked and limited conversation to collaborators Oct 19, 2021
@scikit-image scikit-image unlocked this conversation Apr 5, 2022
@grlee77 grlee77 reopened this Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants