Beispiel #1
0
import numpy as np
from skimage.feature import canny
from vision.segmentation.segment import crop_by_saliency, saliency_dragonfly
from vision.tests import get_test_image
from vision.measurements import subspace_shape

src = np.zeros((11, 2))
src[:, 0] = np.arange(-5, 6)
src[:, 1] = np.power(src[:, 0], 2)

dst = np.zeros((11, 2))
dst[:, 0] = np.arange(-5, 6) + 3
dst[:, 1] = np.power(src[:, 0], 2) / 12

mu, phi, sigma2 = subspace_shape.learn((src, dst))

# plt.plot(src[:, 0], src[:, 1], 'r')
# plt.plot(dst[:, 0], dst[:, 1], 'g')

# avg = mu + phi @ (-0.75 * np.ones((1, 1)))
# avg = avg.reshape(-1, 2)
# plt.plot(avg[:, 0], avg[:, 1], 'b')

# plt.show()

image = np.zeros((100, 100), dtype=np.bool)
j = np.arange(20, 80)
i = np.power(j - 50, 2) / 10
image[90 - i.astype(np.int), j] = True

subspace_shape.infer(image, mu, phi, sigma2)
def visualize_result(image, edge_image, shape, closest_points):
    output_image = 0.5 * (image + edge_image[:, :, np.newaxis])

    points = closest_points[:, [1, 0]]
    perimeter = draw.polygon_perimeter(points[:, 0], points[:, 1])
    draw.set_color(output_image, (perimeter[0].astype(np.int), perimeter[1].astype(np.int)), [0, 1, 0])

    points = shape[:, [1, 0]]
    perimeter = draw.polygon_perimeter(points[:, 0], points[:, 1])
    draw.set_color(output_image, (perimeter[0].astype(np.int), perimeter[1].astype(np.int)), [0, 0, 1])
    return output_image

shapes = [smoothed_shape(read_shape(i)) for i in range(4)]
aligned_shapes = procrustes.generalized_procrustes(shapes)
shape_model = subspace_shape.learn(aligned_shapes, K=8)

# wings_image = get_test_image('wing_area', 'cropped', 'unlabelled', '7.png')
wings_image = get_test_image('wing_area', 'pinned', '1.png')
edges = canny(wings_image[:, :, 1], 3)

saliency = saliency_dragonfly(wings_image)
thresh = threshold(saliency)

edges = skeletonize(edges)
gaps = scipy.ndimage.filters.convolve(1 * edges, np.ones((3, 3)), mode='constant', cval=False)
edges[(gaps == 2) & ~edges] = True
edges = skeletonize(edges)

distance = scipy.ndimage.distance_transform_edt(~edges)
def visualize_result(image, edge_image, shape, closest_points):
    output_image = 0.5 * (image + edge_image[:, :, np.newaxis])

    points = closest_points[:, [1, 0]]
    perimeter = draw.polygon_perimeter(points[:, 0], points[:, 1])
    draw.set_color(output_image, (perimeter[0].astype(np.int), perimeter[1].astype(np.int)), [0, 1, 0])

    points = shape[:, [1, 0]]
    perimeter = draw.polygon_perimeter(points[:, 0], points[:, 1])
    draw.set_color(output_image, (perimeter[0].astype(np.int), perimeter[1].astype(np.int)), [0, 0, 1])
    return output_image

shapes = [smoothed_shape(read_shape(i)) for i in range(4)]
aligned_shapes = procrustes.generalized_procrustes(shapes)
shape_model = subspace_shape.learn(aligned_shapes, K=8)

wings_image = get_test_image('wing_area', 'cropped', 'unlabelled', '7.png')
# write_image('wings.png', wings_image)
edges = canny(wings_image[:, :, 1], 3)

saliency = saliency_dragonfly(wings_image)
thresh = threshold(saliency)

background = threshold(scipy.ndimage.distance_transform_edt(~thresh))

contours = find_contours(thresh, level=0.5)
outline = max(contours, key=attrgetter('size')).astype(np.int)
outline_image = np.zeros_like(edges)
draw.set_color(outline_image, (outline[:, 0], outline[:, 1]), True)