Skip to content

Instantly share code, notes, and snippets.

@philipperemy
Created May 9, 2020 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipperemy/824f7b2b335724a61f177a03ebc3f2ac to your computer and use it in GitHub Desktop.
Save philipperemy/824f7b2b335724a61f177a03ebc3f2ac to your computer and use it in GitHub Desktop.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
matplotlib.use('MACOSX')
img_h, img_w = 250, 500
img = np.zeros((img_h, img_w))
bounding_box_sizes = 25, 50
thumbnail_top_left_x, thumbnail_top_left_y = 100, 320
img[thumbnail_top_left_x:thumbnail_top_left_x + bounding_box_sizes[0],
thumbnail_top_left_y:thumbnail_top_left_y + bounding_box_sizes[1]] = 1
small_mask = np.array(img)
plt.ion()
img_sample_sizes = 20, 40
# plt.show()
for s in range(1000):
range_x = max(bounding_box_sizes[0] - img_sample_sizes[0], 0) # 10 -> 24.
range_y = max(bounding_box_sizes[1] - img_sample_sizes[1], 0) # 10 -> 24.
x0, x1 = thumbnail_top_left_x + range_x, thumbnail_top_left_x + bounding_box_sizes[0] - range_x
y0, y1 = thumbnail_top_left_y + range_y, thumbnail_top_left_y + bounding_box_sizes[1] - range_y
assert x0 < x1
assert y0 < y1
plt.imshow(img, cmap='jet')
plt.imsave(f'plt_{str(s).zfill(4)}.png', img)
plt.pause(0.01)
x = np.random.choice(range(x0, x1))
y = np.random.choice(range(y0, y1))
bigger_mask = np.zeros_like(img)
bigger_mask[x - img_sample_sizes[0]:x + img_sample_sizes[0], y - img_sample_sizes[1]: y + img_sample_sizes[1]] = 1
# masks should match.
assert np.all(np.logical_and(bigger_mask, small_mask).astype(np.int) == small_mask.astype(np.int))
img[x - img_sample_sizes[0]:x + img_sample_sizes[0], y - img_sample_sizes[1]: y + img_sample_sizes[1]] += 1
# plt.show()
# big_size += 5 # just for fun.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment