knn的方法查找最近点(halcon)

****************************************************
*用knn的方法查找最近点,并与常规方法进行比较
*由于有些点重复,比如说Pos_Xs的下标913,其值是相同的
*时间上的比较
*****************************************************
dev_update_off ()
path_base := 'H:/'
path_dxf :='H:/ContourStd.dxf'
read_image (HMapX, path_base + 'HMapX'+ 0 +'.hobj')
read_image (HMapY, path_base + 'HMapY'+ 0 +'.hobj')
read_image (HMask, path_base + 'HMask'+ 0 +'.hobj')
*---------------------------------------------------
get_image_size (HMask, 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)
Sequence := [0:(2 * |Pos_Xs|) - 1]
SequenceSel := Sequence / 2
XTriple := Pos_Xs[SequenceSel]
YTriple := Pos_Ys[SequenceSel]
XMult := (Sequence % 2) [==] 0
YMult := (Sequence % 2) [==] 1
XY := XTriple * XMult + YTriple * YMult
create_class_knn (2, KNNHandle)
add_sample_class_knn (KNNHandle, XY, 0)
train_class_knn (KNNHandle, [], [])
set_params_class_knn (KNNHandle, ['method','k'], ['neighbors_distance',1])
*-------------------------------------
get_image_size (HMask, Width, Height)
gen_rectangle1 (Roi_Row, 5, 0, 5, Width-1)
intersection (HMapX, Roi_Row, Roi_Row)
get_region_points (Roi_Row, Rows, Columns)
get_grayval (HMapX, Rows, Columns, Xs)
get_grayval (HMapY, Rows, Columns, Ys)
**求第6个剖面上的每个点的
count_seconds (a)
neighbor_index:=[]
neighbor_distance:=[]
for i:=0 to |Xs|-1 by 1
    TestPoint:=[Xs[i],Ys[i]]
    classify_class_knn (KNNHandle, TestPoint, NeighborIndices, Distance)
    get_sample_class_knn (KNNHandle, NeighborIndices, Features, ClassID)
    neighbor_index:=[neighbor_index,NeighborIndices]
    neighbor_distance:=[neighbor_distance,Distance]
endfor
count_seconds (b)
c:=b-a


count_seconds (d)

*验证:
test_index:=[]
test_distance:=[]
for i:=0 to |Xs|-1 by 1

    closest_index:=0
    min_dis:=1000
    distance_flag:=100000

     for j:=0 to |Pos_Ys|-1 by 1
        distance_pp (Xs[i], Ys[i], Pos_Xs[j], Pos_Ys[j], Dis)
        if(Dis < distance_flag)
            min_dis:= Dis
            closest_index:=j
        endif
        distance_flag:=min_dis 
     endfor
     test_distance:=[test_distance,min_dis]
     test_index:=[test_index,closest_index]
endfor
count_seconds (e)
f:=e-d
a:=test_distance-neighbor_distance

猜你喜欢

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