Image Backward(inverse)-Warping In Python

192 views Asked by At

I am studying image processing and I am trying to implement a code for backward-warping in Python. However, I am facing an issue.

In forward-warping, when pixel positions change, empty spaces are filled, but there is a problem called the blackhole phenomenon where black pixels appear in the output. I understand that backward-warping, which involves using the inverse matrix, is used to address this issue. So, I tried to implement it myself, but strangely, the blackhole phenomenon still persists. What I thought is that multiplying by the inverse matrix also eventually leads to empty pixels. What specifically could be the problem, and how can I solve it?

I will not use openCV library.

I would like to know what the problem is and how to approach the equation. I also need modifications to the code.

input_image = cv2.imread('cat.jpg')

def rotaiton(image, angle):
    
    result_image = np.zeros_like(image)
    height, width = image.shape[:2]
    
    centering_matrix = np.array([
        [ 1, 0, -width/2],
        [ 0, 1, -height/2],
        [0, 0, 1]
    ])
    
    # 역행렬을 사용하기 때문에 .. 반대로
    rotation_matrix = np.array([
        [ math.cos(math.radians(angle)), -math.sin(math.radians(angle)), 0],
        [ math.sin(math.radians(angle)), math.cos(math.radians(angle)), 0],
        [0, 0, 1]
    ])
    
    recentering_matrix = np.array([
        [1, 0, width/2],
        [0, 1, height/2],
        [0, 0, 1]
    ])
    
    for y in range(height):
        for x in range(width):
            original_coords = np.array([x, y, 1])
            
            # 연산
            matrix = np.dot(recentering_matrix, np.dot(np.linalg.inv(rotation_matrix), centering_matrix))
            result = np.dot(matrix, original_coords)

            result_matrix_x, result_matrix_y = int(result[0]), int(result[1])

            #범위를 벗어날 경우 제외. 이미지 범위 내에 있는 경우에만
            if 0 <= result_matrix_x < width and 0 <= result_matrix_y < height:
                result_image[result_matrix_y, result_matrix_x] = image[y, x]
    
    return result_image

# 회전시킬 이미지와 회전 각도를 설정하기
result_img = rotaiton(input_image, 45)

cv2.imshow('result_img', result_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
0

There are 0 answers