memo: OpenCV

remap

Docs - OpenCV: Remapping

Use cv2.remap to do image warpping:

Homography mapping determines the projection location on the source image for a reference image.

r e ( ( ( f 0 1 2 , , , p 0 0 0 i ) ) ) x e ( ( ( l 0 1 2 , , , c 1 1 1 o ) ) ) o r ( ( ( d 0 2 2 s , , , 2 2 2 ) ) ) p o ( ( ( i x x x n y y y t z z z s ) ) ) w ( ( ( o x x x r y y y l z z z d ) ) ) c ( ( ( o x x x o y y y r z z z d ) ) ) s ( ( ( p x x x r , , , o y y y j ) ) ) s ( ( ( o x x x n , , , y y y s ) ) ) r c ( ( ( x x x i , , , m y y y g ) ) )

By fetching the pixels of the source image to the grid, according to the projection coordinates, a warpped source image is obtained.

Therefore, remap requires the (x,y) “new arrange pattern” of the source image as input, along with the source image.

Example in CasMVSNet-pl:

1
2
3
4
5
6
7
8
9
xy_ref = np.mgrid[:img_wh[1],:img_wh[0]][::-1].astype(np.float32)
xy_src = xy_ref2src(xy_ref, depth_ref, P_world2ref,
                    depth_src, P_world2src, img_wh)

# Sample the depth of xy_src using bilinear interpolation
depth_src2ref = cv2.remap(depth_src,
                          xy_src[0].astype(np.float32),
                          xy_src[1].astype(np.float32),
                          interpolation=cv2.INTER_LINEAR)

resize

(2024-03-14)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
img_ref = cv2.imread(os.path.join(root_dir, # channel is BGR
           f'Rectified/{scan}/rect_{vid+1:03d}_3_r5000.png'))

# scale to specified size
img_ref = cv2.resize(img_ref, tuple(args.img_wh),
          interpolation=cv2.INTER_LINEAR)[:,:,::-1] # to RGB

# scaling with factor, h and w both increase 4 times
proba_ref = cv2.resize(proba_ref, None, fx=4, fy=4,
            interpolation=cv2.INTER_LINEAR)