remap
Usage
Remaps an image to implement geometric transforms such as rotation, scaling, and distortion. It generates a transformed image by specifying locations of source image pixels in the destination image, and is widely used in fields such as image correction and stereo vision.
Interface
1 | cv2.remap(src, map1, map2, interpolation=cv2.INTER_LINEAR, dst=None, borderMode=cv2.BORDER_CONSTANT, borderValue=None) |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Input image. |
Not null |
Input |
map1 |
2D array containing the X-coordinate of each pixel in the source image (or the first part of the pixel mapping matrix). |
32-bit floating point number within the range of [0, width–1], where width is the width of the source image. |
Input |
map2 |
2D array containing the Y-coordinate of each pixel in the source image (or the second part of the pixel mapping matrix). |
32-bit floating point number within the range of [0, height–1], where height is the height of the source image. |
Input |
interpolation |
Interpolation mode. The default value is cv2.INTER_LINEAR. Other options include cv2.INTER_NEAREST and cv2.INTER_CUBIC. |
cv2.INTER_LINEAR, cv2.INTER_NEAREST, cv2.INTER_CUBIC, etc. |
Input |
dst |
(Optional) Output image. A new image is created by default. |
Not null |
Output |
borderMode |
Border mode. The default value is cv2.BORDER_CONSTANT which determines how to process pixels outside the source image. |
Common modes: cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, and cv2.BORDER_REFLECT |
Input |
borderValue |
Border value, which is valid only when borderMode is set to BORDER_CONSTANT. |
|
Input |
Return Value
- Output image
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import numpy as np import cv2 # Create a 5x5 image. src = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint8) # Create a mapping matrix to define image remapping. map_x = np.array([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], dtype=np.float32) map_y = np.array([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4]], dtype=np.float32) dst = cv2.remap(src, map_x, map_y, cv2.INTER_LINEAR) print(dst) |
Output:
1 2 3 4 5 | [[0 0 0 0 0] [0 1 1 1 0] [0 1 0 1 0] [0 1 1 1 0] [0 0 0 0 0]] |