Improved line detection

This commit is contained in:
Caesar2011
2019-03-17 19:47:07 +01:00
parent 8626a2db01
commit 59e73827cf

View File

@@ -108,59 +108,56 @@ def group_empty_boxes(seams):
def generate_lines(labeled, ncomponents, gray): def generate_lines(labeled, ncomponents, gray):
plt.imshow(labeled, cmap=rand_cmap(ncomponents, type='hard', first_color_black=True, last_color_black=False, verbose=False))
plt.show()
plt.imsave("result/groups.png", labeled, cmap=rand_cmap(ncomponents, type='hard', first_color_black=True, last_color_black=False, verbose=False))
groups = np.copy(labeled) groups = np.copy(labeled)
group_id = 1 group_id = 1
entries = [] entries = []
pixelgroup = None pixelgroup = None
in_top_mode = True in_top_mode = True
indices = np.indices(labeled.shape).T[:, :, [1, 0]] indices = np.indices(labeled.shape).T[:, :, [1, 0]]
indices = np.swapaxes(indices, 0, 1) indices = np.swapaxes(indices, 0, 1)
colors = [[0, 0, 0]]
def submit_entry():
nonlocal pixelgroup, group_id, entries
minps = np.min(pixelgroup, axis=0)
maxps = np.max(pixelgroup, axis=0)
if pixel.shape[0] < 20 or maxps[1] - minps[1] < 5 or maxps[0] - minps[0] < 5:
return
entry = gray[minps[1]:maxps[1] + 1, minps[0]:maxps[0] + 1]
if pixelgroup.shape[0] / entry.shape[0] / entry.shape[1] < 0.10:
return
pixelgroup = np.subtract(pixelgroup, minps)
white = np.ones_like(entry) * np.max(entry)
for w in range(entry.shape[1]):
ys = pixelgroup[pixelgroup[:, 0] == w][:, 1]
if ys.size > 0:
ymin = np.min(ys)
ymax = np.max(ys)
white[ymin:ymax, w] = entry[ymin:ymax, w]
#white[pixelgroup[:, 1], pixelgroup[:, 0]] = entry[pixelgroup[:, 1], pixelgroup[:, 0]]
entries.append(white)
pixelgroup = None
group_id += 1
for label in range(1, ncomponents+1): for label in range(1, ncomponents+1):
pixel = indices[labeled == label] pixel = indices[labeled == label]
minp = np.min(pixel, axis=0) minp = np.min(pixel, axis=0)
maxp = np.max(pixel, axis=0) maxp = np.max(pixel, axis=0)
right_pixel = pixel[pixel[:, 0] == maxp[0]][:, 1] right_pixel = pixel[pixel[:, 0] == maxp[0]][:, 1]
second_pixel = pixel[pixel[:, 0] == maxp[0]-1][:, 1] second_pixel = pixel[pixel[:, 0] == maxp[0]-1][:, 1]
color = [0, 0, 1]
if second_pixel.size > 0 and min(right_pixel) > min(second_pixel): if second_pixel.size > 0 and min(right_pixel) > min(second_pixel):
# up # up
color[0] = 1
if not in_top_mode: if not in_top_mode:
minps = np.min(pixelgroup, axis=0) submit_entry()
maxps = np.max(pixelgroup, axis=0)
if pixel.shape[0] < 20 or maxps[1]-minps[1] < 5 or maxps[0]-minps[0] < 5:
continue
entry = gray[minps[1]:maxps[1]+1, minps[0]:maxps[0]+1]
pixelgroup = np.subtract(pixelgroup, minps)
white = np.ones_like(entry) * np.max(entry)
white[pixelgroup[:, 1], pixelgroup[:, 0]] = entry[pixelgroup[:, 1], pixelgroup[:, 0]]
entries.append(white)
pixelgroup = None
group_id += 1
in_top_mode = True in_top_mode = True
if second_pixel.size > 0 and max(right_pixel) < max(second_pixel): if second_pixel.size > 0 and max(right_pixel) < max(second_pixel):
# down # down
color[1] = 1
in_top_mode = False in_top_mode = False
if pixelgroup is not None and minp[1] - 5 > np.max(pixelgroup, axis=0)[1]:
submit_entry()
groups[labeled == label] = group_id groups[labeled == label] = group_id
if pixelgroup is None: if pixelgroup is None:
pixelgroup = pixel[:, :] pixelgroup = pixel[:, :]
else: else:
pixelgroup = np.concatenate((pixelgroup, pixel)) pixelgroup = np.concatenate((pixelgroup, pixel))
colors.append(color) submit_entry()
#plt.imsave("result/groups_types.png", labeled,
# cmap=list_cmap(np.array(colors)))
#g = np.array(load_image("result/groups_types.png")[:, :, :3], dtype="float")
#b = load_image("result/gray.png")
#t = (g[:, :, :3] + np.tile(b[:, :], (3, 1, 1)).swapaxes(2, 0).swapaxes(1, 0) * 1) / 2
#t = (g[:, :, :3] + np.tile(b[:, :], (3, 1, 1)).swapaxes(2, 0).swapaxes(1, 0) * 1) / 2
#save_image("result/combined_types.png", t/255)
#plt.imsave("result/combined_new.png", groups,
# cmap=rand_cmap(500, type='hard', first_color_black=True, last_color_black=False, verbose=False))
return entries return entries