ransac去除误匹配点对

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22904277/article/details/81868279
****************************************************
*****************ransac去除匹配点对******************
****************************************************
dev_update_off ()
*-----------第一,读入数据并生成二维轮廓
path_base := 'H:/测试数据/'
dev_clear_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (HMapX, path_base + 'HMapX'+ 0 +'.hobj')
read_image (HMapY, path_base + 'HMapY'+ 0 +'.hobj')
get_image_size (HMapX, Width, Height)
gen_rectangle1 (Roi_Row, 0, 0, 0, Width-1)
intersection (HMapX, Roi_Row, Roi_Row)
get_region_points (Roi_Row, Rows, Columns)
get_grayval (HMapX, Rows, Columns, Pos_Xs)
get_grayval (HMapY, Rows, Columns, Pos_Ys)
gen_contour_polygon_xld (Contour, Pos_Ys, Pos_Xs)

*------------第二、对同一轮廓数据进行一定变换,并对其偶数下标的数据进行shuffle
hom_mat2d_identity (HomMat2D)
hom_mat2d_translate (HomMat2D, -0, 200, HomMat2D)
hom_mat2d_rotate (HomMat2D, rad(-20), 0, 0, HomMat2D)
affine_trans_contour_xld (Contour, Contour2, HomMat2D)
get_contour_xld (Contour2, Reg_Ys, Reg_Xs)
*shuffle操作,
half_shuffle:=[0:2:|Reg_Ys|]
tuple_shuffle (half_shuffle, Sequence)
Reg_Ys[half_shuffle]:=Reg_Ys[Sequence]
Reg_Xs[half_shuffle]:=Reg_Xs[Sequence]
*画出的误匹配点对(原始的和shuffle后的)
disp_line (WindowHandle,Pos_Ys, Pos_Xs, Reg_Ys, Reg_Xs)

*-----------第三,ransac
pt_number:=|Reg_Ys|
*二维点最少2个点对,三维3个
sampleSize:=2
idx_num_flag:=0

for iter:=1 to 10 by 1
    *①每次迭代生成(1-pt_number)之间的sampleSize个不同的随机数
    base_num:=pt_number-1
    sample:=base_num*rand(sampleSize)
    tuple_round (sample, sample)
    *随机的2个对应点对计算变换矩阵
    vector_to_rigid (Reg_Xs[sample], Reg_Ys[sample],Pos_Xs[sample], Pos_Ys[sample], CurrentHomMat2D)
    idx := []
    *②注册后求出对应点间的距离,如果距离小于一个阈值,则保留该点对
    for i:=0 to pt_number-1 by 1
        affine_trans_point_2d (CurrentHomMat2D, Reg_Xs[i], Reg_Ys[i], reg_x, reg_y)
        distance_pp (reg_y, reg_x, Pos_Ys[i], Pos_Xs[i], Dis)
        if(Dis<1)
            idx:=[idx,i]
        endif
    endfor
    *③保留迭代中数量最多的inliers(对应点对下标)
    idx_num:=|idx|
    if(idx_num>idx_num_flag)
        idx_num_flag:=idx_num
        inliers:=idx
    endif
endfor
*正确的点对求解变换矩阵,如果去误匹配成功的话,其inliers基本为奇数,不全是奇数的原因是此前的shuffle可以出现,原有的位置不变
vector_to_rigid (Reg_Xs[inliers], Reg_Ys[inliers],Pos_Xs[inliers], Pos_Ys[inliers], HomMat2D)
*画出正确的对应点间的连线
disp_line (WindowHandle,Pos_Ys[inliers], Pos_Xs[inliers], Reg_Ys[inliers], Reg_Xs[inliers])

猜你喜欢

转载自blog.csdn.net/qq_22904277/article/details/81868279