ArUcoCreateMarkerExample.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.SceneManagement;
  4. using System.IO;
  5. using System.Collections;
  6. using OpenCVForUnity.CoreModule;
  7. using OpenCVForUnity.ArucoModule;
  8. using OpenCVForUnity.ImgcodecsModule;
  9. using OpenCVForUnity.UnityUtils;
  10. namespace OpenCVForUnityExample
  11. {
  12. /// <summary>
  13. /// ArUco Create Marker Example
  14. /// Referring to https://github.com/opencv/opencv_contrib/blob/master/modules/aruco/samples/create_marker.cpp.
  15. /// </summary>
  16. public class ArUcoCreateMarkerExample : MonoBehaviour
  17. {
  18. /// <summary>
  19. /// The size of the output marker image (px).
  20. /// </summary>
  21. public int markerSize = 1000;
  22. /// <summary>
  23. /// The marker type.
  24. /// </summary>
  25. public MarkerType markerType = MarkerType.CanonicalMarker;
  26. /// <summary>
  27. /// The marker type dropdown.
  28. /// </summary>
  29. public Dropdown markerTypeDropdown;
  30. /// <summary>
  31. /// The dictionary identifier.
  32. /// </summary>
  33. public ArUcoDictionary dictionaryId = ArUcoDictionary.DICT_6X6_250;
  34. /// <summary>
  35. /// The dictionary id dropdown.
  36. /// </summary>
  37. public Dropdown dictionaryIdDropdown;
  38. /// <summary>
  39. /// The marker identifier.
  40. /// </summary>
  41. public MarkerID markerId = MarkerID.MarkerID_1;
  42. /// <summary>
  43. /// The marker id dropdown.
  44. /// </summary>
  45. public Dropdown markerIdDropdown;
  46. /// <summary>
  47. /// The save path input field.
  48. /// </summary>
  49. public InputField savePathInputField;
  50. /// <summary>
  51. /// The marker img mat.
  52. /// </summary>
  53. Mat markerImg;
  54. /// <summary>
  55. /// The texture.
  56. /// </summary>
  57. Texture2D texture;
  58. // width of the marker borders.
  59. const int borderBits = 1;
  60. // for GridBoard.
  61. // number of markers in X direction
  62. const int gridBoradMarkersX = 5;
  63. // number of markers in Y direction
  64. const int gridBoradMarkersY = 7;
  65. // marker side length (normally in meters)
  66. const float gridBoradMarkerLength = 0.04f;
  67. // separation between two markers (same unit as markerLength)
  68. const float gridBoradMarkerSeparation = 0.01f;
  69. // id of first marker in dictionary to use on board.
  70. const int gridBoradMarkerFirstMarker = 0;
  71. // minimum margins (in pixels) of the board in the output image
  72. const int gridBoradMarginSize = 10;
  73. // for ChArUcoBoard.
  74. // number of chessboard squares in X direction
  75. const int chArUcoBoradMarkersX = 5;
  76. // number of chessboard squares in Y direction
  77. const int chArUcoBoradMarkersY = 7;
  78. // chessboard square side length (normally in meters)
  79. const float chArUcoBoradSquareLength = 0.04f;
  80. // marker side length (same unit than squareLength)
  81. const float chArUcoBoradMarkerLength = 0.02f;
  82. // minimum margins (in pixels) of the board in the output image
  83. const int chArUcoBoradMarginSize = 10;
  84. // Use this for initialization
  85. void Start ()
  86. {
  87. markerImg = new Mat (markerSize, markerSize, CvType.CV_8UC3);
  88. texture = new Texture2D (markerImg.cols (), markerImg.rows (), TextureFormat.RGB24, false);
  89. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  90. markerTypeDropdown.value = (int)markerType;
  91. markerIdDropdown.value = (int)markerId;
  92. dictionaryIdDropdown.value = (int)dictionaryId;
  93. CreateMaeker ();
  94. }
  95. // Update is called once per frame
  96. void Update ()
  97. {
  98. }
  99. private void CreateMaeker ()
  100. {
  101. if (markerImg.cols () != markerSize) {
  102. markerImg.Dispose ();
  103. markerImg = new Mat (markerSize, markerSize, CvType.CV_8UC3);
  104. texture = new Texture2D (markerImg.cols (), markerImg.rows (), TextureFormat.RGB24, false);
  105. } else {
  106. markerImg.setTo (Scalar.all (255));
  107. }
  108. gameObject.transform.localScale = new Vector3 (markerImg.cols (), markerImg.rows (), 1);
  109. float width = markerImg.width () / 0.7f;
  110. float height = markerImg.height () / 0.7f;
  111. float widthScale = (float)Screen.width / width;
  112. float heightScale = (float)Screen.height / height;
  113. if (widthScale < heightScale) {
  114. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  115. gameObject.transform.localPosition = new Vector3 (0, -height * 0.1f, 0);
  116. } else {
  117. Camera.main.orthographicSize = height / 2;
  118. gameObject.transform.localPosition = new Vector3 (width * 0.1f, 0, 0);
  119. }
  120. // create dictinary.
  121. Dictionary dictionary = Aruco.getPredefinedDictionary ((int)dictionaryId);
  122. // draw marker.
  123. switch (markerType) {
  124. default:
  125. case MarkerType.CanonicalMarker:
  126. Aruco.drawMarker (dictionary, (int)markerId, markerSize, markerImg, borderBits);
  127. Debug.Log ("draw CanonicalMarker: " + "dictionaryId " + (int)dictionaryId + " markerId " + (int)markerId + " sidePixels " + markerSize + " borderBits " + borderBits);
  128. break;
  129. case MarkerType.GridBoard:
  130. GridBoard gridBoard = GridBoard.create (gridBoradMarkersX, gridBoradMarkersY, gridBoradMarkerLength, gridBoradMarkerSeparation, dictionary, gridBoradMarkerFirstMarker);
  131. gridBoard.draw (new Size (markerSize, markerSize), markerImg, gridBoradMarginSize, borderBits);
  132. gridBoard.Dispose ();
  133. Debug.Log ("draw GridBoard: " + "markersX " + gridBoradMarkersX + " markersY " + gridBoradMarkersY + " markerLength " + gridBoradMarkerLength +
  134. " markerSeparation " + gridBoradMarkerSeparation + "dictionaryId " + (int)dictionaryId + " outSize " + markerSize + " marginSize " + gridBoradMarginSize + " borderBits " + borderBits);
  135. break;
  136. case MarkerType.ChArUcoBoard:
  137. CharucoBoard charucoBoard = CharucoBoard.create (chArUcoBoradMarkersX, chArUcoBoradMarkersY, chArUcoBoradSquareLength, chArUcoBoradMarkerLength, dictionary);
  138. charucoBoard.draw (new Size (markerSize, markerSize), markerImg, chArUcoBoradMarginSize, borderBits);
  139. charucoBoard.Dispose ();
  140. Debug.Log ("draw ChArUcoBoard: " + "markersX " + chArUcoBoradMarkersX + " markersY " + chArUcoBoradMarkersY + " markerLength " + chArUcoBoradSquareLength +
  141. " markerSeparation " + chArUcoBoradMarkerLength + "dictionaryId " + (int)dictionaryId + " outSize " + markerSize + " marginSize " + chArUcoBoradMarginSize + " borderBits " + borderBits);
  142. break;
  143. }
  144. Utils.matToTexture2D (markerImg, texture, true, 0, true);
  145. }
  146. private void SaveMarkerImg ()
  147. {
  148. // save the markerImg.
  149. string saveDirectoryPath = Path.Combine (Application.persistentDataPath, "ArUcoCreateMarkerExample");
  150. string savePath = "";
  151. #if UNITY_WEBGL && !UNITY_EDITOR
  152. string format = "jpg";
  153. MatOfInt compressionParams = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 100);
  154. #else
  155. string format = "png";
  156. MatOfInt compressionParams = new MatOfInt (Imgcodecs.IMWRITE_PNG_COMPRESSION, 0);
  157. #endif
  158. switch (markerType) {
  159. default:
  160. case MarkerType.CanonicalMarker:
  161. savePath = Path.Combine (saveDirectoryPath, "CanonicalMarker-d" + (int)dictionaryId + "-i" + (int)markerId + "-sp" + markerSize + "-bb" + borderBits + "." + format);
  162. break;
  163. case MarkerType.GridBoard:
  164. savePath = Path.Combine (saveDirectoryPath, "GridBoard-mx" + gridBoradMarkersX + "-my" + gridBoradMarkersY + "-d" + (int)dictionaryId + "-os" + markerSize + "-bb" + borderBits + "." + format);
  165. break;
  166. case MarkerType.ChArUcoBoard:
  167. savePath = Path.Combine (saveDirectoryPath, "ChArUcoBoard-mx" + chArUcoBoradMarkersX + "-my" + chArUcoBoradMarkersY + "-d" + (int)dictionaryId + "-os" + markerSize + "-bb" + borderBits + "." + format);
  168. break;
  169. }
  170. if (!Directory.Exists (saveDirectoryPath)) {
  171. Directory.CreateDirectory (saveDirectoryPath);
  172. }
  173. Imgcodecs.imwrite (savePath, markerImg, compressionParams);
  174. savePathInputField.text = savePath;
  175. Debug.Log ("savePath: " + savePath);
  176. }
  177. /// <summary>
  178. /// Raises the destroy event.
  179. /// </summary>
  180. void OnDestroy ()
  181. {
  182. if (markerImg != null)
  183. markerImg.Dispose ();
  184. }
  185. /// <summary>
  186. /// Raises the back button click event.
  187. /// </summary>
  188. public void OnBackButtonClick ()
  189. {
  190. SceneManager.LoadScene ("OpenCVForUnityExample");
  191. }
  192. /// <summary>
  193. /// Raises the marker type dropdown value changed event.
  194. /// </summary>
  195. public void OnMarkerTypeDropdownValueChanged (int result)
  196. {
  197. if ((int)markerType != result) {
  198. markerType = (MarkerType)result;
  199. markerIdDropdown.interactable = (markerType == MarkerType.CanonicalMarker);
  200. CreateMaeker ();
  201. }
  202. }
  203. /// <summary>
  204. /// Raises the dictionary id dropdown value changed event.
  205. /// </summary>
  206. public void OnDictionaryIdDropdownValueChanged (int result)
  207. {
  208. if ((int)dictionaryId != result) {
  209. dictionaryId = (ArUcoDictionary)result;
  210. CreateMaeker ();
  211. }
  212. }
  213. /// <summary>
  214. /// Raises the marker id dropdown value changed event.
  215. /// </summary>
  216. public void OnMarkerIdDropdownValueChanged (int result)
  217. {
  218. if ((int)markerId != result) {
  219. markerId = (MarkerID)result;
  220. CreateMaeker ();
  221. }
  222. }
  223. /// <summary>
  224. /// Raises the save marker img button click event.
  225. /// </summary>
  226. public void OnSaveMarkerImgButtonClick ()
  227. {
  228. SaveMarkerImg ();
  229. }
  230. public enum MarkerType
  231. {
  232. CanonicalMarker,
  233. GridBoard,
  234. ChArUcoBoard,
  235. //ChArUcoDiamondMarker //Not yet implemented.
  236. }
  237. public enum ArUcoDictionary
  238. {
  239. DICT_4X4_50 = Aruco.DICT_4X4_50,
  240. DICT_4X4_100 = Aruco.DICT_4X4_100,
  241. DICT_4X4_250 = Aruco.DICT_4X4_250,
  242. DICT_4X4_1000 = Aruco.DICT_4X4_1000,
  243. DICT_5X5_50 = Aruco.DICT_5X5_50,
  244. DICT_5X5_100 = Aruco.DICT_5X5_100,
  245. DICT_5X5_250 = Aruco.DICT_5X5_250,
  246. DICT_5X5_1000 = Aruco.DICT_5X5_1000,
  247. DICT_6X6_50 = Aruco.DICT_6X6_50,
  248. DICT_6X6_100 = Aruco.DICT_6X6_100,
  249. DICT_6X6_250 = Aruco.DICT_6X6_250,
  250. DICT_6X6_1000 = Aruco.DICT_6X6_1000,
  251. DICT_7X7_50 = Aruco.DICT_7X7_50,
  252. DICT_7X7_100 = Aruco.DICT_7X7_100,
  253. DICT_7X7_250 = Aruco.DICT_7X7_250,
  254. DICT_7X7_1000 = Aruco.DICT_7X7_1000,
  255. DICT_ARUCO_ORIGINAL = Aruco.DICT_ARUCO_ORIGINAL,
  256. }
  257. public enum MarkerID
  258. {
  259. MarkerID_0,
  260. MarkerID_1,
  261. MarkerID_2,
  262. MarkerID_3,
  263. MarkerID_4,
  264. MarkerID_5,
  265. MarkerID_6,
  266. MarkerID_7,
  267. MarkerID_8,
  268. MarkerID_9,
  269. }
  270. }
  271. }