Added initial lens tests

This commit is contained in:
simleek
2019-10-03 23:51:26 -07:00
parent d3ea81dc50
commit dd6021d70a
2 changed files with 133 additions and 4 deletions

View File

@ -47,10 +47,6 @@ class VideoHandlerThread(threading.Thread):
for c in self.callbacks:
try:
frame_c = c(frame)
except TypeError as te:
raise TypeError(
"Callback functions for cvpubsub need to accept two arguments: array and uid"
)
except Exception as e:
self.exception_raised = e
frame = frame_c = self.exception_raised

View File

@ -95,3 +95,136 @@ class TestSubWin(ut.TestCase):
autoencoder.fit(grab_noise, grab, steps_per_epoch=1, epochs=1)
output_image = autoencoder.predict(grab, steps=1)
displayer.update((output_image[0] * 255.0).astype(np.uint8), "uid for autoencoder output")
def test_lens_construction(self):
from displayarray import display
import numpy as np
center = (75, 450)
zoom = .5
zoom_out = 1.0 / zoom
arr = np.random.uniform(0, 1, (300, 600, 3))
display(arr, blocking=True)
y = np.arange(arr.shape[0])
x = np.arange(arr.shape[1])
y_ = (y - center[0]) * zoom_out / arr.shape[0]
x_ = (x - center[1]) * zoom_out / arr.shape[1]
p = np.array(np.meshgrid(x_, y_))
display(p[0] + .5, blocking=True)
display(p[1] + .5, blocking=True)
barrel_power = 1.5
theta = np.arctan2(p[1], p[0])
display((theta + (np.pi / 2.0)) / (np.pi / 2.0), blocking=True)
radius = np.linalg.norm(p, axis=0)
print(radius.shape)
display(radius, blocking=True)
radius = pow(radius, barrel_power)
display(radius, blocking=True)
print(len(x))
x_new = 0.5 * (radius * np.cos(theta) + 1)
display(x_new, blocking=True)
x_new = np.clip(x_new * len(x), 0, len(x) - 1)
display(x_new / float(len(x)), blocking=True)
y_new = 0.5 * (radius * np.sin(theta) + 1)
display(y_new, blocking=True)
y_new = np.clip(y_new * len(y), 0, len(y) - 1)
p = np.array(np.meshgrid(y, x)).astype(np.uint32)
p_new = np.array((y_new, x_new)).astype(np.uint32)
arr[p[0], p[1], :] = np.swapaxes(arr[p_new[0], p_new[1], :], 0, 1)
display(arr, blocking=True)
def test_display_lens(self):
from displayarray import display
from displayarray.input import mouse_loop
import numpy as np
import cv2
import skimage.measure
import skimage.transform
def lens(arr):
center = lens.center
zoom = lens.zoom
zoom_out = 1.0 / zoom[0]
if not isinstance(lens.bleed[0], np.ndarray):
lens.bleed[0] = np.zeros_like(arr)
y = np.arange(arr.shape[0])
x = np.arange(arr.shape[1])
y_ = (y - arr.shape[0] / 2.0) * zoom_out / arr.shape[0]
x_ = (x - arr.shape[1] / 2.0) * zoom_out / arr.shape[1]
p = np.array(np.meshgrid(x_, y_))
y2_ = (y - center[0]) * zoom_out / arr.shape[0]
x2_ = (x - center[1]) * zoom_out / arr.shape[1]
p2 = np.array(np.meshgrid(x2_, y2_))
barrel_power = lens.power[0]
theta = np.arctan2(p2[1], p2[0])
radius = np.linalg.norm(p2, axis=0)
radius = pow(radius, barrel_power)
x_new = 0.5 * (radius * np.cos(theta) + 1)
x_new = np.clip(x_new * len(x), 0, len(x) - 1)
y_new = 0.5 * (radius * np.sin(theta) + 1)
y_new = np.clip(y_new * len(y), 0, len(y) - 1)
p = np.array(np.meshgrid(y, x)).astype(np.uint32)
p_new = np.array((y_new, x_new)).astype(np.uint32)
# arr[p[0], p[1], :] = np.swapaxes(arr[p_new[0], p_new[1], :], 0, 1)
arr2 = lens.bleed[0].copy()
arr2[y,...] = (arr2[y,...] + arr2[(y+1)%len(y), ...])/2
arr2[y, ...] = (arr2[y, ...] + arr2[(y - 1) % len(y), ...]) / 2
arr2[:,x, ...] = (arr2[:,x, ...] + arr2[:, (x + 1) % len(x), ...]) / 2
arr2[:,x, ...] = (arr2[:,x, ...] + arr2[:, (x - 1) % len(x), ...]) / 2
#arr2 = np.zeros_like(arr)
arr2[p_new[0], p_new[1], :] = np.swapaxes(arr[p[0], p[1], :], 0, 1)
#lens.bleed[0][p_new[0], p_new[1], :] = np.swapaxes(arr[p[0], p[1], :], 0, 1)
#avg = skimage.measure.block_reduce(arr2, (2,2, 1), np.min)
#avg2 = skimage.transform.resize(avg, (arr2.shape))
lens.bleed[0]=arr2
#lens.bleed
return arr2
lens.center = [250, 250]
lens.zoom = [.5]
lens.power = [1.0]
lens.bleed = [None]
@mouse_loop
def m_loop(me):
m_loop.center[:] = [me.y, me.x]
if me.event == cv2.EVENT_MOUSEWHEEL:
if me.flags & cv2.EVENT_FLAG_CTRLKEY:
if me.flags > 0:
m_loop.zoom[0] *= 1.1
else:
m_loop.zoom[0] /= 1.1
else:
if me.flags > 0:
m_loop.power[0] *= 1.1
else:
m_loop.power[0] /= 1.1
m_loop.center = lens.center
m_loop.zoom = lens.zoom
m_loop.power = lens.power
display("fractal test.mp4", callbacks=lens, blocking=True)