Halcon馬賽圖拼接(多圖平移)

ImgPath := 'C:/Users/zjm/Desktop/圖像拼接/新建文件夾/'
ImgName := 'bga_r_'
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'white', WindowHandle1)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
CamParam := [0.0121693,-2675.63,7.40046e-006,7.4e-006,1920/2,1080/2,1920,1080]
*根據指定的徑向畸變係數畸變係數修改了內部攝像機參數。
change_radial_distortion_cam_par ('adaptive', CamParam, 0, CamParOut)//自適應

gen_empty_obj (Images)
for J := 1 to 8 by 1
    read_image (Image, ImgPath +J+'.jpg')
    * 改變圖像的徑向失真。
    change_radial_distortion_image (Image, Image, Image, CamParam, CamParOut)
    concat_obj (Images, Image, Images)
    dev_display (Image)
    disp_message (WindowHandle1, 'Image ' + J$'d', 'window', 20, 10, 'green', 'false')
    wait_seconds (0.5)
endfor
disp_continue_message (WindowHandle1, 'black', 'true')

*顯示用於計算投影的點匹配圖像之間的變換,我們將把所有的圖像放大顯示
*平鋪的圖像,在圖像之間留有一定的空間,以便擴展圖片很容易看到
dev_set_window_extents (-1, -1, 1920*4, 1080*2)
*在窗口平鋪顯示
tile_images_offset (Images, TiledImage, [0,0,0,0,1080,1080,1080,1080], [0,1920,1920*2,1920*3,0,1920,1920*2,1920*3], [-1,-1,-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1,-1,-1], 1920*4, 1080*2)
dev_clear_window ()
dev_display (TiledImage)
disp_message (WindowHandle1, 'All 10 images', 'window', 20, 10, 'green', 'false')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()

*現在我們計算出對圖像和這兩個圖像之間的點匹配。圖像對之間的變換。
* dev_clear_window ()
* dev_display (TiledImage)
disp_message (WindowHandle1, 'Point matches', 'window', 20, 10, 'green', 'false')
* 定義配置。From圖片與To圖對應起來進行拼接,規則(下,下右,右)
From := [1,2,3]
To := [2,3,4]
Num := |From|
*我們需要變量來累積投影變換矩陣,匹配點的座標和匹配的數量。每個圖像對點。
ProjMatrices := []
Rows1 := []
Cols1 := []
Rows2 := []
Cols2 := []
NumCorrespondences := []
* 現在我們可以確定圖像對之間的變換。
for J := 0 to Num - 1 by 1
    F := From[J]
    T := To[J]
    select_obj (Images, ImageF, F)
    select_obj (Images, ImageT, T)
    * 提取兩個圖像中的點。
    SigmaGrad := 1
    SigmaSmooth := 3
    Alpha := 0.04
    Threshold := 0
    points_harris (ImageF, SigmaGrad, SigmaSmooth, Alpha, Threshold, RowFAll, ColFAll)
    points_harris (ImageT, SigmaGrad, SigmaSmooth, Alpha, Threshold, RowTAll, ColTAll)
    * 取兩個圖像中的點。
    *獲取的結果爲圖像右上方的座標點,F和T爲初始圖和匹配目標圖FR\FC和TR\TC爲
    if (F > 4)
        FShiftR := 1100
        FShiftC := (F - 5) * 1940
    else
        FShiftR := 0
        FShiftC := (F - 1) * 1940
    endif
    if (T > 4)
        TShiftR := 1100
        TShiftC := (T -5) * 1940
    else
        TShiftR := 0
        TShiftC := (T - 1) * 1940
    endif
    *初始圖和匹配圖偏移量
    RowMove := (FShiftR - TShiftR) / 2.7
    ColMove := (FShiftC - TShiftC) / 2
    * 執行匹配
    *掩碼窗口的大小爲MaskSize*MaskSize
    MaskSize := 21
    *半高度匹配的搜索窗口爲20
    RowTolerance := 20
    *半寬度的匹配搜索窗口爲20。  
    ColTolerance := 20
    *旋轉角度範圍
    Rotation := 0
    *灰度值匹配的閾值。
    MatchThreshold := 50
    *轉換一致性檢查的閾值。
    DistanceThreshold := 0.4
    *爲隨機數生成器種子。
    RandSeed := 4364537
    *通過尋找點之間的對應關係,計算兩個圖像之間的投影變換矩陣。
    *ssd”,則使用灰色差的平方之和,“sad”表示絕對差的和,“ncc”表示歸一化的互相關
     proj_match_points_ransac (ImageF, ImageT, RowFAll, ColFAll, RowTAll, ColTAll, 'sad', MaskSize, RowMove, ColMove, RowTolerance, ColTolerance, Rotation, MatchThreshold, 'gold_standard', DistanceThreshold, RandSeed, ProjMatrix, Points1, Points2)
    * 積累的變換矩陣/將獲取的變換矩陣進行疊加保存至ProjMatrice
     ProjMatrices := [ProjMatrices,ProjMatrix]
    Rows1 := [Rows1,subset(RowFAll,Points1)]
    Cols1 := [Cols1,subset(ColFAll,Points1)]
    Rows2 := [Rows2,subset(RowTAll,Points2)]
    Cols2 := [Cols2,subset(ColTAll,Points2)]
     *將每次匹配後的特徵點個數保存起來
     NumCorrespondences := [NumCorrespondences,|Points1|]
endfor
disp_continue_message (WindowHandle1, 'black', 'true')
* 
* 設置可能的轉換類型和堆棧順序
PossibleTransformations := ['projective','affine','similarity','rigid']
*參考圖像的索引。
StartImage := 3
*馬賽克圖像的疊加順序。
StackingOrder := [1,2,3,4,5,6,7,8]
*輸入圖像的域是否被轉換
TransformRegion := 'false'

* 最後,我們可以從投影變換中生成調整後的拼接圖像。
* for i := 0 to |PossibleTransformations| - 1 by 1
*     Transformation := PossibleTransformations[i]
*將多張圖片進行捆綁用於生成馬賽克圖片
bundle_adjust_mosaic (8, StartImage, From, To, ProjMatrices, Rows1, Cols1, Rows2, Cols2, NumCorrespondences, 'projective', MosaicMatrices2D, Rows, Cols, Error)
   *生成馬賽克圖
gen_bundle_adjusted_mosaic (Images, MosaicImage, MosaicMatrices2D, StackingOrder, TransformRegion, TransMat2D) 
    stop ()
* endfor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章