I have a raster image. Using scipy, I am clustering pixels that belong to each peak in the histogram that corresponds to the values in the image. I am able to detect two clear peaks that are clearly segregated in the image. What I want to know is how much these points overlap. For each mask, I extract the "outline" using dilation and erosion. I compute the intersection of the outlines from the two peaks. The overlap ratio is calculated as: overlap_ratio = (number of overlapping outline pixels) / (minimum of the two outline sizes) I visualize the outlines and their overlap using a color-coded plot (yellow for Peak 1, blue for Peak 2, red for overlap). If the mean outline overlap percentage is greater than 20%, there is significant overlap. However, this method is not effective at identifying overlap when the cluster is thin (as in the second picture), so the method identifies more overlap than there really is. How can I create a more precise mask that compares the overlap in area of the cluster to the overlap in area of the other cluster? Would I be better off comparing polygons using shapely? Continue reading...