I am working to cluster probabilistic hough lines together using unit vectors. The clustering changes every run though and is not quite right. I want to cluster the lines of [this image][2]. But I am getting this clustering and it changes drastically every run though. I know the probabilistic hough changes slightly every run but I would like the keep the merged lines pretty consistent. Is the problem with the way I am calculating unit-vector or DBSCAN or is there a better way to do clustering. Any help would be appreciated.
line_dict = []
# using hough lines thru skimage.transform- probabilistic_hough_line
for line in lines:
meta_lines = {}
start_point, end_point = line
# line equations and add line info to line dictionary
meta_lines["start"] = start_point
meta_lines["end"] = end_point
distance = [end_point[0] - start_point[0], end_point[1] - start_point[1]]
norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
direction = [distance[0] / norm, distance[1] / norm]
meta_lines["unit-vector"] = direction
line_dict.append(meta_lines)
#clustering of lines using DBSCAN
X = StandardScaler().fit_transform([x["unit-vector"] for x in line_dict])
db = DBSCAN(eps=0.2, min_samples=1).fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_
# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
clusters = [X[labels == i] for i in range(n_clusters_)]
#cluster start/end poitns of lines
for c in range(len(clusters)):
for i in range(len(line_dict)):
line_dict[i]["scale"] = X[i]
if line_dict[i]["scale"] in clusters[c]:
line_dict[i]["cluster"] = c
line_dict.sort(key=itemgetter("cluster"))
cluster_lines = []
for key, group in itertools.groupby(line_dict, lambda item: item["cluster"]):
cluster_lines.append([(i["start"], i["end"]) for i in group])
merged_lines = []
for i in cluster_lines:
points = []
for x in i:
p0, p1 = x
points.extend((p0, p1))
# sort points and use min/max for endpoints of line
k = sorted(points)
merged_lines.append([k[0],k[-1]])
Edit:
Original Image (I am low rep on stackoverflow so I can only post 2 images, removed the original one with the hough lines on image. Hough line code:
from skimage.transform import probabilistic_hough_line
#Img is grayscale image
thresh = threshold_otsu(img)
binary = img > thresh
binary = np.invert(binary)
skel = skeletonize(binary) # skeletonize image
lines = probabilistic_hough_line(skel,
threshold=5,
line_length=10,
line_gap=5)