天天看點

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

一、ArUco項目源碼簡析

聲明:以下内容均是在虛拟機Ubuntu系統下進行操作,用開發闆Nano Pi的同學亦可同樣實作,但這裡為了友善截圖以及界面複制。

1、首先來看一下ArUco項目的源碼結構

bin目錄下有一些源碼自動生成的可執行檔案,像是一些demo,我們可以用它們實作ArUco的一些基本功能,比如相機标定、生成二維碼、檢測二維碼等等。

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

這些可執行檔案的源碼都可以在你的源碼包中找到,當然日後我們自己寫程式的時候完全可以參考這些源碼。因為個人感覺這個項目的源碼不是特别清晰,下面簡單梳理一下我們可以在哪個檔案夾下找到哪些可執行檔案的源碼:

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

細心的同學可能會發現,源碼包中還有好多源碼沒有被生成可執行檔案,這是因為在cmake配置檔案中把它們屏蔽掉了。

2、接下來運作源碼中幾個比較常用的demo

2.1、聲明

該庫附帶了幾個demo,可以幫助您學習如何使用該庫:

/utils

  • #/utils / aruco_print_marker:建立标記并将其儲存在可以列印的jpg檔案中。
  • #/utils / aruco_print_dictionary:将字典中訓示的所有标記(ARUCO,APRILTAGS,ARTOOLKIT +等)儲存到字典中。
  • #/utils / aruco_print_dictionary:将字典中訓示的所有标記(ARUCO,APRILTAGS,ARTOOLKIT +等)儲存到字典中。
  • #/utils / aruco_simple:檢測圖像中标記的簡單測試應用程式
  • #/utils / aruco_test:這是檢測的主要應用程式。它從視訊的攝像頭讀取圖像并檢測标記。此外,如果您提供相機的内在函數(通過OpenCv校準獲得)和以米為機關的标記大小,庫将計算标記内在函數,以便您可以輕松建立AR應用程式。
  • #/utils / aruco_tracker:顯示如何使用跟蹤器的示例。

/utils_markermap

  • #/utils_markermap / aruco_create_markermap:建立簡單的标記貼圖(舊闆)。它建立了一個可以在一張紙上列印的标記網格。

    該程式的結果是兩個檔案(.png和.yml).png檔案是您可以列印的标記的圖像。.yml檔案是您需要傳遞給其他程式的配置檔案,是以他們知道地圖是如何的。.yml包含标記的位置(以像素為機關)。由于我們事先并不知道列印标記的大小,我們在這裡使用像素。但是,為了獲得相機,需要知道标記的實際尺寸。對于此pourpose,您可以使用程式utils_markermap / aruco_markermap_pix2meters建立一個新的.yml,其中的地圖資訊以米為機關。或者,所有測試程式都允許您在指令行中訓示markersize。

  • #/utils_markermap / aruco_markermap_pix2meters将markermap配置檔案從像素轉換為米
  • #/utils_markermap / aruco_simple_markermap:顯示如何使用标記貼圖确定相機姿勢的簡單示例
  • #/utils_markermap / aruco_test_markermap:一個更詳細的示例,展示如何使用标記貼圖确定相機姿勢

/utils_calibration

  • #/utils_calibration / aruco_calibration:使用由aruco标記組成的棋盤校準相機的程式。在此目錄中,您可以找到可列印的校準闆aruco_calibration_board_a4.pdf。它是一個标記圖,其定義在aruco_calibration_board_a4.yml中。但是,它在程式中是寫死的,是以您甚至不需要将其作為參數傳遞。
  • #/utils_calibration / aruco_calibration_fromimages與上面相同,但是儲存在檔案中的圖像

/utils_gl

  • #/utils_gl / aruco_test_gl簡單示例,展示如何将aruco與OpenGL結合使用
  • 關于OPENGL的注意事項:該庫支援與OpenGL的內建。為了編譯支援OpenGL,您隻需在系統中安裝GL和過剩(或freeglut)的開發包。

2.2、aruco_calibration相機标定(前一篇文章有詳細講)

cd 到ArUco安裝目錄的bin目錄下,運作:$ ./aruco_calibration

j[email protected]:/usr/local/bin$ sudo ./aruco_calibration
           

Usage: (in.avi|live[:camera_index(e.g 0 or 1)])) out_camera_calibration.yml

[-m markermapConfig.yml (configuration of the board. If use default one (in utils), no need to set this)]

[-size :(value in meters of a marker. If you provide a board that contains that information, this is ommited) ]

[-save: if enabled, saves the calibration images used]

這是一個用ArUco闆标定相機的程式,因為你沒有給它合适的入口參數,是以程式列印一段“Usage:”就直接退出了。根據上面的用法介紹,正确的指令應該是:

