Shared pixels on shared edges

Viewed 89

enter image description here

I am trying to determine how to return coordinates of shared pixels from shared edges in an image (example image above). Essentially, three things would be returned.

  1. An image that draws over the shared edges in red (or another colour, it doesn't matter.
  2. A dataframe that lists the shared pixel coordinates.
  3. What I call a complexity index, which essentially lists which polygon has the most shared edges. So, using the image provided, polygon 1 has one shared edge, polygons 2 and 3 have three shared edges, polygon 4 has two shared edges, and polygon 5 has one shared edge.

I've had no real luck with this. Below is some code that I've attempted. I've started by doing contour selection and creating a nested dictionary (contour_df) with image contour data (contour_df) that lists all the pixel information from my image. A big problem I'm running into off the bat is that itertools.permutations is running every pixel coordinate combination, which takes forever, and I can't get past it to try anything else.

Ultimately, I believe I'm approaching this problem incorrectly and may need to start from scratch. Thanks for any and all suggestions.

def complexity_estimator(contour_df):

    adjacency_list = []
    for i in range(0, contour_df.shape[0]):
        if contour_df.iloc[i]["parent_index"] == -1:
            adjacency_list.append(0)
        else:
            # list coordinates for the contour we are interested In
            contour_coordinate = contour_df.iloc[i]["contour"]

            # list of coordinates of each of the siblings that we are interested In (list of listS)
            contour_coordinate_siblings = contour_df[contour_df["parent_index"] == contour_df.iloc[i]["parent_index"]]['contour'].values

            count = 0
            for sibling_contour in contour_coordinate_siblings:

                # compare contour_coordinate with sibling_contour
                adjacent = complexity_measure(contour_coordinate,sibling_contour)

                if adjacent == True:
                    count = count + 1

            adjacency_list.append(count)

    contour_df['complexity'] = adjacency_list

    return contour_df


def complexity_measure(contour_coordinates1, contour_coordinates2):

    if np.array_equal(contour_coordinates1, contour_coordinates2):
        return False
    else:
        new_pairs =  [list(zip(x,contour_coordinates2)) for x in itertools.permutations(contour_coordinates2,len(contour_coordinates2))]

        print (new_pairs)
        #dist = math.hypot(x2 - x1, y2 - y1)
    return True
0 Answers
Related