# Got colored border after using cv2.remap function for undistortion

**URL:** <https://forum.opencv.org/t/got-colored-border-after-using-cv2-remap-function-for-undistortion/17198>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [April 8, 2024, 3:01am UTC](https://forum.opencv.org/t/got-colored-border-after-using-cv2-remap-function-for-undistortion/17198 "2024-04-08T03:01:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiahui97](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jiahui97](https://forum.opencv.org/u/jiahui97)\
**Post date:** [April 8, 2024, 3:01am UTC](https://forum.opencv.org/t/got-colored-border-after-using-cv2-remap-function-for-undistortion/17198/1 "2024-04-08T03:01:21Z")

</div>

When using cv2.remap function to remap distorted 1920 \* 1080 pixels image to undistorted 2912 \* 1184 pixels, I have got right results, but the color of border look strange, (there are red blue, pink…), how can I get image with black borders?

 ![20240221_010728_511_](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/07ad8c7dc1ed2eb035f64ca10697fd66f55e1628.jpeg)

Here is my operation and code:  
First calculating the undistort rectify map by camera parameters and distortion coefficients, the size of the map is (4, 1184 \* 2912), where 4 is x, y of target image (dst\_y, dst\_x), and x, y of source image (src\_y, src\_x), 1184\*2912 is the number of pixels in target image.  
Then I reshape the value of src\_y, src\_x to got map\_y, map\_x, and using cv2.remap to undistort the image:

```auto
def cv2_remap(img_src, points_map):
    map_y = points_map[2, :].reshape(1184, 2912).astype(np.float32)
    map_x = points_map[3, :].reshape(1184, 2912).astype(np.float32)
    img_dst = cv2.remap(img_src, map_x, map_y, interpolation=cv2.INTERSECT_NONE,
                        borderMode=cv2.BORDER_TRANSPARENT, borderValue=(0, 0, 0))
    return img_dst

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [April 8, 2024, 6:13am UTC](https://forum.opencv.org/t/got-colored-border-after-using-cv2-remap-function-for-undistortion/17198/2 "2024-04-08T06:13:15Z")

</div>

have you looked up the border mode in the documentation?

and what’s going on with your interpolation flag?

---

<div class="post-metadata">

**Author:** ![jiahui97](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jiahui97](https://forum.opencv.org/u/jiahui97)\
**Post date:** [April 9, 2024, 5:25am UTC](https://forum.opencv.org/t/got-colored-border-after-using-cv2-remap-function-for-undistortion/17198/3 "2024-04-09T05:25:27Z")

</div>

I tried to modify the border mode, and still got the same result. But I think I found the reason. It is due to the undistort map I calculated. I added a logic that when the corresponding coordinates is beyond the size of source image, the dst\_y, dst\_x, src\_y and src\_x will be set as [0,0,0,0]. This lead to the pixels in the border of target image always get the values of (0, 0) pixel in source image. I have corrected the code and I think this issue can be closed. Thank you for your reply, best wishes!
