ransac去除誤匹配點對
阿新 • • 發佈:2018-11-20
****************************************************
*****************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])