def test_neighbor_midy(self): """Test 2.""" im = np.zeros((5, 5)) vals = im_utils.neighbor_vals(im, 0, 2) # make assertion assert vals[1] == 0. assert vals[2] == 0. assert vals[4] == 0. assert vals[6] == 0. assert vals[7] == 0.
def test_neighbor_endymidx(self): """Test 8.""" im = np.zeros((5, 5)) vals = im_utils.neighbor_vals(im, 2, 4) # make assertion assert vals[0] == 0. assert vals[1] == 0. assert vals[2] == 0. assert vals[3] == 0. assert vals[4] == 0.
def test_neighbor_endyzero(self): """Test 7.""" im = np.zeros((5, 5)) vals = im_utils.neighbor_vals(im, 2, 0) # make assertion assert vals[3] == 0. assert vals[4] == 0. assert vals[5] == 0. assert vals[6] == 0. assert vals[7] == 0.
def test_neighbor_endxmid(self): """Test 6.""" im = np.zeros((5, 5)) vals = im_utils.neighbor_vals(im, 4, 2) # make assertion assert vals[0] == 0. assert vals[1] == 0. assert vals[3] == 0. assert vals[5] == 0. assert vals[6] == 0.
def simplify_skel(Iskel): """ This function iterates through all skeleton pixels pixels that have connectivity > 2. It removes the pixel and checks if the number of blobs has changed after removal. If so, the pixel is necessary to maintain connectivity. Otherwise the pixel is not retained. It also adds pixels to the centers of "+" cases, as this reduces the number of branchpoints from 4 to 1. Parameters ---------- Iskel : np.array Image of the skeleton to simplify. Returns ------- Iskel : np.array The simplified skeleton. """ Iskel = np.array(Iskel, dtype=np.uint8) Ic = imu.im_connectivity(Iskel) ypix, xpix = np.where(Ic > 2) # Get all pixels with connectivity > 2 for y, x in zip(ypix, xpix): nv = imu.neighbor_vals(Iskel, x, y) # Skip edge cases if np.any(np.isnan(nv)) == True: continue # Create 3x3 array with center pixel removed Inv = np.array( [[nv[0], nv[1], nv[2]], [nv[3], 0, nv[4]], [nv[5], nv[6], nv[7]]], dtype=bool) # Check the connectivity after removing the pixel, set to zero if unchanged Ilabeled = measure.label(Inv, background=0, connectivity=2) if np.max(Ilabeled) == 1: Iskel[y, x] = 0 # We simplify the network if we actually add pixels to the centers of # "+" cases, so let's do that. kern = np.array([[1, 10, 1], [10, 1, 10], [1, 10, 1]], dtype=np.uint8) Iconv = cv2.filter2D(Iskel, -1, kern) Iskel[Iconv == 40] = 1 return Iskel
def skel_simplify(Iskel): """ Another attempt to simplify skeletons. This method iterates through all pixels that have connectivity > 2. It removes the pixel and checks if the number of blobs has changed after removal. If so, the pixel is necessary to maintain connectivity. """ Iskel = np.array(Iskel, dtype=np.uint8) Ic = iu.im_connectivity(Iskel) ypix, xpix = np.where(Ic > 2) # Get all pixels with connectivity > 2 for y, x in zip(ypix, xpix): nv = iu.neighbor_vals(Iskel, x, y) # Check for edge cases; skip them for now if np.any(np.isnan(nv)) == True: continue # Create 3x3 array with center pixel removed Inv = np.array( [[nv[0], nv[1], nv[2]], [nv[3], 0, nv[4]], [nv[5], nv[6], nv[7]]], dtype=np.bool) # Check the connectivity after removing the pixel, set to zero if unchanged Ilabeled = measure.label(Inv, background=0, connectivity=2) if np.max(Ilabeled) == 1: Iskel[y, x] = 0 # We simplify the network if we actually add pixels to the centers of # "+" cases, so let's do that. kern = np.array([[1, 10, 1], [10, 1, 10], [1, 10, 1]], dtype=np.uint8) Iconv = cv2.filter2D(Iskel, -1, kern) Iskel[Iconv == 40] = 1 return Iskel