Skip to content

Commit

Permalink
implemented binary masks
Browse files Browse the repository at this point in the history
  • Loading branch information
matko031 committed Jul 14, 2021
1 parent 72a9983 commit af4eaf2
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 81 deletions.
11 changes: 5 additions & 6 deletions stimuli/illusions/benary_cross.py
@@ -1,5 +1,5 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus

def benarys_cross(ppd=10, cross_size=(8,8,8,8), cross_thickness=5, padding=(1,1,1,1), target_size=2, back=1., cross=0., target=.5):
Expand Down Expand Up @@ -30,7 +30,7 @@ def benarys_cross(ppd=10, cross_size=(8,8,8,8), cross_thickness=5, padding=(1,1,
height = cross_top_px + cross_thickness_px + cross_bottom_px

img = np.ones((height, width)) * back
mask = np.ones((height, width)) * False
mask = np.zeros((height, width))

x_edge_left, x_edge_right = cross_left_px, -cross_right_px
y_edge_top, y_edge_bottom = cross_top_px, -cross_bottom_px
Expand All @@ -45,8 +45,8 @@ def benarys_cross(ppd=10, cross_size=(8,8,8,8), cross_thickness=5, padding=(1,1,
img[tpos1y:tpos1y + target_size_px, tpos1x:tpos1x + target_size_px] = target
img[tpos2y:tpos2y + target_size_px, tpos2x:] = target

mask[tpos1y:tpos1y + target_size_px, tpos1x:tpos1x + target_size_px] = True
mask[tpos2y:tpos2y + target_size_px, tpos2x:] = True
mask[tpos1y:tpos1y + target_size_px, tpos1x:tpos1x + target_size_px] = 1
mask[tpos2y:tpos2y + target_size_px, tpos2x:] = 2

img = pad_img(img, padding, ppd, back)
mask = pad_img(mask, padding, ppd, 0)
Expand All @@ -62,5 +62,4 @@ def domijan2015():
if __name__ == '__main__':
import matplotlib.pyplot as plt
stim = benarys_cross()
plt.imshow(stim.img, cmap='gray')
plt.show()
plot_stim(stim, mask=True)
15 changes: 10 additions & 5 deletions stimuli/illusions/checkerboard_contrast_contrast.py
@@ -1,5 +1,5 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus


Expand Down Expand Up @@ -37,7 +37,13 @@ def checkerboard_contrast_contrast_effect(ppd=10, n_checks=8, check_size=1.0, ta
tpos = (n_checks - target_length) // 2
idx[tpos:tpos + target_length, tpos:tpos + target_length] = True
arr1[idx] = alpha * arr1[idx] + (1 - alpha) * tau
mask_arr1[idx] = True
mask_counter = 1
for i, _ in enumerate(idx):
for j, el in enumerate(_):
if el:
mask_arr1[i,j] = mask_counter
mask_counter += 1


arr2 = arr1.copy()
arr2[~idx] = tau
Expand Down Expand Up @@ -70,6 +76,5 @@ def domijan2015():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = checkerboard_contrast_contrast_effect()
plt.imshow(img, cmap='gray')
plt.show()
stim = checkerboard_contrast_contrast_effect()
plot_stim(stim, mask=True)
15 changes: 7 additions & 8 deletions stimuli/illusions/checkerboard_sbc.py
@@ -1,5 +1,5 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus

def checkerboard_contrast(ppd=10, n_checks=8, check_size=1.0, target1_coords=(3, 2), target2_coords=(5, 5), extend_targets=False,
Expand Down Expand Up @@ -35,16 +35,16 @@ def checkerboard_contrast(ppd=10, n_checks=8, check_size=1.0, target1_coords=(3,
arr[target1_coords] = target
arr[target2_coords] = target

mask[target1_coords] = True
mask[target2_coords] = True
mask[target1_coords] = 1
mask[target2_coords] = 2

if extend_targets:
for idx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
arr[target1_coords[0] + idx[0], target1_coords[1] + idx[1]] = target
arr[target2_coords[0] + idx[0], target2_coords[1] + idx[1]] = target

mask[target1_coords[0] + idx[0], target1_coords[1] + idx[1]] = True
mask[target2_coords[0] + idx[0], target2_coords[1] + idx[1]] = True
mask[target1_coords[0] + idx[0], target1_coords[1] + idx[1]] = 1
mask[target2_coords[0] + idx[0], target2_coords[1] + idx[1]] = 2

img = np.repeat(np.repeat(arr, check_size_px, axis=0), check_size_px, axis=1)
mask = np.repeat(np.repeat(mask, check_size_px, axis=0), check_size_px, axis=1)
Expand All @@ -68,6 +68,5 @@ def domijan2015_extended():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = checkerboard_contrast()
plt.imshow(img, cmap='gray')
plt.show()
stim = checkerboard_contrast()
plot_stim(stim, mask=True)
39 changes: 25 additions & 14 deletions stimuli/illusions/cube.py
@@ -1,9 +1,9 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus


def cube_illusion(ppd=10, n_cells=4, target_length=1, cell_long=1.5, cell_short=1.0, corner_cell_width=1.8, corner_cell_height=1.8,
def cube_illusion(ppd=10, n_cells=5, target_length=2, cell_long=1.5, cell_short=1.0, corner_cell_width=1.8, corner_cell_height=1.8,
cell_spacing=.5, padding=(1.0,1.0,1.0,1.0), occlusion_overlap=(.7,.7,.7,.7), back=0., grid=1., target=.5, double=True):


Expand Down Expand Up @@ -50,47 +50,59 @@ def cube_illusion(ppd=10, n_cells=4, target_length=1, cell_long=1.5, cell_short=

img = np.ones((height_px, width_px)) * back
mask = np.zeros((height_px, width_px))
mask_counter = 0

for i, val in np.ndenumerate(arr):
mask_val = val == target
target_cell = val==target
if target_cell:
mask_counter += 1
if i[0] in range(1, n_cells-1) and i[1] in range(1, n_cells-1):
continue # skip centre cells for efficiency
elif i == (0,0): # top left corner cell
img[:corner_cell_height_px, :corner_cell_width_px] = val
mask[:corner_cell_height_px, :corner_cell_width_px] = mask_val
if target_cell:
mask[:corner_cell_height_px, :corner_cell_width_px] = mask_counter

elif i == (0, n_cells-1): # top right corner cell
img[:corner_cell_height_px, -corner_cell_width_px:] = val
mask[:corner_cell_height_px, -corner_cell_width_px:] = mask_val
if target_cell:
mask[:corner_cell_height_px, -corner_cell_width_px:] = mask_counter

elif i == (n_cells-1, 0): # bottom left corner cell
img[-corner_cell_height_px:, :corner_cell_width_px] = val
mask[-corner_cell_height_px:, :corner_cell_width_px] = mask_val
if target_cell:
mask[-corner_cell_height_px:, :corner_cell_width_px] = mask_counter

elif i == (n_cells - 1, n_cells-1): # bottom right corner cell
img[-corner_cell_height_px:, -corner_cell_width_px:] = val
mask[-corner_cell_height_px:, -corner_cell_width_px:] = mask_val
if target_cell:
mask[-corner_cell_height_px:, -corner_cell_width_px:] = mask_counter

else:
if i[0] == 0 or i[0] == n_cells -1: # top/bottom side
x = corner_cell_width_px + cell_spacing_px + (i[1] - 1) * (cell_long_px + cell_spacing_px)
if i[0] == 0: # top side
img[:cell_short_px, x:x + cell_long_px] = val
mask[:cell_short_px, x:x + cell_long_px] = mask_val
if target_cell:
mask[:cell_short_px, x:x + cell_long_px] = mask_counter
else: # bottom side
img[-cell_short_px:, x:x + cell_long_px] = val
mask[-cell_short_px:, x:x + cell_long_px] = mask_val
if target_cell:
mask[-cell_short_px:, x:x + cell_long_px] = mask_counter

else: # left/right side
y = corner_cell_width_px + cell_spacing_px + (i[0] - 1) * (cell_long_px + cell_spacing_px)

if i[1] == 0: # left side
img[y:y + cell_long_px, :cell_short_px] = val
mask[y:y + cell_long_px, :cell_short_px] = mask_val
if target_cell:
mask[y:y + cell_long_px, :cell_short_px] = mask_counter

else: # right side
img[y:y + cell_long_px, -cell_short_px:] = val
mask[y:y + cell_long_px, -cell_short_px:] = mask_val
if target_cell:
mask[y:y + cell_long_px, -cell_short_px:] = mask_counter



# add occlusion
Expand Down Expand Up @@ -128,6 +140,5 @@ def domijan2015():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = cube_illusion()
plt.imshow(img, cmap='gray')
plt.show()
stim = cube_illusion()
plot_stim(stim, mask=True)
15 changes: 9 additions & 6 deletions stimuli/illusions/dungeon.py
@@ -1,5 +1,5 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus


Expand Down Expand Up @@ -36,14 +36,18 @@ def dungeon_illusion(ppd=10, n_cells=5, target_radius=1, cell_size=1.0, padding=
# add targets
idx = radii <= (target_radius * 2)
arr[idx] = target
mask_arr[idx] = True
mask_arr[idx] = 1

# compute and apply grid mask
grid_mask = [[(False if i % 2 == 0 and j % 2 == 0 else True) for i in range(n_cells * 2 - 1)] for j in
range(n_cells * 2 - 1)]
grid_mask = np.array(grid_mask)
arr[grid_mask] = back
mask_arr[grid_mask] = False
mask_arr[grid_mask] = 0

ind1, ind2 = np.nonzero(mask_arr)
for i in range(len(ind1)):
mask_arr[ind1[i], ind2[i]] = i+1

img = np.repeat(np.repeat(arr, cell_size_px, axis=0), cell_size_px, axis=1)
img = pad_img(img, padding, ppd, back)
Expand All @@ -68,6 +72,5 @@ def domijan2015():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = dungeon_illusion()
plt.imshow(img, cmap='gray')
plt.show()
stim = dungeon_illusion()
plot_stim(stim, mask=True)
17 changes: 9 additions & 8 deletions stimuli/illusions/grating.py
@@ -1,9 +1,9 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus


def grating_illusion(ppd=10, n_bars=5, target_length=1, bar_width=1.0, bar_height=8.0, padding=(1.0,1.0,1.0,1.0), back=0., grid=1., target=0.5, double=True):
def grating_illusion(ppd=10, n_bars=5, target_length=2, bar_width=1.0, bar_height=8.0, padding=(1.0,1.0,1.0,1.0), back=0., grid=1., target=0.5, double=True):
"""
Grating illusion
Expand Down Expand Up @@ -32,7 +32,8 @@ def grating_illusion(ppd=10, n_bars=5, target_length=1, bar_width=1.0, bar_heigh

target_offset = (n_bars-target_length)//2
arr[target_offset:target_offset+target_length] = target
mask_arr[target_offset:target_offset+target_length] = True
for i, j in enumerate(range(target_offset,target_offset+target_length)):
mask_arr[j] = i+1

# final image array
width_px = (n_bars*2-1)*bar_width_px
Expand All @@ -41,10 +42,11 @@ def grating_illusion(ppd=10, n_bars=5, target_length=1, bar_width=1.0, bar_heigh
mask = np.zeros((height_px, width_px))

for i, val in np.ndenumerate(arr):
mask_val = val==target
target_val = val==target
x = i[0]*2*bar_width_px
img[:, x:x+bar_width_px] = val
mask[:, x:x+bar_width_px] = mask_val
if target_val:
mask[:, x:x+bar_width_px] = mask_arr[i]

img = pad_img(img, padding, ppd, back)
mask = pad_img(mask, padding, ppd, 0)
Expand All @@ -68,6 +70,5 @@ def domijan2015():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = grating_illusion()
plt.imshow(img, cmap='gray')
plt.show()
stim = grating_illusion()
plot_stim(stim, mask=True)
9 changes: 4 additions & 5 deletions stimuli/illusions/grating_induction.py
@@ -1,6 +1,6 @@
import numpy as np
from scipy.ndimage.filters import gaussian_filter
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus

import stimuli
Expand All @@ -22,7 +22,7 @@ def grating_illusion(shape=(10,10), ppd=40, frequency=0.5, target_height=0.5, bl
target_start = height_px//2 - target_height_px//2
target_end = target_start + target_height_px
img[target_start:target_end, :] = target
mask[target_start:target_end, :] = True
mask[target_start:target_end, :] = 1

img = pad_img(img, padding, ppd, target)
mask = pad_img(mask, padding, ppd, 0)
Expand All @@ -47,6 +47,5 @@ def RHS2007_grating_induction():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = grating_illusion()
plt.imshow(img, cmap='gray')
plt.show()
stim = grating_illusion()
plot_stim(stim, mask=True)
9 changes: 4 additions & 5 deletions stimuli/illusions/rings.py
@@ -1,5 +1,5 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus

def ring_pattern(ppd=10, n_rings=8, target_pos_l=4, target_pos_r=3, ring_width=.5, padding=(1.0,1.0,1.0,1.0,), back=0., rings=1., target=.5, invert_rings=False, double=True, ):
Expand Down Expand Up @@ -37,7 +37,7 @@ def ring_pattern(ppd=10, n_rings=8, target_pos_l=4, target_pos_r=3, ring_width=.
arr[radii == target_pos_l] = target

mask_arr = np.zeros((n_rings*2, n_rings*2))
mask_arr[radii == target_pos_l] = True
mask_arr[radii == target_pos_l] = 1


# build image from array
Expand Down Expand Up @@ -82,6 +82,5 @@ def domijan2015():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = ring_pattern()
plt.imshow(img, cmap='gray')
plt.show()
stim = ring_pattern()
plot_stim(stim, mask=True)
9 changes: 3 additions & 6 deletions stimuli/illusions/sbc.py
@@ -1,6 +1,6 @@
import numpy as np
import stimuli
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus


Expand Down Expand Up @@ -81,11 +81,8 @@ def RHS2007_sbc_small():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = simultaneous_brightness_contrast()
plt.imshow(img, cmap='gray')
plt.show()


stim = simultaneous_brightness_contrast()
plot_stim(stim, mask=True)


###################################
Expand Down
13 changes: 4 additions & 9 deletions stimuli/illusions/todorovic.py
Expand Up @@ -2,7 +2,7 @@
import numpy as np
import math

from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus


Expand Down Expand Up @@ -79,7 +79,7 @@ def todorovic_illusion(target_shape=(4,4), ppd=10, covers_shape=(2.5, 2.5), spac
mask = np.hstack([mask, stim2.target_mask])

img = pad_img(img, padding, ppd, target)
mask = pad_img(mask, padding, ppd, target)
mask = pad_img(mask, padding, ppd, 0)

stim = Stimulus()
stim.img = img
Expand Down Expand Up @@ -144,13 +144,8 @@ def RHS2007_todorovic_in_small():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = todorovic_illusion()
plt.imshow(img, cmap='gray')
plt.show()




stim = todorovic_illusion()
plot_stim(stim, mask=True)


# This function comes from the lightness module that has been integrated inside the illusions dir.
Expand Down

0 comments on commit af4eaf2

Please sign in to comment.