I have a 2D datapoint set (Nx2) and they are labelled (Nx1). I need to check if the minimum enclosing areas created by the datapoints with the same label are behaving well.
Well behaviour: having roughly the same shape (or at least same orientation) with others, not intruding the area of other shapes too much, touching 2 and exactly 2 edges if not top or bottom shape.
I created a sample code, I need to quantify a way to distinguish the cases like first plot from the cases like the second plot.
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull, convex_hull_plot_2d
datapoints = np.random.rand(1000,2)
label = np.zeros((1000))
label[:] = 3
label[datapoints[:, 1]<0.6]=2
label[datapoints[:, 1]<0.3]=1
plt.scatter(datapoints[:,0],datapoints[:,1], c=label)
for subset_label in range(1,4):
subset = datapoints[label==subset_label]
hull = ConvexHull(subset)
hull.close()
plt.plot(subset[hull.vertices,0], subset[hull.vertices,1], 'r-', lw=2)
plt.show()
label[:] = 3
label[datapoints[:, 1]<0.6]=2
label[np.logical_and(datapoints[:, 1]<0.9,datapoints[:,0]<0.15)]=1
label[np.logical_and(datapoints[:, 1]<0.3,datapoints[:,0]<0.5)]=1
label[np.logical_and(datapoints[:, 1]<0.3,datapoints[:,0]>0.7)]=4
fig, ax = plt.subplots()
ax.scatter(datapoints[:,0],datapoints[:,1], c=label)
for subset_label in range(1,5):
subset = datapoints[label==subset_label]
hull = ConvexHull(subset)
hull.close()
plt.plot(subset[hull.vertices,0], subset[hull.vertices,1], 'r-', lw=2)
plt.show()
I need things like regionprops from skimage.measure or contours from opencv. But it doesn't seem to be right in my case where I already have the "region" or the "contour".
Otherwise, I could check region moments (or orientation, eccentricity etc)
The sets here are nicely aligned, they have roughly the same shape, their orientation is very similar (horizontal), interior set touches 2 edges exactly
Blue set intruded the lower set and created 2 smaller sets. Blue set has very different shape than the others. Pink set has a rather vertical orientation than horizontal (as opposed to other sets).