-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mask.py
113 lines (80 loc) · 2.66 KB
/
Mask.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Functions to create masks for images (2D arrays)
"""
# Import numpy
import numpy as np
def angularmask(image, start, stop, xcenter, ycenter):
"""
Purpose: Mask out an angular region from start to stop using
(xcenter, ycenter) as origin. All pixels outside angular region
are masked out (FALSE).
Parameters
----------
image: 2D array representing the image
start: starting angle in radians
stop: end angle in radians
xcenter: x co-ordinate for origin
ycenter: y co-ordinate for origin
Masks (blocks) a set of pixels (array elements) within angular
range from start to stop, evaluating the angle from
(xcenter,ycenter).
Angles defined as:
|
pi/2 --> pi | 0--> pi/2
|
--------+----------
-pi/2 --> -pi | 0 --> -pi/2
|
|
Angles increase in counter (anti) clockwise direction
Returns
-------
result: boolean array, same dimensions as image
TRUE - Unmasked pixel
FALSE - Masked pixel
"""
# Create 2D array of positions w.r.t to origin
xSize, ySize = image.shape
y,x = np.ogrid[-ycenter:ySize-ycenter,-xcenter:xSize-xcenter]
angle_array = np.arctan2(y,x)
mask = None
if stop > start :
mask = (angle_array >= start) & (angle_array <= stop)
else:
mask = ~((angle_array >= stop) & (angle_array <= start))
return mask
def circularmask(image, radius, xcenter, ycenter):
"""
Purpose: Mask out a circular region centered at (xcenter,ycenter)
with radius of 'radius'. All pixels outside circular region are
masked out (FALSE).
Parameters
----------
image: 2D array representing the image
radius: radius of masked out region
xcenter: x co-ordinate for origin
ycenter: y co-ordinate for origin
Returns
-------
result: boolean array, same dimensions as image
TRUE - Unmasked pixel
FALSE - Masked pixel
"""
xSize, ySize = image.shape
y,x = np.ogrid[-ycenter:ySize-ycenter,-xcenter:xSize-xcenter]
radii_array = np.hypot(x,y)
mask = (radii_array < radius)
return mask
# Test code for mask
if __name__ == "__main__" :
import matplotlib.pyplot as plt
testImage = np.ones([1024,1024])
mask = angularmask(testImage,
0.125*np.pi,0.25*np.pi,
512,512)
plt.imshow(testImage*mask,origin='lower')
plt.draw()
mask = circularmask(testImage, 256,
512,512)
plt.imshow(testImage*mask,origin='lower')
plt.draw()