Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17477

rotation transform matrix in inverse warping

$
0
0

I'm writing an inverse image-warping function on my own, which takes an image, a transform matrix, and an output size as inputs.

Here's how I implement the function:

def inverse_warp(image, trans_matrix, output_size):    output_image = np.zeros((output_size[0], output_size[1], len(image.shape)), dtype=np.uint8)    inv_trans_matrix = np.linalg.inv(trans_matrix)    # inverse warpping    for v in range(output_size[0]):        for u in range(output_size[1]):            # get coords in origin image from dest image            uv = np.array([[u], [v], [1]])  # homogeneous coords            xy = inv_trans_matrix @ uv            # convert back to cartesian coords            x, y = xy[0, 0] / xy [2, 0], xy[1, 0] / xy [2, 0]            # check bounding problem            if 0 <= y <= image.shape[0] and 0 <= x <= image.shape[1]:                # bilinear interpolation                x_1, x_2 = int(np.floor(x)), int(np.ceil(x))                y_1, y_2 = int(np.floor(y)), int(np.ceil(y))                x, y = int(round(x)), int(round(y))                x_m = np.array([x_2 - x, x - x_1])                y_m = np.array([[y_2 - y], [y - y_1]])                # get color from origin image                col_11 = image[y_1, x_1]                col_12 = image[y_2, x_1]                col_21 = image[y_1, x_2]                col_22 = image[y_2, x_2]                if x_1 == x_2 or y_1 == y_2:                    col_xy = image[int(y), int(x)]                else:                    col_m = np.array([[col_11, col_12], [col_21, col_22]])                    col_xy = 1 / ((x_2 - x_1) * (y_2 - y_1)) * np.matmul(np.matmul(y_m, col_m), x_m)                output_image[v, u] = col_xy    return output_image

I applied a 90 degrees clockwise rotation with the following code:

i1 = plt.imread("image1.jpg")height, width, _ = i1.shapetrans_90_clockwise_matrix = np.array([    [0, 1, 0],     [-1, 0, height],    [0, 0, 1]])output_size_90 = (width, height)i1_90_clockwise = inverse_warp(i1, trans_90_clockwise_matrix, output_size_90)plt.imshow(i1_90_clockwise)

the result is not quite right, there's part of the image missing after rotation, like this:enter image description here

It seems the rotated picture is not shown from the origin. The y-axis is downwards which could be the problem, but how can I fix it?

Thanks in advance.


Viewing all articles
Browse latest Browse all 17477

Latest Images

Trending Articles



Latest Images