def test_relabel_from_one(): ar = np.array([1, 1, 5, 5, 8, 99, 42]) ar_relab, fw, inv = relabel_from_one(ar) ar_relab_ref = np.array([1, 1, 2, 2, 3, 5, 4]) assert_array_equal(ar_relab, ar_relab_ref) fw_ref = np.zeros(100, int) fw_ref[1] = 1; fw_ref[5] = 2; fw_ref[8] = 3; fw_ref[42] = 4; fw_ref[99] = 5 assert_array_equal(fw, fw_ref) inv_ref = np.array([0, 1, 5, 8, 42, 99]) assert_array_equal(inv, inv_ref)
def test_relabel_from_one(): ar = np.array([1, 1, 5, 5, 8, 99, 42]) ar_relab, fw, inv = relabel_from_one(ar) ar_relab_ref = np.array([1, 1, 2, 2, 3, 5, 4]) assert_array_equal(ar_relab, ar_relab_ref) fw_ref = np.zeros(100, int) fw_ref[1] = 1 fw_ref[5] = 2 fw_ref[8] = 3 fw_ref[42] = 4 fw_ref[99] = 5 assert_array_equal(fw, fw_ref) inv_ref = np.array([0, 1, 5, 8, 42, 99]) assert_array_equal(inv, inv_ref)
plt.imshow(elevation_map, cmap='spectral') plt.axis('off') plt.subplot(133) plt.imshow(regions, cmap='spectral') plt.axis('off') plt.tight_layout() # remove borders and relabel regions l0, l1 = img.shape indices_borders = [regions[0, 0], regions[0, l1/2], \ regions[l0 - 1, l1/2]] for index in indices_borders: regions[regions == index] = 0 seg, _, _ = segmentation.relabel_from_one(regions) # Compute neighbors # ------------------------------------------------------ neighbors = [] for lab in range(1, seg.max() + 1): dilation_of_region = morphology.binary_dilation(seg == lab, morphology.diamond(3)) neighbors.append(np.unique(seg[dilation_of_region])) res = measure.regionprops(seg, ['Area', 'Centroid']) areas = np.array([entry['Area'] for entry in res]) centroids = np.array([entry['Centroid'] for entry in res])