sudo ./aruco_calibration live:0 out_camera_calibration.yml -m aruco_calibration_grid_board_a4.yml -size 0.038
           

運作指令前看一下你的目前檔案夾下有沒有aruco_calibration_grid_board_a4.pdf,将pdf檔案列印出來或者在另一台電腦上打開全屏顯示(雙顯示器的就更友善啦),然後用直尺量出單個marker的邊長(機關是米),如果跟我的不一樣要修改“-size”後面的值。

或者直接預設

sudo ./aruco_calibration live:0 out_camera_calibration.yml s -size 0.038
           
NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

運作效果如上圖所示,視窗左上角有操作提示,不贅述了。運作完了ESC鍵退出即可,相機标定檔案儲存在目前檔案夾下。

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

補充aruco_calibration_fromimages

同理,這裡隻是用image來實作。先看看需要哪些參數吧

sudo ./aruco_calibration_fromimages 
           

Usage: out_camera_calibration.yml directory_with_images [options]

options:

-size maker_size : Size of the markers in meters.

-m markersetconfig.yml : By default, the one in utils is assumed. Otherwise, set the file here

-auto_orient : forces width larger than height by flipping image 90degs.

是以隻需要按這個格式,即可:

sudo ./aruco_calibration_fromimages  out_camera_calibration.yml  *directory_with_images*
           

2.3、aruco_test 檢測識别 markers(前一篇文章有詳細講)

sudo ./aruco_test
           

Usage: (in.avi|live[:camera_index(e.g 0 or 1)]) [-c camera_params.yml]

[-s marker_size_in_meters] [-d dictionary:ALL_DICTS by default] [-h]

[-ws w:h] [-skip frames] Dictionaries: ARUCO ARUCO_MIP_16h3

ARUCO_MIP_25h7 ARUCO_MIP_36h12 ARTOOLKITPLUS ARTOOLKITPLUSBCH TAG16h5

TAG25h7 TAG25h9 TAG36h11 TAG36h10 CHILITAGS ALL_DICTS Instead of

these, you can directly indicate the path to a file with your own generated dictionary

sudo ./aruco_test live:0 -c out_camera_calibration.yml -s 0.038 
           

還用剛才的那個ArUco闆,來看一下這次的運作效果:

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

怎麼樣?圖檔是不是很炫酷!但這個東西其實對我們沒什麼用,真正有用的是在終端裡顯示的那一串串字元。每一行字元串代表的含義是:marker的id以及該marker的四個角點在圖像中的像素座标,Txyz為相機座标系到該marker座标系的平移向量,Rxyz為相機座标系到該marker座标系的旋轉向量。

2.4、aruco_print_marker 生成單個 marker

同上,就不再贅述了,每次先讀讀需要什麼參數

sudo ./aruco_print_marker
           

Usage: outfile.(jpg|png|ppm|bmp) [options] [-e use

enclsing corners] [-bs :bit size in pixels. 50 by default ]

[-d : ARUCO_MIP_36h12 default] [-border: adds the white

border around] [-center: highlights the center] Dictionaries:

ARUCO ARUCO_MIP_16h3 ARUCO_MIP_25h7 ARUCO_MIP_36h12 ARTOOLKITPLUS

ARTOOLKITPLUSBCH TAG16h5 TAG25h7 TAG25h9 TAG36h11 TAG36h10 CHILITAGS

ALL_DICTS Instead of these, you can directly indicate the path to a

file with your own generated dictionary

運作,每次先讀讀需要什麼參數,78表示碼的ids,marker.jpg表示生成的檔案,-d ARUCO表示生成的字典,-border表示生成白邊,-cente表示生成中心圓點。

sudo ./aruco_print_marker 78 marker.jpg -d ARUCO -border
           
NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

2.4、aruco_print_dictionary生成多個markers

sudo ./aruco_print_dictionary
           

Usage: outdir dictionary [ -s bit_image_size: 75 default]

Dictionaries: ARUCO ARUCO_MIP_16h3 ARUCO_MIP_25h7 ARUCO_MIP_36h12

ARTOOLKITPLUS ARTOOLKITPLUSBCH TAG16h5 TAG25h7 TAG25h9 TAG36h11

TAG36h10 CHILITAGS ALL_DICTS Instead of these, you can directly

indicate the path to a file with your own generated dictionary

sudo mkdir ARUCO_MIP_36h12_outdir
sudo  ./aruco_print_dictionary  ARUCO_MIP_36h12_outdir/  ARUCO_MIP_36h12
           
NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

在ARUCO_MIP_36h12_dir檔案夾下生成250個ARUCO_MIP_36h12字典中的marker。

