def hm(img): s1 = get_structure("rectangular") s2 = get_structure("elliptical") s3 = get_structure("cross-shaped") result1 = ndimage.binary_hit_or_miss(img, structure1=s1).astype(np.int) result2 = ndimage.binary_hit_or_miss(img, structure1=s2).astype(np.int) result3 = ndimage.binary_hit_or_miss(img, structure1=s3).astype(np.int) plt.subplot(2,2,1), plt.imshow(img, cmap="gray"), plt.title("Real Image") plt.subplot(2,2,2), plt.imshow(result1, cmap="gray"), plt.title("rectangular kernel") plt.subplot(2,2,3), plt.imshow(result2, cmap="gray"), plt.title("elliptical kernel") plt.subplot(2,2,4), plt.imshow(result3, cmap="gray"), plt.title("cross-shaped kernel") plt.show()
def fill(img): """both img and element should be binnary""" e = np.ones((3,3), np.int) e[1,1] = 0 interiorPoints = ndimage.binary_hit_or_miss(img, e).astype(np.int) img = img + interiorPoints return img.astype(np.uint8)
def clean(img): """both img and element should be binnary""" e = np.zeros((3,3), np.int) e[1,1] = 1 interiorPoints = ndimage.binary_hit_or_miss(img, e).astype(np.int) img = img - interiorPoints return img.astype(np.uint8)
def find1Cpixels(bwImage): """ identifies 1-connected pixels in image """ # fills pixels matching the following neighborhoods: hoods = [[[1, 0, 0], [0, 1, 0], [0, 0, 0]], [[0, 1, 0], [0, 1, 0], [0, 0, 0]], [[0, 0, 1], [0, 1, 0], [0, 0, 0]], [[0, 0, 0], [1, 1, 0], [0, 0, 0]], [[0, 0, 0], [0, 1, 1], [0, 0, 0]], [[0, 0, 0], [0, 1, 0], [1, 0, 0]], [[0, 0, 0], [0, 1, 0], [0, 1, 0]], [[0, 0, 0], [0, 1, 0], [0, 0, 1]]] output = np.zeros(bwImage.shape, dtype=np.bool) # for each neighborhood, find matching pixels and set them to 1 in the img for hood in hoods: output = np.logical_or(output, ndimage.binary_hit_or_miss(bwImage, hood)) return output
def drain_rois(img_data): """ Find all the ROIs in img_data and returns a similar volume with the ROIs emptied, keeping only their border voxels. This is useful for DTI tractography. @param img_data: numpy array @return: an array of same shape as img_data """ out = np.zeros(img_data.shape, dtype=img_data.dtype) if img_data.ndim == 2: kernel = np.ones([3, 3], dtype=int) elif img_data.ndim == 3: kernel = np.ones([3, 3, 3], dtype=int) elif img_data.ndim == 4: kernel = np.ones([3, 3, 3, 3], dtype=int) vals = np.unique(img_data) vals = vals[vals != 0] for i in vals: roi = img_data == i hits = scn.binary_hit_or_miss(roi, kernel) roi[hits] = 0 out[roi > 0] = i return out
def thining(img): ss = [] s = np.array([ [1,1], [1,1]], 'uint8') plt.subplot(1,2,1), plt.imshow(img, cmap="gray"), plt.title(str(2)) for i in range(12): img = ndimage.binary_hit_or_miss(img, structure1=s).astype(np.uint8) plt.subplot(1,2,2), plt.imshow(img, cmap="gray"), plt.title(str(2)) plt.show()
def __operationTask(self,input_var): ''' perform respective moephological operation on input block. Parameters ---------- input_var : type: 3d numpy array, ith block. Returns ------- output : type: 3d array, output of operation, ith block array. ''' D=self.__operationArgumentDic if self.__operation=="binary_closing": return ndimage.binary_closing(input_var, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif self.__operation=="binary_dilation": return ndimage.binary_dilation(input_var, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif self.__operation=="binary_erosion": return ndimage.binary_erosion(input_var, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif self.__operation=="binary_fill_holes": #the output might be different then scipy.ndimage return ndimage.binary_fill_holes(input_var, structure=D["structure"],output=D["output"], origin=D["origin"]) elif self.__operation=="binary_hit_or_miss": return ndimage.binary_hit_or_miss(input_var, structure1=D["structure1"],structure2=D["structure2"],output=D["output"], origin1=D["origin1"], origin2=D["origin2"]) elif self.__operation=="binary_opening": return ndimage.binary_opening(input_var, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif self.__operation=="binary_propagation": return ndimage.binary_propagation(input_var, structure=D["structure"],output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"]) elif self.__operation=="black_tophat": return ndimage.black_tophat(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="grey_dilation": return ndimage.grey_dilation(input_var, structure=D["structure"],size=D["size"], footprint=D["footprint"],output=D["output"], mode=D["mode"], cval=D["cval"], origin=D["origin"]) elif self.__operation=="grey_closing": return ndimage.grey_closing(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="grey_erosion": return ndimage.grey_erosion(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="grey_opening": return ndimage.grey_opening(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="morphological_gradient": return ndimage.morphological_gradient(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="morphological_laplace": return ndimage.morphological_laplace(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="white_tophat": return ndimage.white_tophat(input_var, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif self.__operation=="multiply": return input_var*D["scalar"] else: return input_var # no operation performed....
def return_ends(skeleton): ''' Find the endpoints of the skeleton. ''' end_points = [] for i, struct in enumerate(end_structs): hits = nd.binary_hit_or_miss(skeleton, structure1=struct) if not np.any(hits): continue for y, x in zip(*np.where(hits)): end_points.append((y, x)) return end_points
def finalPruning(self, skeleton): removedBranches = [] prunedSkeleton = skeleton for i in xrange(0, len(finalPruningKernel)): removedBranches.append( ndimage.binary_hit_or_miss(prunedSkeleton, finalPruningKernel[i]).astype(int)) finalRemovedBranches = removedBranches[0] for i in xrange(1, len(removedBranches)): finalRemovedBranches = np.logical_or( finalRemovedBranches, removedBranches[i]).astype(int) prunedSkeleton = np.logical_and( skeleton, np.logical_not(finalRemovedBranches).astype(int)).astype(int) return prunedSkeleton
def pruning(self, skeleton): prunedSkeleton = skeleton removedBranches = [] for i in xrange(0, len(pruningHitKernel)): removedBranches.append( ndimage.binary_hit_or_miss( prunedSkeleton, structure1=pruningHitKernel[i], structure2=pruningMissKernel[i]).astype(int)) finalRemovedBranches = removedBranches[0] for k in xrange(1, len(removedBranches)): finalRemovedBranches = np.logical_or( finalRemovedBranches, removedBranches[k]).astype(int) prunedSkeleton = np.logical_and( skeleton, np.logical_not(finalRemovedBranches).astype(int)).astype(int) return prunedSkeleton
def bwdiagfill(bwimage): """ clone of matlab's bwmorph(image,'diag') function? """ # fills pixels matching the following neighborhoods: hoods = [[[0, 1, 0], [1, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 0], [0, 0, 1], [0, 1, 0]], [[0, 1, 0], [0, 0, 1], [0, 0, 0]]] output = bwimage.copy() # for each neighborhood, find matching pixels and set them to 1 in the img for hood in hoods: output = np.logical_or(output, ndimage.binary_hit_or_miss(bwimage, hood)) return output
def skel(img): """skeletonize image""" print("Skel") hits = [ np.array([[0, 0, 0], [0, 1, 0], [1, 1, 1]]), np.array([[0, 0, 0], [1, 1, 0], [0, 1, 0]]) ] misses = [ np.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]), np.array([[0, 1, 1], [0, 0, 1], [0, 0, 0]]) ] for i in range(6): hits.append(np.transpose(hits[-2])[::-1, ...]) misses.append(np.transpose(misses[-2])[::-1, ...]) filters = zip(hits,misses) while True: prev = img for hit, miss in filters: filtered = ndimage.binary_hit_or_miss(img, hit, miss) img = np.logical_and(img, np.logical_not(filtered)) if np.abs(prev-img).max() == 0: break return img
def operationTask(input): D=operationArgumentDic #self.M.add_mem()#..................................................................................................... if operation=="binary_closing": return ndimage.binary_closing(input, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif operation=="binary_dilation": return ndimage.binary_dilation(input, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif operation=="binary_erosion": return ndimage.binary_erosion(input, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif operation=="binary_fill_holes": return ndimage.binary_fill_holes(input, structure=D["structure"],output=D["output"], origin=D["origin"]) elif operation=="binary_hit_or_miss": return ndimage.binary_hit_or_miss(input, structure1=D["structure1"],structure2=D["structure2"],output=D["output"], origin1=D["origin1"], origin2=D["origin2"]) elif operation=="binary_opening": return ndimage.binary_opening(input, structure=D["structure"], iterations=D["iterations"], output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"], brute_force=D["brute_force"]) elif operation=="binary_propagation": return ndimage.binary_propagation(input, structure=D["structure"],output=D["output"], origin=D["origin"], mask=D["mask"], border_value=D["border_value"]) elif operation=="black_tophat": return ndimage.black_tophat(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="grey_dilation": return ndimage.grey_dilation(input, structure=D["structure"],size=D["size"], footprint=D["footprint"],output=D["output"], mode=D["mode"], cval=D["cval"], origin=D["origin"]) elif operation=="grey_closing": return ndimage.grey_closing(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="grey_erosion": return ndimage.grey_erosion(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="grey_opening": return ndimage.grey_opening(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="morphological_gradient": return ndimage.morphological_gradient(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="morphological_laplace": return ndimage.morphological_laplace(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="white_tophat": return ndimage.white_tophat(input, structure=D["structure"], size=D["size"], footprint=D["footprint"], output=D["output"], origin=D["origin"],mode=D["mode"], cval=D["cval"]) elif operation=="intMultiply": return input*D["scalar"] else: return input
def skeleton(img): h1 = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 1]]) m1 = np.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]) h2 = np.array([[0, 0, 0], [1, 1, 0], [0, 1, 0]]) m2 = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) img = img.copy() while True: last = img for hit, miss in zip(hit_list, miss_list): hm = binary_hit_or_miss(img, hit, miss) # 删除白色点 img = np.logical_and(img, np.logical_not(hm)) if np.all(img == last): break return img
def drain_rois(img): """Find all the ROIs in img and returns a similar volume with the ROIs emptied, keeping only their border voxels. This is useful for DTI tractography. Parameters ---------- img: img-like object or str Can either be: - a file path to a Nifti image - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image. If niimg is a string, consider it as a path to Nifti image and call nibabel.load on it. If it is an object, check if get_data() and get_affine() methods are present, raise TypeError otherwise. Returns ------- np.ndarray an array of same shape as img_data """ img_data = get_img_data(img) out = np.zeros(img_data.shape, dtype=img_data.dtype) krn_dim = [3] * img_data.ndim kernel = np.ones(krn_dim, dtype=int) vals = np.unique(img_data) vals = vals[vals != 0] for i in vals: roi = img_data == i hits = scn.binary_hit_or_miss(roi, kernel) roi[hits] = 0 out[roi > 0] = i return out
def my_thinning(img: np.ndarray, se: np.ndarray) -> np.ndarray: """ """ return np.bitwise_xor(img, ndi.binary_hit_or_miss(img, se))
def _fill_gaps(wires, out): """Fill 1-minute gaps.""" mask = np.array([[1, 0, 1]]) return np.logical_or(wires, ndimage.binary_hit_or_miss(wires, mask), out=out)
def find_interestpoints(skeleton, plotFlag): B1 = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) B2 = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]]) B3 = np.array([[0, 1, 0], [1, 1, 1], [0, 0, 0]]) B4 = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 0]]) B5 = np.array([[0, 1, 0], [1, 1, 0], [0, 1, 0]]) B6 = np.array([[1, 0, 0], [0, 1, 0], [1, 0, 1]]) B7 = np.array([[0, 0, 0], [1, 1, 1], [0, 1, 0]]) B8 = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 1]]) B9 = np.array([[0, 1, 0], [0, 1, 1], [0, 1, 0]]) B10 = np.array([[1, 0, 1], [0, 1, 0], [0, 0, 1]]) B11 = np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0]]) B12 = np.array([[0, 1, 0], [1, 1, 0], [0, 0, 1]]) B13 = np.array([[1, 0, 0], [0, 1, 1], [1, 0, 0]]) B14 = np.array([[0, 0, 1], [1, 1, 0], [0, 1, 0]]) B15 = np.array([[0, 1, 0], [0, 1, 0], [1, 0, 1]]) B16 = np.rot90(B14) B17 = np.rot90(B15) B18 = np.rot90(B16) IMhit_mis = ( binary_hit_or_miss(skeleton, B1) + binary_hit_or_miss(skeleton, B2) + binary_hit_or_miss(skeleton, B3) + binary_hit_or_miss(skeleton, B4) + binary_hit_or_miss(skeleton, B5) + binary_hit_or_miss(skeleton, B6) + binary_hit_or_miss(skeleton, B7) + binary_hit_or_miss(skeleton, B8) + binary_hit_or_miss(skeleton, B9) + binary_hit_or_miss(skeleton, B10) + binary_hit_or_miss(skeleton, B11) + binary_hit_or_miss(skeleton, B12) + binary_hit_or_miss(skeleton, B13) + binary_hit_or_miss(skeleton, B14) + binary_hit_or_miss(skeleton, B15) + binary_hit_or_miss(skeleton, B16) + binary_hit_or_miss(skeleton, B17) + binary_hit_or_miss(skeleton, B18)) IMhit_mis_bin = IMhit_mis.astype(np.int) coordinates = np.nonzero(IMhit_mis_bin) if (plotFlag == 1): plt.figure(3) plt.imshow(skeleton) plt.scatter(x=coordinates[1], y=coordinates[0], c='r', s=20, marker='x') plt.show() return coordinates
def binary_hit_or_miss_transform(A, B=None, D=None): if B is None: B = np.ones((3, 3)) output = ndimage.binary_hit_or_miss(A, structure1=B).astype(np.uint8) return output
def test_binary_hit_or_miss_operation_dense_input_default_value(self): print("\n test_binary_hit_or_miss_operation_dense_input_default_value...") v_output = vc.binary_hit_or_miss(input_dvar, structure1=structure1, make_float32=False) d_output = ndimage.binary_hit_or_miss(input_dvar, structure1=structure1,) msgs = "test_binary_hit_or_miss_operation_dense_input_default_value" self.assertTrue((d_output==v_output).all(), msg=msgs)
def lengths(skeleton, verbose=False): ''' Length finding via morphology. ''' # 4-connected labels four_labels = label(skeleton, 4, background=0) four_sizes = nd.sum(skeleton, four_labels, range(np.max(four_labels) + 1)) # Lengths is the number of pixels minus number of objects with more # than 1 pixel. four_length = np.sum(four_sizes[four_sizes > 1]) - len( four_sizes[four_sizes > 1]) # Find pixels which a 4-connected and subtract them off the skeleton four_objects = np.where(four_sizes > 1)[0] skel_copy = copy(skeleton) for val in four_objects: skel_copy[np.where(four_labels == val)] = 0 # Remaining pixels are only 8-connected # Lengths is same as before, multiplied by sqrt(2) eight_labels = label(skel_copy, 8, background=0) eight_sizes = nd.sum(skel_copy, eight_labels, range(np.max(eight_labels) + 1)) eight_length = ( (np.sum(eight_sizes) - 1) - np.max(eight_labels)) * np.sqrt(2) # If there are no 4-connected pixels, we don't need the hit-miss portion. if four_length == 0.0: conn_length = 0.0 else: # Check 4 to 8-connected elements struct1 = np.array([[1, 0, 0], [0, 1, 1], [0, 0, 0]]) struct2 = np.array([[0, 0, 1], [1, 1, 0], [0, 0, 0]]) # Next check the three elements which will be double counted check1 = np.array([[1, 1, 0, 0], [0, 0, 1, 1]]) check2 = np.array([[0, 0, 1, 1], [1, 1, 0, 0]]) check3 = np.array([[1, 1, 0], [0, 0, 1], [0, 0, 1]]) store = np.zeros(skeleton.shape) # Loop through the 4 rotations of the structuring elements for k in range(0, 4): hm1 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(struct1, k=k)) store += hm1 hm2 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(struct2, k=k)) store += hm2 hm_check3 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(check3, k=k)) store -= hm_check3 if k <= 1: hm_check1 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(check1, k=k)) store -= hm_check1 hm_check2 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(check2, k=k)) store -= hm_check2 conn_length = np.sqrt(2) * np.sum(np.sum(store, axis=1), axis=0) # hits if verbose: print "Four Length: %s" % (four_length) print "Eight Length: %s" % (eight_length) print "Connect Length: %s" % (conn_length) return conn_length + eight_length + four_length
def main(): structure = np.ones((3, 3, 3)) #.............test1. dense....... filename = 'gyroidUniform.npy' input = np.load(filename, mmap_mode="r") print( "..............................dense.............................................." ) #0.Nothing.............. print("\n nothing testing...") output = vc.nothing(input, blockSize=50, fakeGhost=4, makeFloat32=False) print("\nresult: ", (input == output).all()) print(output.dtype, input.dtype) #1.grey_dilation.............. print("\ngrey_dilation VoxelProcessind") output = vc.grey_dilation(input, structure=structure, makeFloat32=False) print("\ngrey_dilation Default") d = ndimage.grey_dilation(input, structure=structure) print("\nresult: ", (d == output).all()) print(output.dtype, input.dtype) #2.grey_erosion.............. print("\ngrey_erosion VoxelProcessind") output = vc.grey_erosion(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\ngrey_erosion Default") d = ndimage.grey_erosion(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) #3.grey_closing.............. print("\ngrey_closing VoxelProcessind") output = vc.grey_closing(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\ngrey_closing Default") d = ndimage.grey_closing(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) print(output.dtype, input.dtype) #4.grey_opening.............. print("\ngrey_opening VoxelProcessind") output = vc.grey_opening(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\ngrey_opening Default") d = ndimage.grey_opening(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) #5.binary_closing.............. print("\nbinary_closing VoxelProcessind") output = vc.binary_closing(input, makeFloat32=False, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False) print("\nbinary_closing Default") d = ndimage.binary_closing(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False) print("\nresult: ", (d == output).all()) print(output[151][151][151]) #6.binary_opening.............. print("\nbinary_opening VoxelProcessind") output = vc.binary_opening(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False) print("\nbinary_opening Default") d = ndimage.binary_opening(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False) print("\nresult: ", (d == output).all()) #7.binary_dilation.............. print("\nbinary_dilation VoxelProcessind") output = vc.binary_dilation(input, makeFloat32=False, structure=structure, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False) print("\nbinary_dilation Default") d = ndimage.binary_dilation(input, structure=structure, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False) print("\nresult: ", (d == output).all()) #8.binary_erosion.............. print("\nbinary_erosion VoxelProcessind") output = vc.binary_erosion(input, makeFloat32=False, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False) print("\nbinary_erosion Default") d = ndimage.binary_erosion(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False) print("\nresult: ", (d == output).all()) #9.binary_fill_holes.............. print("\nbinary_fill_holes VoxelProcessind") output = vc.binary_fill_holes(input, makeFloat32=False, structure=None, output=None, origin=0) print("\nbinary_fill_holes Default") d = ndimage.binary_fill_holes(input, structure=None, output=None, origin=0) print("\nresult: ", (d == output).all()) #10.binary_hit_or_miss.............. print("\nbinary_hit_or_miss VoxelProcessind") output = vc.binary_hit_or_miss(input, makeFloat32=False, structure1=None, structure2=None, output=None, origin1=0, origin2=None) print("\nbinary_hit_or_miss Default") d = ndimage.binary_hit_or_miss(input, structure1=None, structure2=None, output=None, origin1=0, origin2=None) print("\nresult: ", (d == output).all()) #11.binary_propagation.............. print("\nbinary_propagation VoxelProcessind") output = vc.binary_propagation(input, makeFloat32=False, structure=None, mask=None, output=None, border_value=0, origin=0) print("\nbinary_propagation Default") d = ndimage.binary_propagation(input, structure=None, mask=None, output=None, border_value=0, origin=0) print("\nresult: ", (d == output).all()) #12.black_tophat.............. print("\nblack_tophat VoxelProcessind") output = vc.black_tophat(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nblack_tophat Default") d = ndimage.black_tophat(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) #13.morphological_gradient.............. print("\nmorphological_gradient VoxelProcessind") output = vc.morphological_gradient(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nmorphological_gradient Default") d = ndimage.morphological_gradient(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) #14.morphological_laplace.............. print("\nmorphological_laplace VoxelProcessind") output = vc.morphological_laplace(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nmorphological_laplace Default") d = ndimage.morphological_laplace(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) #15.white_tophat.............. print("\nwhite_tophat VoxelProcessind") output = vc.white_tophat(input, makeFloat32=False, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nwhite_tophat VoxelProcessind Default") d = ndimage.white_tophat(input, size=None, footprint=None, structure=structure, output=None, mode='reflect', cval=0.0, origin=0) print("\nresult: ", (d == output).all()) #16.intMultiply.............. print("\nintMultiply VoxelProcessind") output = vc.intMultiply(input, makeFloat32=False, blockSize=50, fakeGhost=1, scalar=10) print("\nintMultiply Default") d = input * 10 print("\nresult: ", (d == output).all()) print( "..............................Sparse.............................................." ) input = random(400, 80000, density=0.3, dtype="float64") input = input.todense() input = np.array(input) input = np.reshape(input, (400, 200, 400)) #0.Nothing.............. print("\n nothing testing...") output = vc.nothing(input, makeFloat32=False) print("\nresult: ", (input == output).all()) print(output.dtype, input.dtype) #1.grey_dilation.............. print("\ngrey_dilation VoxelProcessind") output = vc.grey_dilation(input, structure=structure, makeFloat32=False) print("\ngrey_dilation Default") d = ndimage.grey_dilation(input, structure=structure) print("\nresult: ", (d == output).all()) print(output.dtype, input.dtype) #2.grey_erosion.............. print("\ngrey_erosion VoxelProcessind") output = vc.grey_erosion(input, makeFloat32=False, structure=structure) print("\ngrey_erosion Default") d = ndimage.grey_erosion(input, structure=structure) print("\nresult: ", (d == output).all()) #3.grey_closing.............. print("\ngrey_closing VoxelProcessind") output = vc.grey_closing(input, makeFloat32=False, structure=structure) print("\ngrey_closing Default") d = ndimage.grey_closing(input, structure=structure) print("\nresult: ", (d == output).all()) print(output.dtype, input.dtype) #4.grey_opening.............. print("\ngrey_opening VoxelProcessind") output = vc.grey_opening(input, makeFloat32=False, structure=structure) print("\ngrey_opening Default") d = ndimage.grey_opening(input, structure=structure) print("\nresult: ", (d == output).all()) #5.binary_closing.............. print("\nbinary_closing VoxelProcessind") output = vc.binary_closing(input, makeFloat32=False) print("\nbinary_closing Default") d = ndimage.binary_closing(input) print("\nresult: ", (d == output).all()) #6.binary_opening.............. print("\nbinary_opening VoxelProcessind") output = vc.binary_opening(input, makeFloat32=False) print("\nbinary_opening Default") d = ndimage.binary_opening(input) print("\nresult: ", (d == output).all()) #7.binary_dilation.............. print("\nbinary_dilation VoxelProcessind") output = vc.binary_dilation(input, makeFloat32=False, structure=structure) print("\nbinary_dilation Default") d = ndimage.binary_dilation(input, structure=structure) print("\nresult: ", (d == output).all()) #8.binary_erosion.............. print("\nbinary_erosion VoxelProcessind") output = vc.binary_erosion(input, makeFloat32=False) print("\nbinary_erosion Default") d = ndimage.binary_erosion(input) print("\nresult: ", (d == output).all()) #9.binary_fill_holes.............. print("\nbinary_fill_holes VoxelProcessind") output = vc.binary_fill_holes(input, makeFloat32=False) print("\nbinary_fill_holes Default") d = ndimage.binary_fill_holes(input) print("\nresult: ", (d == output).all()) #10.binary_hit_or_miss.............. print("\nbinary_hit_or_miss VoxelProcessind") output = vc.binary_hit_or_miss(input, makeFloat32=False) print("\nbinary_hit_or_miss Default") d = ndimage.binary_hit_or_miss(input) print("\nresult: ", (d == output).all()) #11.binary_propagation.............. print("\nbinary_propagation VoxelProcessind") output = vc.binary_propagation(input, makeFloat32=False) print("\nbinary_propagation Default") d = ndimage.binary_propagation(input) print("\nresult: ", (d == output).all()) #12.black_tophat.............. print("\nblack_tophat VoxelProcessind") output = vc.black_tophat(input, makeFloat32=False, structure=structure) print("\nblack_tophat Default") d = ndimage.black_tophat(input, structure=structure) print("\nresult: ", (d == output).all()) #13.morphological_gradient.............. print("\nmorphological_gradient VoxelProcessind") output = vc.morphological_gradient( input, structure=structure, makeFloat32=False, ) print("\nmorphological_gradient Default") d = ndimage.morphological_gradient(input, structure=structure) print("\nresult: ", (d == output).all()) #14.morphological_laplace.............. print("\nmorphological_laplace VoxelProcessind") output = vc.morphological_laplace(input, structure=structure, makeFloat32=False) print("\nmorphological_laplace Default") d = ndimage.morphological_laplace(input, structure=structure) print("\nresult: ", (d == output).all()) #15.white_tophat.............. print("\nwhite_tophat VoxelProcessind") output = vc.white_tophat(input, makeFloat32=False, structure=structure) print("\nwhite_tophat VoxelProcessind Default") d = ndimage.white_tophat(input, structure=structure) print("\nresult: ", (d == output).all()) #16.intMultiply.............. print("\nintMultiply VoxelProcessind") output = vc.intMultiply(input, makeFloat32=False, scalar=10) print("\nintMultiply Default") d = input * 10 print("\nresult: ", (d == output).all())
B10 = np.array([[1, 0, 1], [0, 1, 0], [0, 0, 1]]) B11 = np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0]]) B12 = np.array([[0, 1, 0], [1, 1, 0], [0, 0, 1]]) B13 = np.array([[1, 0, 0], [0, 1, 1], [1, 0, 0]]) B14 = np.array([[0, 0, 1], [1, 1, 0], [0, 1, 0]]) B15 = np.array([[0, 1, 0], [0, 1, 0], [1, 0, 1]]) B16 = np.rot90(B14) B17 = np.rot90(B15) B18 = np.rot90(B16) IMhit_mis = ( binary_hit_or_miss(skeleton, B1) + binary_hit_or_miss(skeleton, B2) + binary_hit_or_miss(skeleton, B3) + binary_hit_or_miss(skeleton, B4) + binary_hit_or_miss(skeleton, B5) + binary_hit_or_miss(skeleton, B6) + binary_hit_or_miss(skeleton, B7) + binary_hit_or_miss(skeleton, B8) + binary_hit_or_miss(skeleton, B9) + binary_hit_or_miss(skeleton, B10) + binary_hit_or_miss(skeleton, B11) + binary_hit_or_miss(skeleton, B12) + binary_hit_or_miss(skeleton, B13) + binary_hit_or_miss(skeleton, B14) + binary_hit_or_miss(skeleton, B15) + binary_hit_or_miss(skeleton, B16) + binary_hit_or_miss(skeleton, B17) + binary_hit_or_miss(skeleton, B18)) IMhit_mis_bin = IMhit_mis.astype(np.int) coordinates = np.nonzero(IMhit_mis_bin) coordinates = np.nonzero(IMhit_mis_bin) plt.figure(3)
def _getnodes(Xin): # hits structures = [] structures.append([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) structures.append([[0, 1, 0], [0, 1, 0], [0, 0, 0]]) structures.append([[0, 0, 1], [0, 1, 0], [0, 0, 0]]) structures.append([[0, 0, 0], [0, 1, 1], [0, 0, 0]]) structures.append([[0, 0, 0], [0, 1, 0], [0, 0, 1]]) structures.append([[0, 0, 0], [0, 1, 0], [0, 1, 0]]) structures.append([[0, 0, 0], [0, 1, 0], [1, 0, 0]]) structures.append([[0, 0, 0], [1, 1, 0], [0, 0, 0]]) #structures.append([[1, 1, 1], [0, 1, 1], [1, 0, 0]]) #structures.append([[1, 1, 1], [1, 1, 0], [0, 0, 1]]) #structures.append([[1, 0, 0], [0, 1, 1], [1, 1, 1]]) #structures.append([[0, 0, 1], [1, 1, 0], [1, 1, 1]]) crossings = [[0, 1, 0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0, 1, 0], [0, 0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 1], [0, 1, 0, 1, 0, 0, 0, 1], [0, 1, 0, 1, 0, 1, 0, 0], [0, 0, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0, 1, 0], [1, 0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1, 1, 1], [1, 1, 0, 0, 1, 0, 0, 1], [0, 1, 1, 1, 0, 0, 1, 0], [1, 0, 1, 1, 0, 0, 1, 0], [1, 0, 1, 0, 0, 1, 1, 0], [1, 0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 1, 0, 1, 1], [1, 1, 0, 1, 1, 0, 1, 0], [1, 1, 0, 0, 1, 0, 1, 0], [0, 1, 1, 0, 1, 0, 1, 0], [0, 0, 1, 0, 1, 0, 1, 1], [1, 0, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0, 1, 1, 0, 1], [1, 0, 1, 0, 1, 1, 0, 0], [1, 0, 1, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 1, 1], [0, 1, 1, 0, 1, 0, 0, 1], [1, 1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1, 0], [0, 0, 1, 0, 1, 1, 0, 1], [1, 0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 1, 1, 0], [1, 0, 1, 1, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0, 1], [1, 1, 0, 1, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 1, 1, 1]] for i in range(len(crossings)): A = crossings[i] B = np.ones((3, 3)) B[1, 0] = A[0] B[0, 0] = A[1] B[0, 1] = A[2] B[0, 2] = A[3] B[1, 2] = A[4] B[2, 2] = A[5] B[2, 1] = A[6] B[2, 0] = A[7] structures.append(B.tolist()) nodes = [] for i in range(len(structures)): structure1 = np.array(structures[i]) X0 = spim.binary_hit_or_miss(Xin, structure1=structure1).astype(int) r0, c0 = np.nonzero(X0 == 1) for j in range(len(r0)): nodes.append([c0[j], r0[j]]) nodes = np.array(nodes) return nodes
# hit and miss transform for detecting branched point and endpoint in scikit-image from scipy import ndimage ndimage.binary_hit_or_miss(...)
def lengths(skeleton, verbose=False): ''' Length finding via morphology. ''' # 4-connected labels four_labels = label(skeleton, 4, background=0) four_sizes = nd.sum(skeleton, four_labels, range(np.max(four_labels) + 1)) # Lengths is the number of pixels minus number of objects with more # than 1 pixel. four_length = np.sum( four_sizes[four_sizes > 1]) - len(four_sizes[four_sizes > 1]) # Find pixels which a 4-connected and subtract them off the skeleton four_objects = np.where(four_sizes > 1)[0] skel_copy = copy(skeleton) for val in four_objects: skel_copy[np.where(four_labels == val)] = 0 # Remaining pixels are only 8-connected # Lengths is same as before, multiplied by sqrt(2) eight_labels = label(skel_copy, 8, background=0) eight_sizes = nd.sum( skel_copy, eight_labels, range(np.max(eight_labels) + 1)) eight_length = ( (np.sum(eight_sizes) - 1) - np.max(eight_labels)) * np.sqrt(2) # If there are no 4-connected pixels, we don't need the hit-miss portion. if four_length == 0.0: conn_length = 0.0 else: # Check 4 to 8-connected elements struct1 = np.array([[1, 0, 0], [0, 1, 1], [0, 0, 0]]) struct2 = np.array([[0, 0, 1], [1, 1, 0], [0, 0, 0]]) # Next check the three elements which will be double counted check1 = np.array([[1, 1, 0, 0], [0, 0, 1, 1]]) check2 = np.array([[0, 0, 1, 1], [1, 1, 0, 0]]) check3 = np.array([[1, 1, 0], [0, 0, 1], [0, 0, 1]]) store = np.zeros(skeleton.shape) # Loop through the 4 rotations of the structuring elements for k in range(0, 4): hm1 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(struct1, k=k)) store += hm1 hm2 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(struct2, k=k)) store += hm2 hm_check3 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(check3, k=k)) store -= hm_check3 if k <= 1: hm_check1 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(check1, k=k)) store -= hm_check1 hm_check2 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(check2, k=k)) store -= hm_check2 conn_length = np.sqrt( 2) * np.sum(np.sum(store, axis=1), axis=0) # hits if verbose: print "Four Length: %s" % (four_length) print "Eight Length: %s" % (eight_length) print "Connect Length: %s" % (conn_length) return conn_length + eight_length + four_length
def test_binary_hit_or_miss_operation_sparse_input_blocks_ten(self): print("\n test_binary_hit_or_miss_operation_sparse_input_blocks_ten...") v_output = vc.binary_hit_or_miss(input_svar, structure1=structure1, no_of_blocks=10,make_float32=False) d_output = ndimage.binary_hit_or_miss(input_svar, structure1=structure1,) msgs = "test_binary_hit_or_miss_operation_sparse_input_blocks_ten" self.assertTrue((d_output==v_output).all(), msg=msgs)
def skeleton_length(skeleton): ''' Length finding via morphological operators. We use the differences in connectivity between 4 and 8-connected to split regions. Connections between 4 and 8-connected regions are found using a series of hit-miss operators. The inputted skeleton MUST have no intersections otherwise the returned length will not be correct! Parameters ---------- skeleton : numpy.ndarray Array containing the skeleton. Returns ------- length : float Length of the skeleton. ''' # 4-connected labels four_labels = nd.label(skeleton)[0] four_sizes = nd.sum(skeleton, four_labels, range(np.max(four_labels) + 1)) # Lengths is the number of pixels minus number of objects with more # than 1 pixel. four_length = np.sum( four_sizes[four_sizes > 1]) - len(four_sizes[four_sizes > 1]) # Find pixels which a 4-connected and subtract them off the skeleton four_objects = np.where(four_sizes > 1)[0] skel_copy = copy.copy(skeleton) for val in four_objects: skel_copy[np.where(four_labels == val)] = 0 # Remaining pixels are only 8-connected # Lengths is same as before, multiplied by sqrt(2) eight_labels = nd.label(skel_copy, eight_con())[0] eight_sizes = nd.sum( skel_copy, eight_labels, range(np.max(eight_labels) + 1)) eight_length = ( np.sum(eight_sizes) - np.max(eight_labels)) * np.sqrt(2) # If there are no 4-connected pixels, we don't need the hit-miss portion. if four_length == 0.0: conn_length = 0.0 else: store = np.zeros(skeleton.shape) # Loop through the 4 rotations of the structuring elements for k in range(0, 4): hm1 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(struct1, k=k)) store += hm1 hm2 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(struct2, k=k)) store += hm2 hm_check3 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(check3, k=k)) store -= hm_check3 if k <= 1: hm_check1 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(check1, k=k)) store -= hm_check1 hm_check2 = nd.binary_hit_or_miss( skeleton, structure1=np.rot90(check2, k=k)) store -= hm_check2 conn_length = np.sqrt(2) * \ np.sum(np.sum(store, axis=1), axis=0) # hits return conn_length + eight_length + four_length
def branchedPoints(skel): # defining branch shapes to locate nodes # overexplained this section a bit xbranch0 = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]]) xbranch1 = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) tbranch0 = np.array([[0, 0, 0], [1, 1, 1], [0, 1, 0]]) # flipud is flipping them up-down # tbranch2 is tbranch0 transposed, which permutes it in all directions (might not be using that word right) # tbranch3 is tbranch2 flipped left right # those 3 functions are used to create all possible branches with just a few starting arrays below tbranch1 = np.flipud(tbranch0) tbranch2 = tbranch0.T tbranch3 = np.fliplr(tbranch2) tbranch4 = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 0]]) tbranch5 = np.flipud(tbranch4) tbranch6 = np.fliplr(tbranch4) tbranch7 = np.fliplr(tbranch5) ybranch0 = np.array([[1, 0, 1], [0, 1, 0], [0, 1, 0]]) ybranch1 = np.flipud(ybranch0) ybranch2 = ybranch0.T ybranch3 = np.fliplr(ybranch2) ybranch4 = np.array([[0, 1, 0], [1, 1, 0], [0, 0, 1]]) ybranch5 = np.flipud(ybranch4) ybranch6 = np.fliplr(ybranch4) ybranch7 = np.fliplr(ybranch5) offbranch0 = np.array([[0, 1, 0], [1, 1, 0], [1, 0, 1]]) offbranch1 = np.flipud(offbranch0) offbranch2 = np.fliplr(offbranch0) offbranch3 = np.fliplr(offbranch1) offbranch4 = offbranch0.T offbranch5 = np.flipud(offbranch4) offbranch6 = np.fliplr(offbranch4) offbranch7 = np.fliplr(offbranch5) clustbranch0 = np.array([[0, 1, 1], [0, 1, 1], [1, 0, 0]]) clustbranch1 = np.flipud(clustbranch0) clustbranch2 = np.fliplr(clustbranch0) clustbranch3 = np.fliplr(clustbranch1) clustbranch4 = np.array([[1, 1, 1], [0, 1, 1], [1, 0, 0]]) clustbranch5 = np.flipud(clustbranch4) clustbranch6 = np.fliplr(clustbranch4) clustbranch7 = np.fliplr(clustbranch5) clustbranch8 = np.array([[1, 1, 1], [0, 1, 1], [1, 0, 1]]) clustbranch9 = np.flipud(clustbranch8) clustbranch10 = np.fliplr(clustbranch8) clustbranch11 = np.fliplr(clustbranch9) crossbranch0 = np.array([[1, 0, 0], [1, 1, 1], [0, 1, 0]]) crossbranch1 = np.flipud(crossbranch0) crossbranch2 = np.fliplr(crossbranch0) crossbranch3 = np.fliplr(crossbranch1) crossbranch4 = crossbranch0.T crossbranch5 = np.flipud(crossbranch4) crossbranch6 = np.fliplr(crossbranch4) crossbranch7 = np.fliplr(crossbranch5) # finding the location of all the branch points based on the arrays above br1 = ndimage.binary_hit_or_miss(skel, xbranch0) br2 = ndimage.binary_hit_or_miss(skel, xbranch1) br3 = ndimage.binary_hit_or_miss(skel, tbranch0) br4 = ndimage.binary_hit_or_miss(skel, tbranch1) br5 = ndimage.binary_hit_or_miss(skel, tbranch2) br6 = ndimage.binary_hit_or_miss(skel, tbranch3) br7 = ndimage.binary_hit_or_miss(skel, tbranch4) br8 = ndimage.binary_hit_or_miss(skel, tbranch5) br9 = ndimage.binary_hit_or_miss(skel, tbranch6) br10 = ndimage.binary_hit_or_miss(skel, tbranch7) br11 = ndimage.binary_hit_or_miss(skel, ybranch0) br12 = ndimage.binary_hit_or_miss(skel, ybranch1) br13 = ndimage.binary_hit_or_miss(skel, ybranch2) br14 = ndimage.binary_hit_or_miss(skel, ybranch3) br15 = ndimage.binary_hit_or_miss(skel, ybranch4) br16 = ndimage.binary_hit_or_miss(skel, ybranch5) br17 = ndimage.binary_hit_or_miss(skel, ybranch6) br18 = ndimage.binary_hit_or_miss(skel, ybranch7) br19 = ndimage.binary_hit_or_miss(skel, offbranch0) br20 = ndimage.binary_hit_or_miss(skel, offbranch1) br21 = ndimage.binary_hit_or_miss(skel, offbranch2) br22 = ndimage.binary_hit_or_miss(skel, offbranch3) br23 = ndimage.binary_hit_or_miss(skel, offbranch4) br24 = ndimage.binary_hit_or_miss(skel, offbranch5) br25 = ndimage.binary_hit_or_miss(skel, offbranch6) br26 = ndimage.binary_hit_or_miss(skel, offbranch7) br27 = ndimage.binary_hit_or_miss(skel, clustbranch0) br28 = ndimage.binary_hit_or_miss(skel, clustbranch1) br29 = ndimage.binary_hit_or_miss(skel, clustbranch2) br30 = ndimage.binary_hit_or_miss(skel, clustbranch3) br31 = ndimage.binary_hit_or_miss(skel, clustbranch4) br32 = ndimage.binary_hit_or_miss(skel, clustbranch5) br33 = ndimage.binary_hit_or_miss(skel, clustbranch6) br34 = ndimage.binary_hit_or_miss(skel, clustbranch7) br35 = ndimage.binary_hit_or_miss(skel, clustbranch8) br36 = ndimage.binary_hit_or_miss(skel, clustbranch9) br37 = ndimage.binary_hit_or_miss(skel, clustbranch10) br38 = ndimage.binary_hit_or_miss(skel, clustbranch11) br39 = ndimage.binary_hit_or_miss(skel, crossbranch0) br40 = ndimage.binary_hit_or_miss(skel, crossbranch1) br41 = ndimage.binary_hit_or_miss(skel, crossbranch2) br42 = ndimage.binary_hit_or_miss(skel, crossbranch3) br43 = ndimage.binary_hit_or_miss(skel, crossbranch4) br44 = ndimage.binary_hit_or_miss(skel, crossbranch5) br45 = ndimage.binary_hit_or_miss(skel, crossbranch6) br46 = ndimage.binary_hit_or_miss(skel, crossbranch7) br = br1+br2+br3+br4+br5+br6+br7+br8+br9+br10+br11+br12+br13+br14+br15+br16+br17+br18+br19+br20+br21+br22+br23+br24\ +br25+br26+br27+br28+br29+br30+br31+br32+br33+br34+br35+br36+br37+br38+br39+br40+br41+br42+br43+br44+br45+br46 return br
#10.binary_hit_or_miss.............. print("\nbinary_hit_or_miss VoxelProcessing") start_time = t.time() output = vc.binary_hit_or_miss(input_var, make_float32=False, structure1=structure, structure2=None, output=None, origin1=0, origin2=None) print("vc binary_hit_or_miss: ", (t.time() - start_time), " sec") print("\nbinary_hit_or_miss Default") start_time = t.time() d = ndimage.binary_hit_or_miss(input_var, structure1=structure, structure2=None, output=None, origin1=0, origin2=None) print("scipy binary_hit_or_miss: ", (t.time() - start_time), " sec") print("\nresult: ", (d == output).all()) #11.binary_propagation.............. print("\nbinary_propagation VoxelProcessing") start_time = t.time() output = vc.binary_propagation(input_var, make_float32=False, structure=structure, mask=None, output=None, border_value=0, origin=0)
def skeleton_length(skeleton): ''' Length finding via morphological operators. We use the differences in connectivity between 4 and 8-connected to split regions. Connections between 4 and 8-connected regions are found using a series of hit-miss operators. The inputted skeleton MUST have no intersections otherwise the returned length will not be correct! Parameters ---------- skeleton : numpy.ndarray Array containing the skeleton. Returns ------- length : float Length of the skeleton. ''' # 4-connected labels four_labels = nd.label(skeleton)[0] four_sizes = nd.sum(skeleton, four_labels, range(np.max(four_labels) + 1)) # Lengths is the number of pixels minus number of objects with more # than 1 pixel. four_length = np.sum(four_sizes[four_sizes > 1]) - len( four_sizes[four_sizes > 1]) # Find pixels which a 4-connected and subtract them off the skeleton four_objects = np.where(four_sizes > 1)[0] skel_copy = copy.copy(skeleton) for val in four_objects: skel_copy[np.where(four_labels == val)] = 0 # Remaining pixels are only 8-connected # Lengths is same as before, multiplied by sqrt(2) eight_labels = nd.label(skel_copy, eight_con())[0] eight_sizes = nd.sum(skel_copy, eight_labels, range(np.max(eight_labels) + 1)) eight_length = (np.sum(eight_sizes) - np.max(eight_labels)) * np.sqrt(2) # If there are no 4-connected pixels, we don't need the hit-miss portion. if four_length == 0.0: conn_length = 0.0 else: store = np.zeros(skeleton.shape) # Loop through the 4 rotations of the structuring elements for k in range(0, 4): hm1 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(struct1, k=k)) store += hm1 hm2 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(struct2, k=k)) store += hm2 hm_check3 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(check3, k=k)) store -= hm_check3 if k <= 1: hm_check1 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(check1, k=k)) store -= hm_check1 hm_check2 = nd.binary_hit_or_miss(skeleton, structure1=np.rot90(check2, k=k)) store -= hm_check2 conn_length = np.sqrt(2) * \ np.sum(np.sum(store, axis=1), axis=0) # hits return conn_length + eight_length + four_length
ifname = args.input.strip() ofname = args.output.strip() ofname = add_extension_if_needed(ofname, '.nii.gz') aff = nib.load(ifname).get_affine() vol = nib.load(ifname).get_data() out = np.zeros(vol.shape, dtype=vol.dtype) if vol.ndim == 2: kernel = np.ones([3,3], dtype=int) elif vol.ndim == 3: kernel = np.ones([3,3,3], dtype=int) elif vol.ndim == 4: kernel = np.ones([3,3,3,3], dtype=int) vals = np.unique(vol) vals = vals[vals != 0] for i in vals: roi = vol == i hits = scn.binary_hit_or_miss (roi, kernel) roi[hits] = 0 out[roi > 0] = i ni = nib.Nifti1Image(out, aff) nib.save(ni, ofname) #------------------------------------------------------------------------------- if __name__ == "__main__": sys.exit(main())
def test_binary_hit_or_miss_operation_sparse_input_fakeghost_four(self): print("\n test_binary_hit_or_miss_operation_sparse_input_fakeghost_four...") v_output = vc.binary_hit_or_miss(input_svar,structure1=structure1, fakeghost=4,make_float32=False) d_output = ndimage.binary_hit_or_miss(input_svar,structure1=structure1,) msgs = "test_binary_hit_or_miss_operation_sparse_input_fakeghost_four" self.assertTrue((d_output==v_output).all(), msg=msgs)
def endPoints(skel): ## # Function returns boolen array that contains True values in the coordinates that fit as the endpoint of the fibers. # arguments: # - ‘skel’: array with skeletonized fibers # function returns: # - 'endp': boolen array that contains True values in the coordinates that fit as the endpoint of the fibers ## epoint1 = np.array([[0, 0, 0], [0, 1, 0], [0, 1, 0]]) epoint2 = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 1]]) epoint3 = np.array([[0, 0, 0], [0, 1, 1], [0, 0, 0]]) epoint4 = np.array([[0, 0, 1], [0, 1, 0], [0, 0, 0]]) epoint5 = np.array([[0, 1, 0], [0, 1, 0], [0, 0, 0]]) epoint6 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) epoint7 = np.array([[0, 0, 0], [1, 1, 0], [0, 0, 0]]) epoint8 = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0]]) epoint9 = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 0]]) epoint10 = np.array([[0, 0, 0], [0, 1, 0], [0, 1, 1]]) epoint11 = np.array([[0, 0, 0], [0, 1, 1], [0, 0, 1]]) epoint12 = np.array([[0, 0, 1], [0, 1, 1], [0, 0, 0]]) epoint13 = np.array([[0, 1, 1], [0, 1, 0], [0, 0, 0]]) epoint14 = np.array([[1, 1, 0], [0, 1, 0], [0, 0, 0]]) epoint15 = np.array([[1, 0, 0], [1, 1, 0], [0, 0, 0]]) epoint16 = np.array([[0, 0, 0], [1, 1, 0], [1, 0, 0]]) endp1 = binary_hit_or_miss(skel, structure1=epoint1) endp2 = binary_hit_or_miss(skel, structure1=epoint2) endp3 = binary_hit_or_miss(skel, structure1=epoint3) endp4 = binary_hit_or_miss(skel, structure1=epoint4) endp5 = binary_hit_or_miss(skel, structure1=epoint5) endp6 = binary_hit_or_miss(skel, structure1=epoint6) endp7 = binary_hit_or_miss(skel, structure1=epoint7) endp8 = binary_hit_or_miss(skel, structure1=epoint8) endp9 = binary_hit_or_miss(skel, structure1=epoint9) endp10 = binary_hit_or_miss(skel, structure1=epoint10) endp11 = binary_hit_or_miss(skel, structure1=epoint11) endp12 = binary_hit_or_miss(skel, structure1=epoint12) endp13 = binary_hit_or_miss(skel, structure1=epoint13) endp14 = binary_hit_or_miss(skel, structure1=epoint14) endp15 = binary_hit_or_miss(skel, structure1=epoint15) endp16 = binary_hit_or_miss(skel, structure1=epoint16) endp = endp1 + \ endp2 + \ endp3 + \ endp4 + \ endp5 + \ endp6 + \ endp7 + \ endp8 + \ endp9 + \ endp10 + \ endp11 + \ endp12 + \ endp13 + \ endp14 + \ endp15 + \ endp16 return endp