<jupyter_start><jupyter_text>Func<jupyter_code>def _AffineMatrix(src, dst):
    src_ = np.vstack([src.T, np.array([1,1,1])])
    dst_ = np.vstack([dst.T, np.array([1,1,1])])
    M_ = np.dot(np.dot(dst_, src_.T), np.linalg.inv(np.dot(src_, src_.T)))
    return M_

def _Warp(img, M_, WH):
    W, H = WH[0], WH[1]
    src_y, src_x = np.indices(img.shape[:2])
    src_pts = np.stack((src_x.ravel(), src_y.ravel(), np.ones(src_y.size))).astype(int)
    dst_pts = np.round(np.dot(M_, src_pts)).astype(int)

    dst_img = np.zeros([W, H, 3])
    for c in range(3):
        for i in range(dst_pts.shape[-1]):
            dst_x, dst_y = dst_pts[0, i], dst_pts[1, i]
            src_u, src_v = src_pts[0, i], src_pts[1, i]
            if 0<=dst_x<H and 0<=dst_y<W:
                dst_img[dst_y, dst_x, c] = img[src_v, src_u, c]
    dst_img = dst_img.astype(int)  
    return dst_img<jupyter_output><empty_output><jupyter_text>img<jupyter_code>img_P = "./img/TomHanksApr09.jpg"
# img_P = "./img/tom-cruise-vanessa-kirby-mission-impossible-fallout-1564649325.bmp"
img_org = cv2.imread(img_P)    # bgr
img = cv2.cvtColor(img_org, cv2.COLOR_BGR2RGB)

fig = plt.figure()
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>src<jupyter_code>img_c = np.copy(img)
src = np.array([[276, 173], [317, 179], [285, 226]], np.float32)  # eye_L, eye_R, nose
cv2.polylines(img_c, [src.astype(int)], True, (255, 0, 0), 2)

fig = plt.figure()
plt.imshow(img_c)<jupyter_output><empty_output><jupyter_text>dst<jupyter_code>dst = np.array([[65, 90], [95, 90], [80, 120]],  np.float32)
M = _AffineMatrix(src, dst)
res = _Warp(img, M, (160, 190))

fig = plt.figure()
plt.imshow(res)<jupyter_output><empty_output><jupyter_text>Inv<jupyter_code>M_inv = _AffineMatrix(dst, src)
W_org, H_org, _ = img.shape
MM = M_inv[:-1,:]
inv = cv2.warpAffine(res.astype(np.uint8), MM, (H_org, W_org))

fig = plt.figure()
plt.imshow(inv)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19012987012987012, 'fraction_numerical': 0.04415584415584416, 'mean_word_length': 4.364902506963788, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '283778', 'license': 'MIT', 'n_tokens_mistral': 948, 'n_tokens_neox': 888, 'n_words': 139, 'path': 'ipynb/main.ipynb', 'repo_name': 'Abner0627/IPRV_Affine-Transform', 'size': '1925'}