In [2]:
import numpy as np
import PIL
import cv2
import matplotlib.pyplot as plt
from dmdlib.ALP import *
%matplotlib inline
In [3]:
dmd = np.array(disp_affine_pattern(), dtype='float32')
In [ ]:
In [4]:
# displays right to left on the camera image.
# dmd = np.array(([450, 300], [550, 275], [500, 430]), dtype='float32')
pic = np.array([[1321, 791],[1092, 433], [802, 1040]], dtype='float32')
# pic = np.fliplr(pic)
# pic = np.flipud(pic)
In [5]:
print(pic)
In [6]:
pic_to_dmd = cv2.getAffineTransform(pic, dmd)
In [7]:
from PIL import Image, ImageFont
import numpy as np
import numba
font = ImageFont.load_default()
def make_text_fast(text, array, margins=(10, 10, 150, 150),):
height, width = array.shape
mask = font.getmask(text)
mask_array = np.asarray(mask, dtype=bool)
mask_array.shape = mask.size[1], mask.size[0]
top, bottom, left, right = margins
_array_maker(array, mask_array, top, bottom, left, right, width, height)
return array
# @numba.jit(nopython=True, parallel=True)
def _array_maker(arrayout, arrayin, top, bottom, left, right, width, height):
W_margins = width - right - left
H_margins = height - top - bottom
mH, mW = arrayin.shape
W_scale = np.ceil(W_margins / mW)
H_scale = np.ceil(H_margins / mH)
for x in range(W_margins):
x_mask = int(x // W_scale)
for y in range(H_margins):
y_mask = int(y // H_scale)
maskv = arrayin[y_mask, x_mask]
arrayout[y+top, x+left] = maskv
return
In [8]:
cam_w = 2048
cam_h = 1536
cam_arr = np.zeros((cam_h, cam_w), dtype='float32')
txtbitmap = make_text_fast('hello', cam_arr, margins=(500,500,500,500))
In [86]:
plt.imshow(txtbitmap)
Out[86]:
In [9]:
# dmd_arr = np.zeros_like(cam_arr)
dmd_arr = cv2.warpAffine(cam_arr, pic_to_dmd, (1024,768))
dmd_arr *= 255
In [88]:
plt.imshow(dmd_arr)
Out[88]:
In [89]:
disp_image_pattern(dmd_arr.astype('uint8'))
In [1]:
pic_to_dmd
In [10]:
import pickle
In [11]:
forsave = {'camera_points': pic, 'dmd_points': dmd, 'cam_to_dmd': pic_to_dmd}
In [14]:
with open('d:/patters/mouse_11101/transform.pickle', 'wb') as f:
pickle.dump(forsave, f)
In [36]:
with open('transform.pickle', 'rb') as f2:
a = pickle.load(f2)
In [39]:
pwd
Out[39]:
In [41]:
ls
In [ ]: