Repository navigation
Expand file tree
/
Copy pathmakeMask.py
More file actions
97 lines (68 loc) · 2.71 KB
/
Copy pathmakeMask.py
File metadata and controls
97 lines (68 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 11 11:56:06 2020
This script produces an image containing circles that are
closely packed, but do not overlap.
@author: Brian
"""
#%% Setup Environment
# Reset (clears all variables n stuff)
from IPython import get_ipython
get_ipython().magic('reset -sf')
import numpy as np
import math as m
from random import seed, randint
from skimage import draw, measure
from skimage.io import imshow
#%% INPUTS...
'''Image Options'''
s = 500 # width and height of frame
'''Stop Condition'''
passLimit = 500 # the stop condition
'''Size/Arrangement Options'''
rad_biggest = s/10
rad_smallest = s/100
seed(1) # change the seed (i.e. 2,3,4,5...) if you want a different random arrangement
#%% Solve and Make the Mask
# Make blank mask
mask = np.zeros([s,s], dtype=bool)
# Draw the first circle...
nCircles = 1
r,c = randint(0,s), randint(0,s)
rr,cc = draw.circle(r,c, rad_biggest, shape=(s,s)) # draw a circle with largest radius possible.
mask[rr, cc] = 1
nCircles = 1
testMask = np.zeros([500,500],dtype=bool)
nPasses=0
nAccepts=0
# Draw the other circles...
while(nPasses < passLimit):
testMask = mask.copy()
# Only pick a new centerpoint from available locations (where pixels=0)
avail = np.where(mask == 0)
nAvail = np.size(avail[0])
randy = randint(0, nAvail)
r,c = avail[0][randy], avail[1][randy]
radius = rad_smallest
rr,cc = draw.circle(r,c, radius, shape=(s,s)) # draw a circle with largest radius possible.
testMask[rr, cc] = 1
label_image = measure.label(testMask) # calculate number of image regions.
nRegions = np.max(label_image) # the maximum value in the label image.
if nRegions < nCircles+1: # if the first guess didn't work out
nPasses += 1
else: # if the guess did work out
# nDilateAttempts = 0
nAccepts += 1
nCircles+=1
while(nRegions == nCircles and radius < rad_biggest):
mask = mask | testMask # take the previous one (which didn't have overlap)
# images.append(mask)
testMask = mask.copy() # new one becomes the old one
radius = radius * 1.15 # Increase in 15% intervals.
rr,cc = draw.circle(r,c, radius, shape=(s,s)) # draw a circle with largest radius possible.
testMask[rr, cc] = 1
label_image = measure.label(testMask) # calculate number of image regions.
nRegions = np.max(label_image) # the maximum value in the label image.
print('progress (sort of) = ', m.floor(nPasses/passLimit*100), '%')
imshow(mask)
print('DONE.')