Improved line detection
This commit is contained in:
@@ -108,59 +108,56 @@ def group_empty_boxes(seams):
|
||||
|
||||
|
||||
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)
|
||||
group_id = 1
|
||||
|
||||
entries = []
|
||||
pixelgroup = None
|
||||
in_top_mode = True
|
||||
indices = np.indices(labeled.shape).T[:, :, [1, 0]]
|
||||
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):
|
||||
pixel = indices[labeled == label]
|
||||
minp = np.min(pixel, axis=0)
|
||||
maxp = np.max(pixel, axis=0)
|
||||
right_pixel = pixel[pixel[:, 0] == maxp[0]][:, 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):
|
||||
# up
|
||||
color[0] = 1
|
||||
if not in_top_mode:
|
||||
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:
|
||||
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
|
||||
submit_entry()
|
||||
in_top_mode = True
|
||||
if second_pixel.size > 0 and max(right_pixel) < max(second_pixel):
|
||||
# down
|
||||
color[1] = 1
|
||||
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
|
||||
if pixelgroup is None:
|
||||
pixelgroup = pixel[:, :]
|
||||
else:
|
||||
pixelgroup = np.concatenate((pixelgroup, pixel))
|
||||
colors.append(color)
|
||||
#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))
|
||||
|
||||
submit_entry()
|
||||
return entries
|
||||
Reference in New Issue
Block a user