立体匹配中remap重映射前后的坐标点对应算法(python版)

目标:双目立体匹配中需要求得人脸映射前后的坐标对应关系

双目标定及矫正的参考链接:

C/C++实现双目矫正(不使用OpenCV内部函数)及矫正源码解析
matlab标定数据做双目相机矫正OpenCV C++

remap映射的坐标对应关系 python代码:

def remap(imgre, map_x, map_y, x0, y0, x1, y1):
    '''
    brief 进行映射+插值过程,并得到矫正后的检测框位置
    imgre           未矫正的左图
    map_x, map_y    矫正映射map
    x0, y0, x1, y1  检测框坐标点
    output:
    rec_img         矫正后的左图
    x2, y2, x3, y3  矫正后匹配的检测框坐标点

    '''

    rec_img = np.zeros(imgre.shape, np.uint8)
    x2, y2, x3, y3 = -100, -100, -100, -100

    for i in range(imgre.shape[1]):
        for j in range(imgre.shape[0]):
            x = int(map_x[j, i])
            y = int(map_y[j, i])
           
            if x > 1 and x < imgre.shape[1] - 1 and y > 1 and y < imgre.shape[0] - 1:
                u = map_x[j, i] - x
                v = map_y[j, i] - y
                rec_img[j, i] = int( (1 - u)*(1 - v)*imgre[y, x] + (1-u)*v*imgre[y+1, x] + u*(1-v)*imgre[y, x+1] + u*v*imgre[y+1, x+1] )

                if match(None, None, x0, y0, x, y) and x2 < -99:                    
                    x2 = i            
                    y2 = j   
                if match(None, None, x1, y1, x, y) and x3 < -99:                    
                    x3 = i
                    y3 = j
                        
    return rec_img, x2, y2, x3, y3
def match(map_x, map_y, x_det, y_det, x, y): # x y_det 为检测框坐标点, x y 为map_x y 中的遍历点
    '''
    brief 矫正前后的检测框匹配(嵌套在remap函数中, 使用其遍历map的for循环; 也可单独进行匹配, 只需传入map即可)
    map_x, map_y 矫正映射map
    x y_det      检测框坐标点
    x y          map_x y 中的遍历值
    (map_x, map_y  和  x y 使用其一即可)
    '''
    # print(map_x != None , x is None)
    # assert map_x != None and x is None, 'map and (x, y) must have one.' 
    if x != None:
        if abs(x - x_det) < 2 and abs(y - y_det) < 2:   # 匹配3*3邻域             
            return True
        else:
            return False

你可能感兴趣的:(双目相机,1024程序员节)