I couldn't find a similar question on here that gave a solution I was looking for, so I'm posting here. To expand on the title, I would like some method to calculate the 'perimeter' and 'area' of patches (with unit length and area) with similar values in a two-dimensional NumPy array.
The output would contain this information on each 'patch' in the array (the array will usually just have two or three values). Perhaps a dictionary with the value of the patch as the key, and a tuple with area and perimeter as the value, something like
patch_info_dict = {"patch1_value": (area1, perimeter1),
"patch2_value": (area2, perimeter2),
...}
For example, given these two 6x6 arrays:
import numpy as np
arr1 = np.array([[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]])
arr2 = np.array([[1,0,0,0,0,2],
[0,1,1,1,1,0],
[0,1,2,2,1,0],
[0,1,2,2,1,0],
[0,1,1,1,1,0],
[2,0,0,0,0,1]])
the information returned would be
# for arr1
patch_info_dict = {"1": (18, 18), "0": (18, 18)}
# for arr2
patch_info_dict = {"1": (1, 4), "1": (1, 4), "2": (1, 4), "2": (1, 4),
"0": (4, 10), "0": (4, 10), "0": (4, 10), "0": (4, 10),
"2": (4, 8), "1": (12, 24)}
As you can see, I want the perimeter to be the sum of the inner and outer perimeters (seen in the square ring of 1s in arr2) and elements that are diagonal from other elements of the same value are not part of the same patch.
I'm having trouble starting to write this program. Some brainstorming ideas I've had include identifying constraints, such as the area of each patch must obviously add up to the np.size() of the entire array, but I'm not sure if that's even useful.
Another idea I had was to convert this to a picture somehow, maybe using plt.matshow():
and then using some image analysis library to analyze the picture, but I don't know enough about these types of libraries.
If anyone has any pointers of even getting started or libraries/functions that might be useful, I'd appreciate the help. Thanks!