Warps the pixels of an image into a new image using the horizontal and vertical flows. Pixels that are warped from an invalid location are set to 0. Parameters: image: Grayscale image horizontal_flow: Horizontal flow vertical_flow: Vertical flow Returns
(
image: np.ndarray, horizontal_flow: np.ndarray, vertical_flow: np.ndarray
)
| 16 | |
| 17 | |
| 18 | def warp( |
| 19 | image: np.ndarray, horizontal_flow: np.ndarray, vertical_flow: np.ndarray |
| 20 | ) -> np.ndarray: |
| 21 | """ |
| 22 | Warps the pixels of an image into a new image using the horizontal and vertical |
| 23 | flows. |
| 24 | Pixels that are warped from an invalid location are set to 0. |
| 25 | |
| 26 | Parameters: |
| 27 | image: Grayscale image |
| 28 | horizontal_flow: Horizontal flow |
| 29 | vertical_flow: Vertical flow |
| 30 | |
| 31 | Returns: Warped image |
| 32 | |
| 33 | >>> warp(np.array([[0, 1, 2], [0, 3, 0], [2, 2, 2]]), \ |
| 34 | np.array([[0, 1, -1], [-1, 0, 0], [1, 1, 1]]), \ |
| 35 | np.array([[0, 0, 0], [0, 1, 0], [0, 0, 1]])) |
| 36 | array([[0, 0, 0], |
| 37 | [3, 1, 0], |
| 38 | [0, 2, 3]]) |
| 39 | """ |
| 40 | flow = np.stack((horizontal_flow, vertical_flow), 2) |
| 41 | |
| 42 | # Create a grid of all pixel coordinates and subtract the flow to get the |
| 43 | # target pixels coordinates |
| 44 | grid = np.stack( |
| 45 | np.meshgrid(np.arange(0, image.shape[1]), np.arange(0, image.shape[0])), 2 |
| 46 | ) |
| 47 | grid = np.round(grid - flow).astype(np.int32) |
| 48 | |
| 49 | # Find the locations outside of the original image |
| 50 | invalid = (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]])) |
| 51 | grid[invalid] = 0 |
| 52 | |
| 53 | warped = image[grid[:, :, 1], grid[:, :, 0]] |
| 54 | |
| 55 | # Set pixels at invalid locations to 0 |
| 56 | warped[invalid[:, :, 0] | invalid[:, :, 1]] = 0 |
| 57 | |
| 58 | return warped |
| 59 | |
| 60 | |
| 61 | def horn_schunck( |