**溫馨提示:**用ArUco官方庫生成的marker左下角自帶水印,為該marker的ID;用OpenCV的aruco module生成的marker不帶水印。重要的是,兩者雖然是同樣的編碼原理,但生成的很多marker不能互相識别,說明它們二者之間的字典内容還是有所不同的。是以,有時即使是用的同一個字典名,ArUco可能也無法識别OpenCV生成的marker。

2.5、aruco_batch_processing

該程式對一組圖像進行批處理。 它僅在2d中列印标記位置

sudo ./aruco_batch_processing
           

Usage: readDir outFile [params] [-d :ALL_DICTS default]

[-minSizeImage : minimum size of a marker in the image. Range

(0,1) 1 whole image] [-Fraser CX:CY:F:K1:K2:K3:P1:P2:b1:b2] fraser

calibration model[-MSize ] indicates the size of the

marker[-fast] uses the fast mode Dictionaries: ARUCO ARUCO_MIP_16h3

ARUCO_MIP_25h7 ARUCO_MIP_36h12 ARTOOLKITPLUS ARTOOLKITPLUSBCH TAG16h5

TAG25h7 TAG25h9 TAG36h11 TAG36h10 CHILITAGS ALL_DICTS Instead of

these, you can directly indicate the path to a file with your own

generated dictionary Example to work with apriltags dictionary :

video.avi -d TAG36h11

在目前目錄下生成放參數的檔案

sudo mkdir ARUCO_MIP_36h12_param
           

執行

sudo ./aruco_batch_processing ARUCO_MIP_36h12_outdir/  ARUCO_MIP_36h12_param/
           

為啥檔案夾裡沒有,這裡還不清楚,留待研究,但是程式執行沒有報錯,應該是參數的問題。

2.6、aruco_create_markermap建立标記并将其儲存在可以列印的jpg檔案中。

sudo ./aruco_create_markermap 
           

Usage: X:Y Image.png Configuration.yml [-d dictionary_name

(ARUCO_MIP_36h12 default)] [-s ] [-t (0: panel,1:

chessboard )] [-r rand_seed] [-i interMarkerDistance(0,1)] [-ids

n1:n2:… List the ids to be employed] Dictionaries: ARUCO

ARUCO_MIP_16h3 ARUCO_MIP_25h7 ARUCO_MIP_36h12 ARTOOLKITPLUS

ARTOOLKITPLUSBCH TAG16h5 TAG25h7 TAG25h9 TAG36h11 TAG36h10 CHILITAGS

ALL_DICTS

注釋:X:Y表示生成的靶标盤是行列是幾乘幾的,最後生成 Image.png 、Configuration.yml兩個檔案,-d 加字典,-s 加生成圖檔的像素大小,-t表示生成的jpg是那種格式有0、1兩種選項,-i表示靶标間隔0.1表示一格,-ids表示列印多個靶标需要的id(切記:和X:Y需要的多個靶标數量的一緻)

例:

sudo ./aruco_create_markermap 2:3 Panel.jpg  Panel_Configuration.yml -s 900 -t 0 -i 0.1 -ids 23:234:45:34:57:54
           
NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

補:aruco_markermap_pix2meters将markermap配置檔案從像素轉換為米

sudo ./aruco_markermap_pix2meters 
           

Usage: in_boardConfiguration.yml markerSize_meters

out_boardConfiguration.yml

sudo ./aruco_markermap_pix2meters Panel_Configuration.yml  0.4 out_Panel_Configuration.yml
           

如下,你可以打開out_Panel_Configuration.yml比較 Panel_Configuration.yml,可以發現角點的參數已經從像素點轉化成了米。

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

2.7、aruco_simple

sudo ./aruco_simple
           

Usage: (in_image|video.avi) [-c cameraParams.yml] [-s markerSize] [-d

:ALL_DICTS default] [-f arucoConfig.yml] Dictionaries:

ARUCO ARUCO_MIP_16h3 ARUCO_MIP_25h7 ARUCO_MIP_36h12 ARTOOLKITPLUS

ARTOOLKITPLUSBCH TAG16h5 TAG25h7 TAG25h9 TAG36h11 TAG36h10 CHILITAGS

ALL_DICTS Instead of these, you can directly indicate the path to a

file with your own generated dictionary Example to work with apriltags

dictionary : video.avi -d TAG36h11

這裡需要錄制一個視訊檔案,複制到要進行操作的檔案夾

sudo cp video.avi /usr/local/bin/
           
NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析

再執行指令:

sudo ./aruco_simple video.avi  -c out_camera_calibration.yml -s 0.038 
           

得到如下結果,能夠識别出

NanoPi M4開發opencv圖像識别aruco碼全過程(超詳細)(二:測試補充)一、ArUco項目源碼簡析