MatBasicProcessingExample.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.SceneManagement;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using OpenCVForUnity.CoreModule;
  8. using OpenCVForUnity.UnityUtils;
  9. namespace OpenCVForUnityExample
  10. {
  11. /// <summary>
  12. /// Mat Basic Processing Example
  13. /// </summary>
  14. public class MatBasicProcessingExample : MonoBehaviour
  15. {
  16. public ScrollRect exampleCodeScrollRect;
  17. public UnityEngine.UI.Text exampleCodeText;
  18. public ScrollRect executionResultScrollRect;
  19. public UnityEngine.UI.Text executionResultText;
  20. // Use this for initialization
  21. IEnumerator Start ()
  22. {
  23. // fix the screen orientation.
  24. Screen.orientation = ScreenOrientation.LandscapeLeft;
  25. // wait for the screen orientation to change.
  26. yield return null;
  27. }
  28. // Update is called once per frame
  29. void Update ()
  30. {
  31. }
  32. /// <summary>
  33. /// Raises the destroy event.
  34. /// </summary>
  35. void OnDestroy ()
  36. {
  37. Screen.orientation = ScreenOrientation.AutoRotation;
  38. }
  39. private void UpdateScrollRect ()
  40. {
  41. exampleCodeScrollRect.verticalNormalizedPosition = executionResultScrollRect.verticalNormalizedPosition = 1f;
  42. }
  43. public void OnBackButtonClick ()
  44. {
  45. SceneManager.LoadScene ("OpenCVForUnityExample");
  46. }
  47. public void OnInitializationExampleButtonClick ()
  48. {
  49. //
  50. // initialization example
  51. //
  52. // 3x3 matrix (set array value)
  53. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  54. mat1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  55. Debug.Log ("mat1=" + mat1.dump ());
  56. // 2x2 rotation matrix
  57. double angle = 30, a = Math.Cos (angle * Math.PI / 180), b = Math.Sin (angle * Math.PI / 180);
  58. Mat mat2 = new Mat (2, 2, CvType.CV_64FC1);
  59. mat2.put (0, 0, a, -b, b, a);
  60. Debug.Log ("mat2=" + mat2.dump ());
  61. // 5x5 all 1’s matrix
  62. Mat mat3 = Mat.ones (5, 5, CvType.CV_64FC1);
  63. Debug.Log ("mat3=" + mat3.dump ());
  64. // 5x5 all zero’s matrix
  65. Mat mat4 = Mat.zeros (5, 5, CvType.CV_64FC1);
  66. Debug.Log ("mat4=" + mat4.dump ());
  67. // 5x5 identity matrix
  68. Mat mat5 = Mat.eye (5, 5, CvType.CV_64FC1);
  69. Debug.Log ("mat5=" + mat5.dump ());
  70. // 3x3 initialize with a constant
  71. Mat mat6 = new Mat (3, 3, CvType.CV_64FC1, new Scalar (5));
  72. Debug.Log ("mat6=" + mat6.dump ());
  73. // 3x2 initialize with a uniform distribution random number
  74. Mat mat7 = new Mat (3, 2, CvType.CV_8UC1);
  75. Core.randu (mat7, 0, 256);
  76. Debug.Log ("mat7=" + mat7.dump ());
  77. // 3x2 initialize with a normal distribution random number
  78. Mat mat8 = new Mat (3, 2, CvType.CV_8UC1);
  79. Core.randn (mat8, 128, 10);
  80. Debug.Log ("mat8=" + mat8.dump ());
  81. exampleCodeText.text = @"
  82. //
  83. // initialization example
  84. //
  85. // 3x3 matrix (set array value)
  86. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  87. mat1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  88. Debug.Log (""mat1="" + mat1.dump());
  89. // 2x2 rotation matrix
  90. double angle = 30, a = Math.Cos(angle*Math.PI/180), b = Math.Sin(angle*Math.PI/180);
  91. Mat mat2 = new Mat (2, 2, CvType.CV_64FC1);
  92. mat2.put (0, 0, a, -b, b, a);
  93. Debug.Log (""mat2="" + mat2.dump());
  94. // 5x5 all 1’s matrix
  95. Mat mat3 = Mat.ones(5, 5, CvType.CV_64FC1);
  96. Debug.Log (""mat3="" + mat3.dump());
  97. // 5x5 all zero’s matrix
  98. Mat mat4 = Mat.zeros(5, 5, CvType.CV_64FC1);
  99. Debug.Log (""mat4="" + mat4.dump());
  100. // 5x5 identity matrix
  101. Mat mat5 = Mat.eye(5, 5, CvType.CV_64FC1);
  102. Debug.Log (""mat5="" + mat5.dump());
  103. // 3x3 initialize with a constant
  104. Mat mat6 = new Mat (3, 3, CvType.CV_64FC1, new Scalar(5));
  105. Debug.Log (""mat6="" + mat6.dump());
  106. // 3x2 initialize with a uniform distribution random number
  107. Mat mat7 = new Mat (3, 2, CvType.CV_8UC1);
  108. Core.randu (mat7, 0, 256);
  109. Debug.Log (""mat7="" + mat7.dump());
  110. // 3x2 initialize with a normal distribution random number
  111. Mat mat8 = new Mat (3, 2, CvType.CV_8UC1);
  112. Core.randn (mat8, 128, 10);
  113. Debug.Log (""mat8="" + mat8.dump());
  114. ";
  115. executionResultText.text = "mat1=" + mat1.dump () + "\n";
  116. executionResultText.text += "mat2=" + mat2.dump () + "\n";
  117. executionResultText.text += "mat3=" + mat3.dump () + "\n";
  118. executionResultText.text += "mat4=" + mat4.dump () + "\n";
  119. executionResultText.text += "mat5=" + mat5.dump () + "\n";
  120. executionResultText.text += "mat6=" + mat6.dump () + "\n";
  121. executionResultText.text += "mat7=" + mat7.dump () + "\n";
  122. executionResultText.text += "mat8=" + mat8.dump () + "\n";
  123. UpdateScrollRect ();
  124. }
  125. public void OnMultiChannelExampleButtonClick ()
  126. {
  127. //
  128. // multi channel example
  129. //
  130. // 64F, channels=1, 3x3
  131. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  132. Debug.Log ("mat1");
  133. Debug.Log (" dim:" + mat1.dims () + " elemSize1:" + mat1.elemSize1 () + " channel:" + mat1.channels ());
  134. // 64F, channels=10, 3x3
  135. Debug.Log ("mat2");
  136. Mat mat2 = new Mat (3, 3, CvType.CV_64FC (10));
  137. Debug.Log (" dim:" + mat2.dims () + " elemSize1:" + mat2.elemSize1 () + " channels:" + mat2.channels ());
  138. exampleCodeText.text = @"
  139. //
  140. // multi channel example
  141. //
  142. // 64F, channels=1, 3x3
  143. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  144. Debug.Log (""mat1"");
  145. Debug.Log ("" dim:"" + mat1.dims() + "" elemSize1:"" + mat1.elemSize1() + "" channel:"" + mat1.channels());
  146. // 64F, channels=10, 3x3
  147. Debug.Log (""mat2"");
  148. Mat mat2 = new Mat (3, 3, CvType.CV_64FC(10));
  149. Debug.Log ("" dim:"" + mat2.dims() + "" elemSize1:"" + mat2.elemSize1() + "" channels:"" + mat2.channels());
  150. ";
  151. executionResultText.text = "mat1" + "\n";
  152. executionResultText.text += " dim:" + mat1.dims () + " elemSize1:" + mat1.elemSize1 () + " channels:" + mat1.channels () + "\n";
  153. executionResultText.text += "mat2" + "\n";
  154. executionResultText.text += " dim:" + mat2.dims () + " elemSize1" + mat2.elemSize1 () + " channels:" + mat2.channels () + "\n";
  155. UpdateScrollRect ();
  156. }
  157. public void OnDumpExampleButtonClick ()
  158. {
  159. //
  160. // dump example
  161. //
  162. // 8U, channels=1, 3x3
  163. Mat mat1 = new Mat (3, 3, CvType.CV_8UC1, new Scalar (1));
  164. // 8U, channels=4, 3x3
  165. Mat mat2 = new Mat (3, 3, CvType.CV_8UC4, new Scalar (1, 2, 3, 4));
  166. // dump
  167. Debug.Log ("mat1=" + mat1);
  168. Debug.Log ("mat1.dump()=" + mat1.dump ());
  169. Debug.Log ("mat1=" + mat2);
  170. Debug.Log ("mat2.dump()=" + mat2.dump ());
  171. exampleCodeText.text = @"
  172. //
  173. // dump example
  174. //
  175. // 8U, channels=1, 3x3
  176. Mat mat1 = new Mat (3, 3, CvType.CV_8UC1, new Scalar(1));
  177. // 8U, channels=4, 3x3
  178. Mat mat2 = new Mat (3, 3, CvType.CV_8UC4, new Scalar(1, 2, 3, 4));
  179. // dump
  180. Debug.Log (""mat1="" + mat1);
  181. Debug.Log (""mat1.dump()="" + mat1.dump());
  182. Debug.Log (""mat1="" + mat2);
  183. Debug.Log (""mat2.dump()="" + mat2.dump());
  184. ";
  185. executionResultText.text = "mat1=" + mat1 + "\n";
  186. executionResultText.text += "mat1.dump()=" + mat1.dump () + "\n";
  187. executionResultText.text += "mat2=" + mat2 + "\n";
  188. executionResultText.text += "mat2.dump()=" + mat2.dump () + "\n";
  189. UpdateScrollRect ();
  190. }
  191. public void OnCVExceptionHandlingExampleButtonClick ()
  192. {
  193. //
  194. // CVException handling example
  195. //
  196. // 32F, channels=1, 3x3
  197. Mat m1 = new Mat (3, 3, CvType.CV_32FC1);
  198. m1.put (0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);
  199. // 8U, channels=1, 3x3
  200. Mat m2 = new Mat (3, 3, CvType.CV_8UC1);
  201. m2.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  202. // dump
  203. Debug.Log ("m1=" + m1);
  204. Debug.Log ("m1.dump()=" + m1.dump ());
  205. Debug.Log ("m2=" + m2);
  206. Debug.Log ("m2.dump()=" + m2.dump ());
  207. executionResultText.text = "m1=" + m1 + "\n";
  208. executionResultText.text += "m1.dump()=" + m1.dump () + "\n";
  209. executionResultText.text += "m2=" + m2 + "\n";
  210. executionResultText.text += "m2.dump()=" + m2.dump () + "\n";
  211. // CVException handling
  212. // Publish CVException to Debug.LogError.
  213. Utils.setDebugMode (true, false);
  214. Mat m3 = m1 / m2; // element type is different.
  215. Debug.Log ("m3=" + m3);
  216. Utils.setDebugMode (false);
  217. // Throw CVException.
  218. Utils.setDebugMode (true, true);
  219. try {
  220. Mat m4 = m1 / m2; // element type is different.
  221. Debug.Log ("m4=" + m4);
  222. } catch (Exception e) {
  223. Debug.Log ("CVException: " + e);
  224. executionResultText.text += "CVException: " + e + "\n";
  225. }
  226. Utils.setDebugMode (false);
  227. exampleCodeText.text = @"
  228. //
  229. // CVException handling example
  230. //
  231. // 32F, channels=1, 3x3
  232. Mat m1 = new Mat (3, 3, CvType.CV_32FC1);
  233. m1.put (0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);
  234. // 8U, channels=1, 3x3
  235. Mat m2 = new Mat (3, 3, CvType.CV_8UC1);
  236. m2.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  237. // dump
  238. Debug.Log (""m1="" + m1);
  239. Debug.Log (""m1.dump()="" + m1.dump ());
  240. Debug.Log (""m2="" + m2);
  241. Debug.Log (""m2.dump()="" + m2.dump ());
  242. #if UNITY_STANDALONE || UNITY_EDITOR
  243. // Publish CVException to Debug.LogError.
  244. Utils.setDebugMode (true, false);
  245. Mat m3 = m1 / m2;
  246. Debug.Log(""m3="" + m3);
  247. Utils.setDebugMode (false);
  248. // Throw CVException.
  249. Utils.setDebugMode (true, true);
  250. try
  251. {
  252. Mat m4 = m1 / m2;
  253. Debug.Log(""m4="" + m4);
  254. }
  255. catch (Exception e)
  256. {
  257. Debug.Log (""CVException: "" + e);
  258. }
  259. Utils.setDebugMode (false);
  260. #else
  261. Debug.Log (""The setDebugMode method is only supported on WIN, MAC and LINUX."");
  262. #endif
  263. ";
  264. UpdateScrollRect ();
  265. }
  266. public void OnPropertyExampleButtonClick ()
  267. {
  268. //
  269. // property example
  270. //
  271. // 64F, channels=1, 3x4
  272. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  273. // number of rows
  274. Debug.Log ("rows:" + mat1.rows ());
  275. // number of columns
  276. Debug.Log ("cols:" + mat1.cols ());
  277. // number of dimensions
  278. Debug.Log ("dims:" + mat1.dims ());
  279. // size
  280. Debug.Log ("size[]:" + mat1.size ().width + ", " + mat1.size ().height);
  281. // bit depth ID
  282. Debug.Log ("depth (ID):" + mat1.depth () + "(=" + CvType.CV_64F + ")");
  283. // number of channels
  284. Debug.Log ("channels:" + mat1.channels ());
  285. // size of one element
  286. Debug.Log ("elemSize:" + mat1.elemSize () + "[byte]");
  287. // size for one channel in one element
  288. Debug.Log ("elemSize1 (elemSize/channels):" + mat1.elemSize1 () + "[byte]");
  289. // total number of elements
  290. Debug.Log ("total:" + mat1.total ());
  291. // total number of channels within one step
  292. Debug.Log ("step1 (step/elemSize1):" + mat1.step1 ());
  293. // is the data continuous?
  294. Debug.Log ("isContinuous:" + mat1.isContinuous ());
  295. // is it a submatrix?
  296. Debug.Log ("isSubmatrix:" + mat1.isSubmatrix ());
  297. // is the data empty?
  298. Debug.Log ("empty:" + mat1.empty ());
  299. exampleCodeText.text = @"
  300. //
  301. // property example
  302. //
  303. // 64F, channels=1, 3x4
  304. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  305. // number of rows
  306. Debug.Log (""rows:"" + mat1.rows());
  307. // number of columns
  308. Debug.Log (""cols:"" + mat1.cols());
  309. // number of dimensions
  310. Debug.Log (""dims:"" + mat1.dims());
  311. // size
  312. Debug.Log (""size[]:"" + mat1.size().width + "", "" + mat1.size().height);
  313. // bit depth ID
  314. Debug.Log (""depth (ID):"" + mat1.depth() + ""(="" + CvType.CV_64F + "")"");
  315. // number of channels
  316. Debug.Log (""channels:"" + mat1.channels());
  317. // size of one element
  318. Debug.Log (""elemSize:"" + mat1.elemSize() + ""[byte]"");
  319. // size for one channel in one element
  320. Debug.Log (""elemSize1 (elemSize/channels):"" + mat1.elemSize1() + ""[byte]"");
  321. // total number of elements
  322. Debug.Log (""total:"" + mat1.total());
  323. // total number of channels within one step
  324. Debug.Log (""step1 (step/elemSize1):"" + mat1.step1());
  325. // is the data continuous?
  326. Debug.Log (""isContinuous:"" + mat1.isContinuous());
  327. // is it a submatrix?
  328. Debug.Log (""isSubmatrix:"" + mat1.isSubmatrix());
  329. // is the data empty?
  330. Debug.Log (""empty:"" + mat1.empty());
  331. ";
  332. executionResultText.text = "rows:" + mat1.rows () + "\n";
  333. executionResultText.text += "cols:" + mat1.cols () + "\n";
  334. executionResultText.text += "dims:" + mat1.dims () + "\n";
  335. executionResultText.text += "size[]:" + mat1.size ().width + ", " + mat1.size ().height + "\n";
  336. executionResultText.text += "depth (ID):" + mat1.depth () + "(=" + CvType.CV_64F + ")" + "\n";
  337. executionResultText.text += "channels:" + mat1.channels () + "\n";
  338. executionResultText.text += "elemSize:" + mat1.elemSize () + "\n";
  339. executionResultText.text += "elemSize1 (elemSize/channels):" + mat1.elemSize1 () + "\n";
  340. executionResultText.text += "total:" + mat1.total () + "\n";
  341. executionResultText.text += "step1 (step/elemSize1):" + mat1.step1 () + "\n";
  342. executionResultText.text += "isContinuous:" + mat1.isContinuous () + "\n";
  343. executionResultText.text += "isSubmatrix:" + mat1.isSubmatrix () + "\n";
  344. executionResultText.text += "empty:" + mat1.empty () + "\n";
  345. UpdateScrollRect ();
  346. }
  347. public void OnFourArithmeticOperationExampleButtonClick ()
  348. {
  349. //
  350. // four arithmetic operation example
  351. //
  352. // 64F, channels=1, 3x3
  353. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  354. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
  355. Debug.Log ("m1=" + m1.dump ());
  356. executionResultText.text = "m1=" + m1.dump () + "\n";
  357. // matrix and scalar
  358. Mat m2 = m1 + new Scalar (3);
  359. Mat m3 = m1 - new Scalar (3);
  360. Mat m4 = m1 * 3; //scaling
  361. Mat m5 = m1 / 3;
  362. Debug.Log ("m1+3=" + m2.dump ());
  363. Debug.Log ("m1-3=" + m3.dump ());
  364. Debug.Log ("m1*3=" + m4.dump ());
  365. Debug.Log ("m1/3=" + m5.dump ());
  366. executionResultText.text += "m1+3=" + m2.dump () + "\n";
  367. executionResultText.text += "m1-3=" + m3.dump () + "\n";
  368. executionResultText.text += "m1*3=" + m4.dump () + "\n";
  369. executionResultText.text += "m1/3=" + m5.dump () + "\n";
  370. // matrix and matrix
  371. Mat m6 = m1 + m1;
  372. Mat m7 = m1.mul (m2);
  373. Mat m8 = m1.mul (m2, 2); //add scaling factor
  374. Debug.Log ("m1+m1=" + m6.dump ());
  375. Debug.Log ("m1.mul(m2)=" + m7.dump ());
  376. Debug.Log ("m1.mul(m2, 2)=" + m8.dump ());
  377. executionResultText.text += "m1+m1=" + m6.dump () + "\n";
  378. executionResultText.text += "m1.mul(m2)=" + m7.dump () + "\n";
  379. executionResultText.text += "m1.mul(m2, 2)=" + m8.dump () + "\n";
  380. // CVException handling
  381. // 8U, channels=1, 3x3
  382. Mat m9 = new Mat (3, 3, CvType.CV_8UC1);
  383. m9.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  384. // 64F, channels=1, 3x3
  385. Mat m10 = new Mat (2, 2, CvType.CV_64FC1);
  386. m10.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  387. // Publish CVException to Debug.LogError.
  388. Utils.setDebugMode (true, false);
  389. Mat m11 = m1 / m9; // element type is different.
  390. Debug.Log ("m1/m9=" + m11);
  391. executionResultText.text += "m1/m9=" + m11.dump () + "\n";
  392. Mat m12 = m1 / m10; // matrix size is different.
  393. Debug.Log ("m1/m10=" + m12);
  394. executionResultText.text += "m1/m10=" + m12.dump () + "\n";
  395. Utils.setDebugMode (false);
  396. exampleCodeText.text = @"
  397. //
  398. // four arithmetic operation example
  399. //
  400. // 64F, channels=1, 3x3
  401. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  402. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
  403. Debug.Log (""m1="" + m1.dump ());
  404. executionResultText.text = ""m1="" + m1.dump () + ""\n"";
  405. // matrix and scalar
  406. Mat m2 = m1 + new Scalar (3);
  407. Mat m3 = m1 - new Scalar (3);
  408. Mat m4 = m1 * 3; //scaling
  409. Mat m5 = m1 / 3;
  410. Debug.Log (""m1+3="" + m2.dump ());
  411. Debug.Log (""m1-3="" + m3.dump ());
  412. Debug.Log (""m1*3="" + m4.dump ());
  413. Debug.Log (""m1/3="" + m5.dump ());
  414. executionResultText.text += ""m1+3="" + m2.dump () + ""\n"";
  415. executionResultText.text += ""m1-3="" + m3.dump () + ""\n"";
  416. executionResultText.text += ""m1*3="" + m4.dump () + ""\n"";
  417. executionResultText.text += ""m1/3="" + m5.dump () + ""\n"";
  418. // matrix and matrix
  419. Mat m6 = m1 + m1;
  420. Mat m7 = m1.mul (m2);
  421. Mat m8 = m1.mul (m2, 2); //add scaling factor
  422. Debug.Log (""m1+m1="" + m6.dump ());
  423. Debug.Log (""m1.mul(m2)="" + m7.dump ());
  424. Debug.Log (""m1.mul(m2, 2)="" + m8.dump ());
  425. executionResultText.text += ""m1+m1="" + m6.dump () + ""\n"";
  426. executionResultText.text += ""m1.mul(m2)="" + m7.dump () + ""\n"";
  427. executionResultText.text += ""m1.mul(m2, 2)="" + m8.dump () + ""\n"";
  428. // CVException handling
  429. // 8U, channels=1, 3x3
  430. Mat m9 = new Mat (3, 3, CvType.CV_8UC1);
  431. m9.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  432. // 64F, channels=1, 3x3
  433. Mat m10 = new Mat (2, 2, CvType.CV_64FC1);
  434. m10.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  435. // Publish CVException to Debug.LogError.
  436. Utils.setDebugMode (true, false);
  437. Mat m11 = m1 / m9; // element type is different.
  438. Debug.Log (""m1/m9="" + m11);
  439. executionResultText.text += ""m1/m9="" + m11.dump () + ""\n"";
  440. Mat m12 = m1 / m10; // matrix size is different.
  441. Debug.Log (""m1/m10="" + m12);
  442. executionResultText.text += ""m1/m10="" + m12.dump () + ""\n"";
  443. Utils.setDebugMode (false);
  444. ";
  445. UpdateScrollRect ();
  446. }
  447. public void OnConvertToExampleButtonClick ()
  448. {
  449. //
  450. // convertTo example
  451. //
  452. // 64F, channels=1, 3x3
  453. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  454. m1.put (0, 0, 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3);
  455. Debug.Log ("m1=" + m1.dump ());
  456. // dst mat, type
  457. Mat m2 = new Mat ();
  458. m1.convertTo (m2, CvType.CV_8U);
  459. Debug.Log ("m2=" + m2.dump ());
  460. // dst mat, type, scale factor, added to the scaled value
  461. Mat m3 = new Mat ();
  462. m1.convertTo (m3, CvType.CV_8U, 2, 10);
  463. Debug.Log ("m3=" + m3.dump ());
  464. exampleCodeText.text = @"
  465. //
  466. // convertTo example
  467. //
  468. // 64F, channels=1, 3x3
  469. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  470. m1.put (0, 0, 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3);
  471. Debug.Log (""m1="" + m1.dump());
  472. // dst mat, type
  473. Mat m2 = new Mat ();
  474. m1.convertTo (m2, CvType.CV_8U);
  475. Debug.Log (""m2="" + m2.dump());
  476. // dst mat, type, scale factor, added to the scaled value
  477. Mat m3 = new Mat ();
  478. m1.convertTo (m3, CvType.CV_8U, 2, 10);
  479. Debug.Log (""m3="" + m3.dump());
  480. ";
  481. executionResultText.text = "m1=" + m1.dump () + "\n";
  482. executionResultText.text += "m2=" + m2.dump () + "\n";
  483. executionResultText.text += "m3=" + m3.dump () + "\n";
  484. UpdateScrollRect ();
  485. }
  486. public void OnReshapeExampleButtonClick ()
  487. {
  488. //
  489. // reshape example
  490. //
  491. // 64F, channels=1, 3x4
  492. Mat m1 = new Mat (3, 4, CvType.CV_64FC1);
  493. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  494. Debug.Log ("m1=" + m1.dump ());
  495. Debug.Log ("ch=" + m1.channels ());
  496. // channels=2, 3x2
  497. Mat m2 = m1.reshape (2);
  498. Debug.Log ("m2=" + m2.dump ());
  499. Debug.Log ("ch=" + m2.channels ());
  500. // channels=1, 2x6
  501. Mat m3 = m1.reshape (1, 2);
  502. Debug.Log ("m3=" + m3.dump ());
  503. Debug.Log ("ch=" + m3.channels ());
  504. exampleCodeText.text = @"
  505. //
  506. // reshape example
  507. //
  508. // 64F, channels=1, 3x4
  509. Mat m1 = new Mat (3, 4, CvType.CV_64FC1);
  510. m1.put (0, 0, 1,2,3,4,5,6,7,8,9,10,11,12);
  511. Debug.Log (""m1="" + m1.dump());
  512. Debug.Log (""ch="" + m1.channels());
  513. // channels=2, 3x2
  514. Mat m2 = m1.reshape (2);
  515. Debug.Log (""m2="" + m2.dump());
  516. Debug.Log (""ch="" + m2.channels());
  517. // channels=1, 2x6
  518. Mat m3 = m1.reshape (1, 2);
  519. Debug.Log (""m3="" + m3.dump());
  520. Debug.Log (""ch="" + m3.channels());
  521. ";
  522. executionResultText.text = "m1=" + m1.dump () + "\n";
  523. executionResultText.text += "ch=" + m1.channels () + "\n";
  524. executionResultText.text += "m2=" + m2.dump () + "\n";
  525. executionResultText.text += "ch=" + m2.channels () + "\n";
  526. executionResultText.text += "m3=" + m3.dump () + "\n";
  527. executionResultText.text += "ch=" + m3.channels () + "\n";
  528. UpdateScrollRect ();
  529. }
  530. public void OnRangeExampleButtonClick ()
  531. {
  532. //
  533. // range example
  534. //
  535. // 64F, channels=1, 3x3
  536. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  537. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  538. Debug.Log ("m1=" + m1.dump ());
  539. // all rows
  540. Debug.Log ("m1.rowRange(Range.all())=" + m1.rowRange (Range.all ()).dump ());
  541. // rowRange(0,2)
  542. Debug.Log ("m1.rowRange(new Range(0,2))=" + m1.rowRange (new Range (0, 2)).dump ());
  543. // row(0)
  544. Debug.Log ("m1.row(0)=" + m1.row (0).dump ());
  545. // all cols
  546. Debug.Log ("m1.colRange(Range.all())=" + m1.colRange (Range.all ()).dump ());
  547. // colRange(0,2)
  548. Debug.Log ("m1.colRange(new Range(0,2))=" + m1.colRange (new Range (0, 2)).dump ());
  549. // col(0)
  550. Debug.Log ("m1.col(0)=" + m1.col (0).dump ());
  551. exampleCodeText.text = @"
  552. //
  553. // range example
  554. //
  555. // 64F, channels=1, 3x3
  556. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  557. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  558. Debug.Log (""m1="" + m1.dump());
  559. // all rows
  560. Debug.Log (""m1.rowRange(Range.all())="" + m1.rowRange(Range.all()).dump());
  561. // rowRange(0,2)
  562. Debug.Log (""m1.rowRange(new Range(0,2))="" + m1.rowRange(new Range(0,2)).dump());
  563. // row(0)
  564. Debug.Log (""m1.row(0)="" + m1.row(0).dump());
  565. // all cols
  566. Debug.Log (""m1.colRange(Range.all())="" + m1.colRange(Range.all()).dump());
  567. // colRange(0,2)
  568. Debug.Log (""m1.colRange(new Range(0,2))="" + m1.colRange(new Range(0,2)).dump());
  569. // col(0)
  570. Debug.Log (""m1.col(0)="" + m1.col(0).dump());
  571. ";
  572. executionResultText.text = "m1=" + m1.dump () + "\n";
  573. executionResultText.text += "m1.rowRange(Range.all())=" + m1.rowRange (Range.all ()).dump () + "\n";
  574. executionResultText.text += "m1.rowRange(new Range(0,2))=" + m1.rowRange (new Range (0, 2)).dump () + "\n";
  575. executionResultText.text += "m1.row(0)=" + m1.row (0).dump () + "\n";
  576. executionResultText.text += "m1.colRange(Range.all())=" + m1.colRange (Range.all ()).dump () + "\n";
  577. executionResultText.text += "m1.colRange(new Range(0,2))=" + m1.colRange (new Range (0, 2)).dump () + "\n";
  578. executionResultText.text += "m1.col(0)=" + m1.col (0).dump () + "\n";
  579. UpdateScrollRect ();
  580. }
  581. public void OnShallowCopyAndDeepCopyExampleButtonClick ()
  582. {
  583. //
  584. // shallow copy and deep copy example
  585. //
  586. // 3x3 matrix
  587. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  588. mat1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  589. // shallow copy
  590. Mat m_shallow = mat1;
  591. // deep copy (clone, copyTo)
  592. Mat m_deep1 = mat1.clone ();
  593. Mat m_deep2 = new Mat ();
  594. mat1.copyTo (m_deep2);
  595. Debug.Log ("mat1=" + mat1.dump ());
  596. Debug.Log ("m_shallow=" + m_shallow.dump ());
  597. Debug.Log ("m_deep1=" + m_deep1.dump ());
  598. Debug.Log ("m_deep2=" + m_deep2.dump ());
  599. executionResultText.text = "mat1=" + mat1.dump () + "\n";
  600. executionResultText.text += "m_shallow=" + m_shallow.dump () + "\n";
  601. executionResultText.text += "m_deep1=" + m_deep1.dump () + "\n";
  602. executionResultText.text += "m_deep2=" + m_deep2.dump () + "\n";
  603. // rewrite (0, 0) element of matrix mat1
  604. mat1.put (0, 0, 100);
  605. Debug.Log ("mat1=" + mat1.dump ());
  606. Debug.Log ("m_shallow=" + m_shallow.dump ());
  607. Debug.Log ("m_deep1=" + m_deep1.dump ());
  608. Debug.Log ("m_deep2=" + m_deep2.dump ());
  609. executionResultText.text += "mat1=" + mat1.dump () + "\n";
  610. executionResultText.text += "m_shallow=" + m_shallow.dump () + "\n";
  611. executionResultText.text += "m_deep1=" + m_deep1.dump () + "\n";
  612. executionResultText.text += "m_deep2=" + m_deep2.dump () + "\n";
  613. Debug.Log ("mat1.Equals(m_shallow)=" + mat1.Equals (m_shallow));
  614. Debug.Log ("mat1.Equals(m_deep1)=" + mat1.Equals (m_deep1));
  615. Debug.Log ("mat1.Equals(m_deep2)=" + mat1.Equals (m_deep2));
  616. executionResultText.text += "mat1.Equals(m_shallow)=" + mat1.Equals (m_shallow) + "\n";
  617. executionResultText.text += "mat1.Equals(m_deep1)=" + mat1.Equals (m_deep1) + "\n";
  618. executionResultText.text += "mat1.Equals(m_deep2)=" + mat1.Equals (m_deep2) + "\n";
  619. exampleCodeText.text = @"
  620. //
  621. // shallow copy and deep copy example
  622. //
  623. // 3x3 matrix
  624. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  625. mat1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  626. // shallow copy
  627. Mat m_shallow = mat1;
  628. // deep copy (clone, copyTo)
  629. Mat m_deep1 = mat1.clone();
  630. Mat m_deep2 = new Mat();
  631. mat1.copyTo (m_deep2);
  632. Debug.Log (""mat1="" + mat1.dump());
  633. Debug.Log (""m_shallow="" + m_shallow.dump());
  634. Debug.Log (""m_deep1="" + m_deep1.dump());
  635. Debug.Log (""m_deep2="" + m_deep2.dump());
  636. // rewrite (0, 0) element of matrix mat1
  637. mat1.put(0, 0, 100);
  638. Debug.Log (""mat1="" + mat1.dump());
  639. Debug.Log (""m_shallow="" + m_shallow.dump());
  640. Debug.Log (""m_deep1="" + m_deep1.dump());
  641. Debug.Log (""m_deep2="" + m_deep2.dump());
  642. Debug.Log (""mat1.Equals(m_shallow)="" + mat1.Equals(m_shallow));
  643. Debug.Log (""mat1.Equals(m_deep1)="" + mat1.Equals(m_deep1));
  644. Debug.Log (""mat1.Equals(m_deep2)="" + mat1.Equals(m_deep2));
  645. ";
  646. UpdateScrollRect ();
  647. }
  648. public void OnMergeExampleButtonClick ()
  649. {
  650. //
  651. // simple composition: Merge example
  652. //
  653. // 2x2 matrix
  654. Mat m1 = new Mat (2, 2, CvType.CV_64FC1);
  655. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  656. Mat m2 = new Mat (2, 2, CvType.CV_64FC1);
  657. m2.put (0, 0, 1.1, 2.1, 3.1, 4.1);
  658. Mat m3 = new Mat (2, 2, CvType.CV_64FC1);
  659. m3.put (0, 0, 1.2, 2.2, 3.2, 4.2);
  660. List<Mat> mv = new List<Mat> ();
  661. mv.Add (m1);
  662. mv.Add (m2);
  663. mv.Add (m3);
  664. // merge
  665. Mat m_merged = new Mat ();
  666. Core.merge (mv, m_merged);
  667. // dump
  668. Debug.Log ("m_merged=" + m_merged.dump ());
  669. exampleCodeText.text = @"
  670. //
  671. // simple composition: Merge example
  672. //
  673. // 2x2 matrix
  674. Mat m1 = new Mat (2, 2, CvType.CV_64FC1);
  675. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  676. Mat m2 = new Mat (2, 2, CvType.CV_64FC1);
  677. m2.put (0, 0, 1.1, 2.1, 3.1, 4.1);
  678. Mat m3 = new Mat (2, 2, CvType.CV_64FC1);
  679. m3.put (0, 0, 1.2, 2.2, 3.2, 4.2);
  680. List<Mat> mv = new List<Mat>();
  681. mv.Add (m1);
  682. mv.Add (m2);
  683. mv.Add (m3);
  684. // merge
  685. Mat m_merged = new Mat();
  686. Core.merge (mv, m_merged);
  687. // dump
  688. Debug.Log (""m_merged="" + m_merged.dump());
  689. ";
  690. executionResultText.text = "m_merged=" + m_merged.dump () + "\n";
  691. UpdateScrollRect ();
  692. }
  693. public void OnMixChannelsExampleButtonClick ()
  694. {
  695. //
  696. // complex composition: mixChannels example
  697. //
  698. // 2x2 matrix
  699. Mat m1 = new Mat (2, 2, CvType.CV_64FC1);
  700. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  701. Mat m2 = new Mat (2, 2, CvType.CV_64FC1);
  702. m2.put (0, 0, 1.1, 2.1, 3.1, 4.1);
  703. Mat m3 = new Mat (2, 2, CvType.CV_64FC1);
  704. m3.put (0, 0, 1.2, 2.2, 3.2, 4.2);
  705. List<Mat> mv = new List<Mat> ();
  706. mv.Add (m1);
  707. mv.Add (m2);
  708. mv.Add (m3);
  709. // mat for output must be allocated.
  710. Mat m_mixed1 = new Mat (2, 2, CvType.CV_64FC2);
  711. Mat m_mixed2 = new Mat (2, 2, CvType.CV_64FC2);
  712. MatOfInt fromTo = new MatOfInt (0, 0, 1, 1, 1, 3, 2, 2);
  713. List<Mat> mixv = new List<Mat> ();
  714. mixv.Add (m_mixed1);
  715. mixv.Add (m_mixed2);
  716. // mix
  717. Core.mixChannels (mv, mixv, fromTo);
  718. // dump
  719. Debug.Log ("m_mixed1=" + m_mixed1.dump ());
  720. Debug.Log ("m_mixed2=" + m_mixed2.dump ());
  721. exampleCodeText.text = @"
  722. //
  723. // complex composition: mixChannels example
  724. //
  725. // 2x2 matrix
  726. Mat m1 = new Mat (2, 2, CvType.CV_64FC1);
  727. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  728. Mat m2 = new Mat (2, 2, CvType.CV_64FC1);
  729. m2.put (0, 0, 1.1, 2.1, 3.1, 4.1);
  730. Mat m3 = new Mat (2, 2, CvType.CV_64FC1);
  731. m3.put (0, 0, 1.2, 2.2, 3.2, 4.2);
  732. List<Mat> mv = new List<Mat>();
  733. mv.Add (m1);
  734. mv.Add (m2);
  735. mv.Add (m3);
  736. // mat for output must be allocated.
  737. Mat m_mixed1 = new Mat(2, 2, CvType.CV_64FC2);
  738. Mat m_mixed2 = new Mat(2, 2, CvType.CV_64FC2);
  739. MatOfInt fromTo = new MatOfInt (0,0, 1,1, 1,3, 2,2);
  740. List<Mat> mixv = new List<Mat> ();
  741. mixv.Add (m_mixed1);
  742. mixv.Add (m_mixed2);
  743. // mix
  744. Core.mixChannels (mv, mixv, fromTo);
  745. // dump
  746. Debug.Log (""m_mixed1="" + m_mixed1.dump());
  747. Debug.Log (""m_mixed2="" + m_mixed2.dump());
  748. ";
  749. executionResultText.text = "m_mixed1=" + m_mixed1.dump () + "\n";
  750. executionResultText.text += "m_mixed2=" + m_mixed2.dump () + "\n";
  751. UpdateScrollRect ();
  752. }
  753. public void OnSplitExampleButtonClick ()
  754. {
  755. //
  756. // split example
  757. //
  758. // channels=3, 2x3 matrix
  759. Mat m1 = new Mat (2, 3, CvType.CV_64FC3);
  760. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18);
  761. List<Mat> planes = new List<Mat> ();
  762. // split
  763. Core.split (m1, planes);
  764. // dump
  765. foreach (Mat item in planes) {
  766. Debug.Log (item.dump ());
  767. }
  768. exampleCodeText.text = @"
  769. //
  770. // split example
  771. //
  772. // channels=3, 2x3 matrix
  773. Mat m1 = new Mat (2, 3, CvType.CV_64FC3);
  774. m1.put (0, 0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18);
  775. List<Mat> planes = new List<Mat>();
  776. // split
  777. Core.split (m1, planes);
  778. // dump
  779. foreach (Mat item in planes) {
  780. Debug.Log (item.dump());
  781. }
  782. ";
  783. executionResultText.text = "";
  784. foreach (Mat item in planes) {
  785. executionResultText.text += item.dump () + "\n";
  786. }
  787. UpdateScrollRect ();
  788. }
  789. public void OnReduceExampleButtonClick ()
  790. {
  791. //
  792. // reduce example
  793. //
  794. // 3x3 matrix
  795. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  796. m1.put (0, 0, 1, 5, 3, 4, 2, 6, 7, 8, 9);
  797. Mat v1 = new Mat ();
  798. Mat v2 = new Mat ();
  799. Mat v3 = new Mat ();
  800. Mat v4 = new Mat ();
  801. // reduce 3 x 3 matrix to one row
  802. Core.reduce (m1, v1, 0, Core.REDUCE_SUM); // total value of each column
  803. Core.reduce (m1, v2, 0, Core.REDUCE_AVG); // total average value of each column
  804. Core.reduce (m1, v3, 0, Core.REDUCE_MIN); // minimum value of each column
  805. Core.reduce (m1, v4, 0, Core.REDUCE_MAX); // maximum value of each column
  806. // dump
  807. Debug.Log ("m1=" + m1.dump ());
  808. Debug.Log ("v1(sum)=" + v1.dump ());
  809. Debug.Log ("v2(avg)=" + v2.dump ());
  810. Debug.Log ("v3(min)=" + v3.dump ());
  811. Debug.Log ("v4(max)=" + v4.dump ());
  812. executionResultText.text = "m1=" + m1.dump () + "\n";
  813. executionResultText.text += "v1(sum)=" + v1.dump () + "\n";
  814. executionResultText.text += "v2(avg)=" + v2.dump () + "\n";
  815. executionResultText.text += "v3(min)=" + v3.dump () + "\n";
  816. executionResultText.text += "v4(max)=" + v4.dump () + "\n";
  817. // reduce 3 x 3 matrix to one col
  818. Core.reduce (m1, v1, 1, Core.REDUCE_SUM); // total value of each row
  819. Core.reduce (m1, v2, 1, Core.REDUCE_AVG); // total average value of row
  820. Core.reduce (m1, v3, 1, Core.REDUCE_MIN); // minimum value of each row
  821. Core.reduce (m1, v4, 1, Core.REDUCE_MAX); // maximum value of each row
  822. // dump
  823. Debug.Log ("m1=" + m1.dump ());
  824. Debug.Log ("v1(sum)=" + v1.dump ());
  825. Debug.Log ("v2(avg)=" + v2.dump ());
  826. Debug.Log ("v3(min)=" + v3.dump ());
  827. Debug.Log ("v4(max)=" + v4.dump ());
  828. executionResultText.text += "m1=" + m1.dump () + "\n";
  829. executionResultText.text += "v1(sum)=" + v1.dump () + "\n";
  830. executionResultText.text += "v2(avg)=" + v2.dump () + "\n";
  831. executionResultText.text += "v3(min)=" + v3.dump () + "\n";
  832. executionResultText.text += "v4(max)=" + v4.dump () + "\n";
  833. exampleCodeText.text = @"
  834. //
  835. // reduce example
  836. //
  837. // 3x3 matrix
  838. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  839. m1.put (0, 0, 1, 5, 3, 4, 2, 6, 7, 8, 9);
  840. Mat v1 = new Mat ();
  841. Mat v2 = new Mat ();
  842. Mat v3 = new Mat ();
  843. Mat v4 = new Mat ();
  844. // reduce 3 x 3 matrix to one row
  845. Core.reduce (m1, v1, 0, Core.REDUCE_SUM); // total value of each column
  846. Core.reduce (m1, v2, 0, Core.REDUCE_AVG); // total average value of each column
  847. Core.reduce (m1, v3, 0, Core.REDUCE_MIN); // minimum value of each column
  848. Core.reduce (m1, v4, 0, Core.REDUCE_MAX); // maximum value of each column
  849. // dump
  850. Debug.Log (""m1="" + m1.dump());
  851. Debug.Log (""v1(sum)="" + v1.dump());
  852. Debug.Log (""v2(avg)="" + v2.dump());
  853. Debug.Log (""v3(min)="" + v3.dump());
  854. Debug.Log (""v4(max)="" + v4.dump());
  855. // reduce 3 x 3 matrix to one col
  856. Core.reduce (m1, v1, 1, Core.REDUCE_SUM); // total value of each row
  857. Core.reduce (m1, v2, 1, Core.REDUCE_AVG); // total average value of row
  858. Core.reduce (m1, v3, 1, Core.REDUCE_MIN); // minimum value of each row
  859. Core.reduce (m1, v4, 1, Core.REDUCE_MAX); // maximum value of each row
  860. // dump
  861. Debug.Log (""m1="" + m1.dump());
  862. Debug.Log (""v1(sum)="" + v1.dump());
  863. Debug.Log (""v2(avg)="" + v2.dump());
  864. Debug.Log (""v3(min)="" + v3.dump());
  865. Debug.Log (""v4(max)="" + v4.dump());
  866. ";
  867. UpdateScrollRect ();
  868. }
  869. public void OnSubmatrixExampleButtonClick ()
  870. {
  871. //
  872. // submatrix (ROI) example
  873. //
  874. // 3x3 matrix
  875. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  876. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  877. Debug.Log ("m1=" + m1.dump ());
  878. executionResultText.text = "m1=" + m1.dump () + "\n";
  879. // get submatrix (ROI) of range (row[0_2] col[0_2])
  880. Mat m2 = new Mat (m1, new OpenCVForUnity.CoreModule.Rect (0, 0, 2, 2));
  881. Debug.Log ("m2=" + m2.dump ());
  882. executionResultText.text += "m2=" + m2.dump () + "\n";
  883. Debug.Log ("m2.submat()=" + m2.submat (0, 2, 0, 2).dump ());
  884. executionResultText.text += "m2.submat()=" + m2.submat (0, 2, 0, 2).dump () + "\n";
  885. // find the parent matrix size of the submatrix (ROI) m2 and its position in it
  886. Size wholeSize = new Size ();
  887. Point ofs = new Point ();
  888. m2.locateROI (wholeSize, ofs);
  889. Debug.Log ("wholeSize:" + wholeSize.width + "x" + wholeSize.height);
  890. Debug.Log ("offset:" + ofs.x + ", " + ofs.y);
  891. executionResultText.text += "wholeSize:" + wholeSize.width + "x" + wholeSize.height + "\n";
  892. executionResultText.text += "offset:" + ofs.x + ", " + ofs.y + "\n";
  893. // expand the range of submatrix (ROI)
  894. m2.adjustROI (0, 1, 0, 1);
  895. Debug.Log ("rows=" + m2.rows () + ", " + "cols=" + m2.cols ());
  896. Debug.Log ("m2=" + m2.dump ());
  897. executionResultText.text += "rows=" + m2.rows () + ", " + "cols=" + m2.cols () + "\n";
  898. executionResultText.text += "m2=" + m2.dump () + "\n";
  899. exampleCodeText.text = @"
  900. //
  901. // submatrix (ROI) example
  902. //
  903. // 3x3 matrix
  904. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  905. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  906. Debug.Log (""m1="" + m1.dump ());
  907. // get submatrix (ROI) of range (row[0_2] col[0_2])
  908. Mat m2 = new Mat (m1, new OpenCVForUnity.CoreModule.Rect(0,0,2,2));
  909. Debug.Log (""m2="" + m2.dump());
  910. Debug.Log (""m2.submat()="" + m2.submat(0,2,0,2).dump());
  911. // find the parent matrix size of the submatrix (ROI) m2 and its position in it
  912. Size wholeSize = new Size ();
  913. Point ofs = new Point ();
  914. m2.locateROI (wholeSize, ofs);
  915. Debug.Log (""wholeSize:"" + wholeSize.width + ""x"" + wholeSize.height);
  916. Debug.Log (""offset:"" + ofs.x + "", "" + ofs.y);
  917. // expand the range of submatrix (ROI)
  918. m2.adjustROI(0, 1, 0, 1);
  919. Debug.Log (""rows="" + m2.rows() + "", "" + ""cols="" + m2.cols());
  920. Debug.Log (""m2="" + m2.dump());
  921. ";
  922. UpdateScrollRect ();
  923. }
  924. public void OnRandShuffleExampleButtonClick ()
  925. {
  926. //
  927. // randShuffle example
  928. //
  929. // 4x5 matrix
  930. Mat m1 = new Mat (4, 5, CvType.CV_64FC1);
  931. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
  932. Debug.Log ("m1(original)=" + m1.dump ());
  933. executionResultText.text = "m1(original)=" + m1.dump () + "\n";
  934. // shuffle
  935. Core.randShuffle (m1, UnityEngine.Random.value);
  936. Debug.Log ("m1(shuffle)=" + m1.dump ());
  937. executionResultText.text += "m1(shuffle)=" + m1.dump () + "\n";
  938. // submatrix
  939. Mat m2 = new Mat (m1, new OpenCVForUnity.CoreModule.Rect (1, 1, 3, 2));
  940. Debug.Log ("m2(sub-matrix)=" + m2.dump ());
  941. executionResultText.text += "m2(sub-matrix)=" + m2.dump () + "\n";
  942. Core.randShuffle (m2, UnityEngine.Random.value);
  943. Debug.Log ("m2(sub-matrix)=" + m2.dump ());
  944. Debug.Log ("m1=" + m1.dump ());
  945. executionResultText.text += "m2(shuffle sub-matrix)=" + m2.dump () + "\n";
  946. executionResultText.text += "m1=" + m1.dump () + "\n";
  947. exampleCodeText.text = @"
  948. //
  949. // randShuffle example
  950. //
  951. // 4x5 matrix
  952. Mat m1 = new Mat (4, 5, CvType.CV_64FC1);
  953. m1.put (0, 0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
  954. Debug.Log (""m1(original)="" + m1.dump ());
  955. // shuffle
  956. Core.randShuffle (m1, UnityEngine.Random.value);
  957. Debug.Log (""m1(shuffle)="" + m1.dump ());
  958. // submatrix
  959. Mat m2 = new Mat (m1, new OpenCVForUnity.CoreModule.Rect(1,1,3,2));
  960. Debug.Log (""m2(sub-matrix)="" + m2.dump());
  961. Core.randShuffle (m2, UnityEngine.Random.value);
  962. Debug.Log (""m2(sub-matrix)="" + m2.dump());
  963. Debug.Log (""m1="" + m1.dump ());
  964. ";
  965. UpdateScrollRect ();
  966. }
  967. public void OnSortExampleButtonClick ()
  968. {
  969. //
  970. // sort example
  971. //
  972. // 5x5 matrix
  973. Mat m1 = new Mat (5, 5, CvType.CV_8UC1);
  974. Core.randu (m1, 0, 25);
  975. Debug.Log ("m1=" + m1.dump ());
  976. executionResultText.text = "m1=" + m1.dump () + "\n";
  977. Mat dst_mat = new Mat ();
  978. // sort ascending
  979. Core.sort (m1, dst_mat, Core.SORT_EVERY_ROW | Core.SORT_ASCENDING);
  980. Debug.Log ("ROW|ASCENDING:" + dst_mat.dump ());
  981. executionResultText.text += "ROW|ASCENDING:" + dst_mat.dump () + "\n";
  982. // sort descending
  983. Core.sort (m1, dst_mat, Core.SORT_EVERY_ROW | Core.SORT_DESCENDING);
  984. Debug.Log ("ROW|DESCENDING:" + dst_mat.dump ());
  985. executionResultText.text += "ROW|DESCENDING:" + dst_mat.dump () + "\n";
  986. // sort ascending
  987. Core.sort (m1, dst_mat, Core.SORT_EVERY_COLUMN | Core.SORT_ASCENDING);
  988. Debug.Log ("COLUMN|ASCENDING:" + dst_mat.dump ());
  989. executionResultText.text += "COLUMN|ASCENDING:" + dst_mat.dump () + "\n";
  990. // sort descending
  991. Core.sort (m1, dst_mat, Core.SORT_EVERY_COLUMN | Core.SORT_DESCENDING);
  992. Debug.Log ("COLUMN|DESCENDING:" + dst_mat.dump ());
  993. executionResultText.text += "COLUMN|DESCENDING:" + dst_mat.dump () + "\n";
  994. exampleCodeText.text = @"
  995. //
  996. // sort example
  997. //
  998. // 5x5 matrix
  999. Mat m1 = new Mat (5, 5, CvType.CV_8UC1);
  1000. Core.randu (m1, 0, 25);
  1001. Debug.Log (""m1="" + m1.dump ());
  1002. executionResultText.text = ""m1="" + m1.dump() + ""\n"";
  1003. Mat dst_mat = new Mat ();
  1004. // sort ascending
  1005. Core.sort (m1, dst_mat, Core.SORT_EVERY_ROW|Core.SORT_ASCENDING);
  1006. Debug.Log (""ROW|ASCENDING:"" + dst_mat.dump ());
  1007. // sort descending
  1008. Core.sort (m1, dst_mat, Core.SORT_EVERY_ROW|Core.SORT_DESCENDING);
  1009. Debug.Log (""ROW|DESCENDING:"" + dst_mat.dump ());
  1010. // sort ascending
  1011. Core.sort (m1, dst_mat, Core.SORT_EVERY_COLUMN|Core.SORT_ASCENDING);
  1012. Debug.Log (""COLUMN|ASCENDING:"" + dst_mat.dump ());
  1013. // sort descending
  1014. Core.sort (m1, dst_mat, Core.SORT_EVERY_COLUMN|Core.SORT_DESCENDING);
  1015. Debug.Log (""COLUMN|DESCENDING:"" + dst_mat.dump ());
  1016. ";
  1017. UpdateScrollRect ();
  1018. }
  1019. public void OnComparisonExampleButtonClick ()
  1020. {
  1021. //
  1022. // comparison example
  1023. //
  1024. // 3x3 matrix
  1025. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1026. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1027. Mat m2 = new Mat (3, 3, CvType.CV_64FC1);
  1028. m2.put (0, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1);
  1029. Debug.Log ("m1=" + m1.dump ());
  1030. Debug.Log ("m2=" + m2.dump ());
  1031. executionResultText.text = "m1=" + m1.dump () + "\n";
  1032. executionResultText.text += "m2=" + m2.dump () + "\n";
  1033. Mat dst_mat = new Mat ();
  1034. // GT (M1 > M2)
  1035. Core.compare (m1, m2, dst_mat, Core.CMP_GT);
  1036. Debug.Log ("GT (M1 > M2)=" + dst_mat.dump ());
  1037. executionResultText.text += "GT (M1 > M2)=" + dst_mat.dump () + "\n";
  1038. // GE (M1 >= M2)
  1039. Core.compare (m1, m2, dst_mat, Core.CMP_GE);
  1040. Debug.Log ("GE (M1 >= M2)=" + dst_mat.dump ());
  1041. executionResultText.text += "GE (M1 >= M2)=" + dst_mat.dump () + "\n";
  1042. // EQ (M1 == M2)
  1043. Core.compare (m1, m2, dst_mat, Core.CMP_EQ);
  1044. Debug.Log ("EQ (M1 == M2)=" + dst_mat.dump ());
  1045. executionResultText.text += "EQ (M1 == M2)=" + dst_mat.dump () + "\n";
  1046. // NE (M1 != M2)
  1047. Core.compare (m1, m2, dst_mat, Core.CMP_NE);
  1048. Debug.Log ("NE (M1 != M2)=" + dst_mat.dump ());
  1049. executionResultText.text += "NE (M1 != M2)=" + dst_mat.dump () + "\n";
  1050. // LE (M1 <= M2)
  1051. Core.compare (m1, m2, dst_mat, Core.CMP_LE);
  1052. Debug.Log ("LE (M1 <= M2)=" + dst_mat.dump ());
  1053. executionResultText.text += "LE (M1 <= M2)=" + dst_mat.dump () + "\n";
  1054. // LT (M1 < M2)
  1055. Core.compare (m1, m2, dst_mat, Core.CMP_LT);
  1056. Debug.Log ("LT (M1 < M2)=" + dst_mat.dump ());
  1057. executionResultText.text += "LT (M1 < M2)=" + dst_mat.dump () + "\n";
  1058. exampleCodeText.text = @"
  1059. //
  1060. // comparison example
  1061. //
  1062. // 3x3 matrix
  1063. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1064. m1.put (0, 0, 1,2,3,4,5,6,7,8,9);
  1065. Mat m2 = new Mat (3, 3, CvType.CV_64FC1);
  1066. m2.put (0, 0, 10,11,12,13,14,15,16,17,18);
  1067. Debug.Log (""m1="" + m1.dump ());
  1068. Debug.Log (""m2="" + m2.dump ());
  1069. Mat dst_mat = new Mat ();
  1070. // GT (M1 > M2)
  1071. Core.compare (m1, m2, dst_mat, Core.CMP_GT);
  1072. Debug.Log (""GT (M1 > M2)="" + dst_mat.dump ());
  1073. // GE (M1 >= M2)
  1074. Core.compare (m1, m2, dst_mat, Core.CMP_GE);
  1075. Debug.Log (""GE (M1 >= M2)="" + dst_mat.dump ());
  1076. // EQ (M1 == M2)
  1077. Core.compare (m1, m2, dst_mat, Core.CMP_EQ);
  1078. Debug.Log (""EQ (M1 == M2)="" + dst_mat.dump ());
  1079. // NE (M1 != M2)
  1080. Core.compare (m1, m2, dst_mat, Core.CMP_NE);
  1081. Debug.Log (""NE (M1 != M2)="" + dst_mat.dump ());
  1082. // LE (M1 <= M2)
  1083. Core.compare (m1, m2, dst_mat, Core.CMP_LE);
  1084. Debug.Log (""LE (M1 <= M2)="" + dst_mat.dump ());
  1085. // LT (M1 < M2)
  1086. Core.compare (m1, m2, dst_mat, Core.CMP_LT);
  1087. Debug.Log (""LT (M1 < M2)="" + dst_mat.dump ());
  1088. ";
  1089. UpdateScrollRect ();
  1090. }
  1091. public void OnOperatorsExampleButtonClick ()
  1092. {
  1093. //
  1094. // operators example
  1095. //
  1096. // 3x3 matrix
  1097. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1098. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1099. Mat m2 = new Mat (3, 3, CvType.CV_64FC1);
  1100. m2.put (0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18);
  1101. Scalar s = new Scalar (5);
  1102. double alpha = 3;
  1103. Debug.Log ("m1=" + m1.dump ());
  1104. Debug.Log ("m2=" + m2.dump ());
  1105. Debug.Log ("s=" + s);
  1106. Debug.Log ("alpha=" + alpha);
  1107. executionResultText.text = "m1=" + m1.dump () + "\n";
  1108. executionResultText.text += "m2=" + m2.dump () + "\n";
  1109. executionResultText.text += "s=" + s + "\n";
  1110. executionResultText.text += "alpha=" + alpha + "\n";
  1111. // Addition, subtraction, negation: A+B, A-B, A+s, A-s, s+A, s-A, -A
  1112. // (M1 + M2 = Core.add (M1, M2, M_dst))
  1113. Debug.Log ("m1+m2=" + (m1 + m2).dump ());
  1114. executionResultText.text += "m1+m2=" + (m1 + m2).dump () + "\n";
  1115. // (M1 + s = Core.add (M1, s, M_dst))
  1116. Debug.Log ("m1+s=" + (m1 + s).dump ());
  1117. executionResultText.text += "m1+s=" + (m1 + s).dump () + "\n";
  1118. // (M1 – M2 = Core.subtract (M1, M2, M_dst))
  1119. Debug.Log ("m1-m2=" + (m1 - m2).dump ());
  1120. executionResultText.text += "m1-m2=" + (m1 - m2).dump () + "\n";
  1121. // (M1 – s = Core.subtract (M1, s, M_dst))
  1122. Debug.Log ("m1-s=" + (m1 - s).dump ());
  1123. executionResultText.text += "m1-s=" + (m1 - s).dump () + "\n";
  1124. // (-M1 = Core.multiply (M1, Scalar.all (-1), M_dst))
  1125. Debug.Log ("-m1=" + (-m1).dump ());
  1126. executionResultText.text += "-m1=" + (-m1).dump () + "\n";
  1127. // Scaling: A*alpha A/alpha
  1128. // (M1 * 3 = Core.multiply (M1, Scalar.all (3), M_dst))
  1129. Debug.Log ("m1*alpha=" + (m1 * alpha).dump ());
  1130. executionResultText.text += "m1*alpha=" + (m1 * alpha).dump () + "\n";
  1131. // (M1 / 3 = Core.divide (M1, Scalar.all (3), M_dst))
  1132. Debug.Log ("m1/alpha=" + (m1 / alpha).dump ());
  1133. executionResultText.text += "m1/alpha=" + (m1 / alpha).dump () + "\n";
  1134. // Per-element multiplication and division: A.mul(B), A/B, alpha/A
  1135. // (M1.mul(M2) = M1.mul (M2))
  1136. Debug.Log ("m1.mul(m2)=" + (m1.mul (m2)).dump ());
  1137. executionResultText.text += "m1.mul(m2)=" + (m1.mul (m2)).dump () + "\n";
  1138. // (M1 / M2 = Core.divide (M1, M2, M_dst))
  1139. Debug.Log ("m1/m2=" + (m1 / m2).dump ());
  1140. executionResultText.text += "m1/m2=" + (m1 / m2).dump () + "\n";
  1141. // (3 / M1 = Core.divide (new Mat (M1.size (), M1.type (), Scalar.all (3)), M1, M_dst))
  1142. Debug.Log ("alpha/m2=" + (alpha / m2).dump ());
  1143. executionResultText.text += "alpha/m2=" + (alpha / m2).dump () + "\n";
  1144. // Matrix multiplication: A*B
  1145. // (M1 * M2 = Core.gemm (M1, M2, 1, new Mat (), 0, M_dst))
  1146. Debug.Log ("m1*m2=" + (m1 * m2).dump ());
  1147. executionResultText.text += "m1*m2=" + (m1 * m2).dump () + "\n";
  1148. // Bitwise logical operations: A logicop B, A logicop s, s logicop A, ~A, where logicop is one of : &, |, ^.
  1149. // (M1 & M2 = Core.bitwise_and (M1, M2, M_dst))
  1150. Debug.Log ("m1&m2=" + (m1 & m2).dump ());
  1151. executionResultText.text += "m1&m2=" + (m1 & m2).dump () + "\n";
  1152. // (M1 | M2 = Core.bitwise_or (M1, M2, M_dst))
  1153. Debug.Log ("m1|m2=" + (m1 | m2).dump ());
  1154. executionResultText.text += "m1|m2=" + (m1 | m2).dump () + "\n";
  1155. // (M1 ^ M2 = Core.bitwise_xor (M1, M2, M_dst))
  1156. Debug.Log ("m1^m2=" + (m1 ^ m2).dump ());
  1157. executionResultText.text += "m1^m2=" + (m1 ^ m2).dump () + "\n";
  1158. // (~M1 = Core.bitwise_not (M1, M_dst))
  1159. Debug.Log ("~m1=" + (~m1).dump ());
  1160. executionResultText.text += "~m1=" + (~m1).dump () + "\n";
  1161. // Note.
  1162. // The assignment operator behavior is different from OpenCV (c ++).
  1163. // For example, C = A + B will not be expanded to cv :: add (A, B, C).
  1164. // Also cannot assign a scalar to Mat like C = s.
  1165. exampleCodeText.text = @"
  1166. //
  1167. // operators example
  1168. //
  1169. // 3x3 matrix
  1170. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1171. m1.put (0, 0, 1,2,3,4,5,6,7,8,9);
  1172. Mat m2 = new Mat (3, 3, CvType.CV_64FC1);
  1173. m2.put (0, 0, 10,11,12,13,14,15,16,17,18);
  1174. Scalar s = new Scalar (5);
  1175. double alpha = 3;
  1176. Debug.Log (""m1="" + m1.dump ());
  1177. Debug.Log (""m2="" + m2.dump ());
  1178. Debug.Log (""s="" + s);
  1179. Debug.Log (""alpha="" + alpha);
  1180. // Addition, subtraction, negation: A+B, A-B, A+s, A-s, s+A, s-A, -A
  1181. // (M1 + M2 = Core.add (M1, M2, M_dst))
  1182. Debug.Log (""m1+m2="" + (m1 + m2).dump());
  1183. // (M1 + s = Core.add (M1, s, M_dst))
  1184. Debug.Log (""m1+s="" + (m1 + s).dump());
  1185. // (M1 – M2 = Core.subtract (M1, M2, M_dst))
  1186. Debug.Log (""m1-m2="" + (m1 - m2).dump());
  1187. // (M1 – s = Core.subtract (M1, s, M_dst))
  1188. Debug.Log (""m1-s="" + (m1 - s).dump());
  1189. // (-M1 = Core.multiply (M1, Scalar.all (-1), M_dst))
  1190. Debug.Log (""-m1="" + (-m1).dump());
  1191. // Scaling: A*alpha A/alpha
  1192. // (M1 * 3 = Core.multiply (M1, Scalar.all (3), M_dst))
  1193. Debug.Log (""m1*alpha="" + (m1*alpha).dump());
  1194. // (M1 / 3 = Core.divide (M1, Scalar.all (3), M_dst))
  1195. Debug.Log (""m1/alpha="" + (m1/alpha).dump());
  1196. // Per-element multiplication and division: A.mul(B), A/B, alpha/A
  1197. // (M1.mul(M2) = M1.mul (M2))
  1198. Debug.Log (""m1.mul(m2)="" + (m1.mul(m2)).dump());
  1199. // (M1 / M2 = Core.divide (M1, M2, M_dst))
  1200. Debug.Log (""m1/m2="" + (m1 / m2).dump());
  1201. // (3 / M1 = Core.divide (new Mat (M1.size (), M1.type (), Scalar.all (3)), M1, M_dst))
  1202. Debug.Log (""alpha/m2="" + (alpha / m2).dump());
  1203. // Matrix multiplication: A*B
  1204. // (M1 * M2 = Core.gemm (M1, M2, 1, new Mat (), 0, M_dst))
  1205. Debug.Log (""m1*m2="" + (m1 * m2).dump());
  1206. // Bitwise logical operations: A logicop B, A logicop s, s logicop A, ~A, where logicop is one of : &, |, ^.
  1207. // (M1 & M2 = Core.bitwise_and (M1, M2, M_dst))
  1208. Debug.Log (""m1&m2="" + (m1 & m2).dump());
  1209. // (M1 | M2 = Core.bitwise_or (M1, M2, M_dst))
  1210. Debug.Log (""m1|m2="" + (m1 | m2).dump());
  1211. // (M1 ^ M2 = Core.bitwise_xor (M1, M2, M_dst))
  1212. Debug.Log (""m1^m2="" + (m1 ^ m2).dump());
  1213. // (~M1 = Core.bitwise_not (M1, M_dst))
  1214. Debug.Log (""~m1="" + (~m1).dump());
  1215. // Note.
  1216. // The assignment operator behavior is different from OpenCV (c ++).
  1217. // For example, C = A + B will not be expanded to cv :: add (A, B, C).
  1218. // Also cannot assign a scalar to Mat like C = s.
  1219. ";
  1220. UpdateScrollRect ();
  1221. }
  1222. public void OnGetAndPutExampleButtonClick ()
  1223. {
  1224. //
  1225. // get and put example
  1226. //
  1227. // channels=4 3x3 matrix
  1228. Mat m1 = new Mat (3, 3, CvType.CV_8UC4, new Scalar (1, 2, 3, 4));
  1229. Debug.Log ("m1=" + m1.dump ());
  1230. executionResultText.text = "m1=" + m1.dump () + "\n";
  1231. // get an element value.
  1232. double[] m1_1_1 = m1.get (1, 1);
  1233. Debug.Log ("m1[1,1]=" + m1_1_1 [0] + ", " + m1_1_1 [1] + ", " + m1_1_1 [2] + ", " + m1_1_1 [3]);
  1234. executionResultText.text += "m1[1,1]=" + m1_1_1 [0] + ", " + m1_1_1 [1] + ", " + m1_1_1 [2] + ", " + m1_1_1 [3] + "\n";
  1235. // get an array of all element values.
  1236. byte[] m1_array = new byte[m1.total () * m1.channels ()];
  1237. m1.get (0, 0, m1_array);
  1238. string dump_str = "";
  1239. foreach (var i in m1_array) {
  1240. dump_str += i + ", ";
  1241. }
  1242. Debug.Log ("m1_array=" + dump_str);
  1243. executionResultText.text += "m1_array=" + dump_str + "\n";
  1244. // another faster way. (use Utils.copyFromMat())
  1245. Utils.copyFromMat (m1, m1_array);
  1246. dump_str = "";
  1247. foreach (var i in m1_array) {
  1248. dump_str += i + ", ";
  1249. }
  1250. Debug.Log ("m1_array (use Utils.copyFromMat())=" + dump_str);
  1251. executionResultText.text += "m1_array (use Utils.copyFromMat())=" + dump_str + "\n";
  1252. // put an element value in a matrix.
  1253. Mat m2 = m1.clone ();
  1254. m2.put (1, 1, 5, 6, 7, 8);
  1255. Debug.Log ("m2=" + m2.dump ());
  1256. executionResultText.text += "m2=" + m2.dump () + "\n";
  1257. // put an array of element values in a matrix.
  1258. byte[] m2_arr = new byte[] {
  1259. 5,
  1260. 6,
  1261. 7,
  1262. 8,
  1263. 5,
  1264. 6,
  1265. 7,
  1266. 8,
  1267. 5,
  1268. 6,
  1269. 7,
  1270. 8,
  1271. 5,
  1272. 6,
  1273. 7,
  1274. 8,
  1275. 5,
  1276. 6,
  1277. 7,
  1278. 8,
  1279. 5,
  1280. 6,
  1281. 7,
  1282. 8,
  1283. 5,
  1284. 6,
  1285. 7,
  1286. 8,
  1287. 5,
  1288. 6,
  1289. 7,
  1290. 8,
  1291. 5,
  1292. 6,
  1293. 7,
  1294. 8
  1295. };
  1296. m2.put (0, 0, m2_arr);
  1297. Debug.Log ("m2=" + m2.dump ());
  1298. executionResultText.text += "m2=" + m2.dump () + "\n";
  1299. // another faster way. (use Utils.copyToMat())
  1300. Utils.copyToMat (m2_arr, m2);
  1301. Debug.Log ("m2 (use Utils.copyToMat())=" + m2.dump ());
  1302. executionResultText.text += "m2 (use Utils.copyToMat())=" + m2.dump () + "\n";
  1303. // fill element values (setTo method)
  1304. m2.setTo (new Scalar (100, 100, 100, 100));
  1305. Debug.Log ("m2=" + m2.dump ());
  1306. executionResultText.text += "m2=" + m2.dump () + "\n";
  1307. exampleCodeText.text = @"
  1308. //
  1309. // get and put example
  1310. //
  1311. // channels=4 3x3 matrix
  1312. Mat m1 = new Mat (3, 3, CvType.CV_8UC4 , new Scalar(1,2,3,4));
  1313. Debug.Log (""m1="" + m1.dump ());
  1314. // get an element value.
  1315. double[] m1_1_1 = m1.get(1,1);
  1316. Debug.Log (""m1[1,1]="" + m1_1_1[0] + "", "" + m1_1_1[1] + "", "" + m1_1_1[2] + "", "" + m1_1_1[3]);
  1317. // get an array of all element values.
  1318. byte[] m1_array = new byte[m1.total () * m1.channels()];
  1319. m1.get (0, 0, m1_array);
  1320. string dump_str = """";
  1321. foreach (var i in m1_array){
  1322. dump_str += i + "", "";
  1323. }
  1324. Debug.Log (""m1_array="" + dump_str);
  1325. // another faster way. (use Utils.copyFromMat())
  1326. Utils.copyFromMat (m1, m1_array);
  1327. dump_str = """";
  1328. foreach (var i in m1_array) {
  1329. dump_str += i + "", "";
  1330. }
  1331. Debug.Log (""m1_array (use Utils.copyFromMat())="" + dump_str);
  1332. // put an element value in a matrix.
  1333. Mat m2 = m1.clone ();
  1334. m2.put (1, 1, 5,6,7,8);
  1335. Debug.Log (""m2="" + m2.dump ());
  1336. // put an array of element values in a matrix.
  1337. byte[] m2_arr = new byte[]{5,6,7,8,5,6,7,8,5,6,7,8,5,6,7,8,5,6,7,8,5,6,7,8,5,6,7,8,5,6,7,8,5,6,7,8};
  1338. m2.put (0, 0, m2_arr);
  1339. Debug.Log (""m2="" + m2.dump ());
  1340. // another faster way. (use Utils.copyToMat())
  1341. Utils.copyToMat (m2_arr, m2);
  1342. Debug.Log (""m2 (use Utils.copyToMat())="" + m2.dump ());
  1343. // fill element values (setTo method)
  1344. m2.setTo(new Scalar(100,100,100,100));
  1345. Debug.Log (""m2="" + m2.dump ());
  1346. ";
  1347. UpdateScrollRect ();
  1348. }
  1349. public void OnAccessingPixelValueExampleButtonClick ()
  1350. {
  1351. //
  1352. // accessing pixel value example
  1353. //
  1354. // How access pixel value in an OpenCV Mat.
  1355. // channels=4 10x10 matrix (RGBA color image)
  1356. Mat src_img = new Mat (10, 10, CvType.CV_8UC4, new Scalar (0, 0, 0, 255));
  1357. Mat dst_img = new Mat (10, 10, CvType.CV_8UC4);
  1358. Debug.Log ("src_img=" + src_img.dump ());
  1359. executionResultText.text = "src_img=" + src_img.dump () + "\n";
  1360. // Copies an OpenCV Mat data to a pixel data Array.
  1361. byte[] img_array = new byte[src_img.total () * src_img.channels ()];
  1362. Utils.copyFromMat (src_img, img_array);
  1363. int pixel_i = 0;
  1364. int channels = src_img.channels ();
  1365. int total = (int)src_img.total ();
  1366. for (int i = 0; i < total; i++) {
  1367. img_array [pixel_i] = (byte)i;
  1368. img_array [pixel_i + 1] = (byte)i;
  1369. img_array [pixel_i + 2] = (byte)i;
  1370. // Advance to next pixel
  1371. pixel_i += channels;
  1372. }
  1373. // Copies a pixel data Array to an OpenCV Mat data.
  1374. Utils.copyToMat (img_array, dst_img);
  1375. Debug.Log ("dst_img=" + dst_img.dump ());
  1376. executionResultText.text += "dst_img=" + dst_img.dump () + "\n";
  1377. exampleCodeText.text = @"
  1378. //
  1379. // accessing pixel value example
  1380. //
  1381. // How access pixel value in an OpenCV Mat.
  1382. // channels=4 10x10 matrix (RGBA color image)
  1383. Mat src_img = new Mat (10, 10, CvType.CV_8UC4, new Scalar (0, 0, 0, 255));
  1384. Mat dst_img = new Mat (10, 10, CvType.CV_8UC4);
  1385. Debug.Log (""src_img="" + src_img.dump ());
  1386. // Copies an OpenCV Mat data to a pixel data Array.
  1387. byte[] img_array = new byte[src_img.total () * src_img.channels()];
  1388. Utils.copyFromMat (src_img, img_array);
  1389. int pixel_i = 0;
  1390. int channels = src_img.channels();
  1391. int total = (int)src_img.total();
  1392. for (int i = 0; i < total; i++) {
  1393. img_array [pixel_i] = (byte)i;
  1394. img_array [pixel_i + 1] = (byte)i;
  1395. img_array [pixel_i + 2] = (byte)i;
  1396. // Advance to next pixel
  1397. pixel_i += channels;
  1398. }
  1399. // Copies a pixel data Array to an OpenCV Mat data.
  1400. Utils.copyToMat (img_array, dst_img);
  1401. Debug.Log (""dst_img="" + dst_img.dump ());
  1402. ";
  1403. UpdateScrollRect ();
  1404. }
  1405. }
  1406. }