Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

answer28-31运行成功案例(e.g. answer28) #50

Open
AQY-0o0 opened this issue Oct 21, 2021 · 1 comment
Open

answer28-31运行成功案例(e.g. answer28) #50

AQY-0o0 opened this issue Oct 21, 2021 · 1 comment

Comments

@AQY-0o0
Copy link

AQY-0o0 commented Oct 21, 2021

import cv2
import numpy as np
import matplotlib.pyplot as plt


# Affine
def affine(img, a, b, c, d, tx, ty):
    H, W, C = img.shape

    # temporary image
    _img = np.zeros((H + 2, W + 2, C), dtype=np.float32)
    _img[1:H + 1, 1:W + 1] = img

    # get new image shape
    H_new = np.round(H * d).astype(int)
    W_new = np.round(W * a).astype(int)
    out = np.zeros((H_new + 1, W_new + 1, C), dtype=np.float32)

    # get position of new image
    x_new = np.tile(np.arange(W_new), (H_new, 1))
    y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1)

    # get position of original image by affine
    adbc = a * d - b * c
    x = np.round((d * x_new - b * y_new) / adbc).astype(int) - tx + 1
    y = np.round((-c * x_new + a * y_new) / adbc).astype(int) - ty + 1

    x = np.minimum(np.maximum(x, 0), W + 1).astype(int)
    y = np.minimum(np.maximum(y, 0), H + 1).astype(int)

    # assgin pixcel to new image
    out[y_new, x_new] = _img[y, x]

    out = out[:H_new, :W_new]
    out = out.astype(np.uint8)

    return out


# Read image
img = cv2.imread("../imori.jpg").astype(np.float32)

# Affine
out = affine(img, a=1, b=0, c=0, d=1, tx=30, ty=-30)

# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("out.jpg", out)
@AQY-0o0
Copy link
Author

AQY-0o0 commented Oct 21, 2021

源码answer31其实已经调试好了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant