Inverting a real-valued index grid

前端 未结 7 884
南笙
南笙 2020-12-24 15:37

OpenCV\'s remap() uses a real-valued index grid to sample a grid of values from an image using bilinear interpolation, and returns the grid of samples as a new image.

<
相关标签:
7条回答
  • 2020-12-24 16:14

    You can invert map at known points and interpolate it into new grid. It will work fine, while distortion is not very huge.

    Here is very simple implementation in Python using scipy.interpolate.griddata:

    map_x, map_y = cv2.initUndistortRectifyMap(K, D, None, new_K, image_size, cv2.CV_32FC1)
    
    points =  np.stack([map_x.flatten(), map_y.flatten()], axis=1)
    grid = np.mgrid[:map_x.shape[0], :map_y.shape[1]]
    values = grid.reshape(2, -1).T[..., ::-1] 
    
    from scipy.interpolate import griddata
    grid_y, grid_x = grid
    map_back = griddata(points, values, (grid_x, grid_y), method='cubic').astype(map_undistort.dtype)
    

    If you use CV_32FC2 for maps, you can simplify points construction:

    map_undistort, _ = cv2.initUndistortRectifyMap(K, D, None, new_K, image_size, cv2.CV_32FC2)
    points = map_undistort.reshape(-1, 2)
    
    0 讨论(0)
提交回复
热议问题