图像的仿射变换

2020-05-29  本文已影响0人  原上的小木屋

图像的平移

def bl_interpolate(img, dx=1, dy=1):
    H, W, C = img.shape  # 首先取得原图像的尺寸大小
    out = np.zeros((H, W, C), dtype=np.float)  # 重建图像
    for y in range(H):
        for x in range(W):
            for c in range(C):
                y1 = y - dy#取得原图像坐标
                x1 = x - dx#取得原图像坐标
                if ((y1 > H-1 or y1 < 0 or x1 > W-1 or x1 < 0)):
                    pass
                else:
                    out[y, x, c] = img[y1, x1, c]  # 根据上述推导的公式进行变换
    return out.astype(np.uint8)

平移伸缩

def bl_interpolate(img, tx=1, ty=1, dx=1, dy=1):
    H, W, C = img.shape  # 首先取得原图像的尺寸大小
    aH = int(ty * H)  # 计算插值后的图像尺寸
    aW = int(tx * W)  # 计算插值后的图像尺寸
    out = np.zeros((aH, aW, C), dtype=np.float)  # 重建图像
    for y in range(aH):
        for x in range(aW):
            for c in range(C):
                y1 = int((y - dy) // ty)
                x1 = int((x - dx) // tx)
                if ((y1 > H - 1 or y1 < 0 or x1 > W - 1 or x1 < 0)):
                    pass
                else:
                    print(y, x, c)
                    print(y1, x1, c)
                    out[y, x, c] = img[y1, x1, c]  # 根据上述推导的公式进行变换
    return out.astype(np.uint8)

旋转操作

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 shape of new image
    H_new = np.round(H).astype(np.int)
    W_new = np.round(W).astype(np.int)
    out = np.zeros((H_new, W_new, 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(np.int) - tx + 1
    y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1
    # adjust center by affine
    dcx = (x.max() + x.min()) // 2 - W // 2
    dcy = (y.max() + y.min()) // 2 - H // 2
    x -= dcx
    y -= dcy
    x = np.clip(x, 0, W + 1)
    y = np.clip(y, 0, H + 1)
    # assign pixcel
    out[y_new, x_new] = img[y, x]
    out = out.astype(np.uint8)
    return out
# Read image
img = cv2.imread(r"C:\Users\root\Desktop\123.jpg")
# Affine
A = 30.
theta = - np.pi * A / 180.
out = affine(img, a=np.cos(theta), b=-np.sin(theta), c=np.sin(theta), d=np.cos(theta),
             tx=0, ty=0)

图像仿射变换使图像倾斜

def affine(img, dx=30, dy=30):
    # get shape
    H, W, C = img.shape
    # Affine hyper parameters
    a = 1.
    b = dx / H
    c = dy / W
    d = 1.
    tx = 0.
    ty = 0.
    # prepare temporary
    _img = np.zeros((H+2, W+2, C), dtype=np.float32)
    # insert image to center of temporary
    _img[1:H+1, 1:W+1] = img
    # prepare affine image temporary
    H_new = np.ceil(dy + H).astype(np.int)
    W_new = np.ceil(dx + W).astype(np.int)
    out = np.zeros((H_new, W_new, C), dtype=np.float32)
    # preprare assigned index
    x_new = np.tile(np.arange(W_new), (H_new, 1))
    y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1)
    # prepare inverse matrix for affine
    adbc = a * d - b * c
    x = np.round((d * x_new  - b * y_new) / adbc).astype(np.int) - tx + 1
    y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1
    x = np.minimum(np.maximum(x, 0), W+1).astype(np.int)
    y = np.minimum(np.maximum(y, 0), H+1).astype(np.int)
    # assign value from original to affine image
    out[y_new, x_new] = _img[y, x]
    out = out.astype(np.uint8)
    return out
# Read image
img = cv2.imread(r"C:\Users\root\Desktop\123.jpg")
# Affine
out = affine(img, dx=0, dy=30)
上一篇下一篇

猜你喜欢

热点阅读