MatBasicProcessingExample.cs 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331
  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. using OpenCVForUnity.UtilsModule;
  10. using Range = OpenCVForUnity.CoreModule.Range;
  11. namespace OpenCVForUnityExample
  12. {
  13. /// <summary>
  14. /// Mat Basic Processing Example
  15. /// </summary>
  16. public class MatBasicProcessingExample : MonoBehaviour
  17. {
  18. public ScrollRect exampleCodeScrollRect;
  19. public UnityEngine.UI.Text exampleCodeText;
  20. public ScrollRect executionResultScrollRect;
  21. public UnityEngine.UI.Text executionResultText;
  22. // Use this for initialization
  23. IEnumerator Start()
  24. {
  25. // fix the screen orientation.
  26. Screen.orientation = ScreenOrientation.LandscapeLeft;
  27. // wait for the screen orientation to change.
  28. yield return null;
  29. }
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. }
  34. /// <summary>
  35. /// Raises the destroy event.
  36. /// </summary>
  37. void OnDestroy()
  38. {
  39. Screen.orientation = ScreenOrientation.AutoRotation;
  40. }
  41. private void UpdateScrollRect()
  42. {
  43. exampleCodeScrollRect.verticalNormalizedPosition = executionResultScrollRect.verticalNormalizedPosition = 1f;
  44. }
  45. public void OnBackButtonClick()
  46. {
  47. SceneManager.LoadScene("OpenCVForUnityExample");
  48. }
  49. public void OnInitializationExampleButtonClick()
  50. {
  51. //
  52. // initialization example
  53. //
  54. // 3x3 matrix (set array value)
  55. Mat mat1 = new Mat(3, 3, CvType.CV_64FC1);
  56. mat1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  57. Debug.Log("mat1=" + mat1.dump());
  58. // 2x2 rotation matrix
  59. double angle = 30, a = Math.Cos(angle * Math.PI / 180), b = Math.Sin(angle * Math.PI / 180);
  60. Mat mat2 = new Mat(2, 2, CvType.CV_64FC1);
  61. mat2.put(0, 0, a, -b, b, a);
  62. Debug.Log("mat2=" + mat2.dump());
  63. // 5x5 all 1's matrix
  64. Mat mat3 = Mat.ones(5, 5, CvType.CV_64FC1);
  65. Debug.Log("mat3=" + mat3.dump());
  66. // 5x5 all zero's matrix
  67. Mat mat4 = Mat.zeros(5, 5, CvType.CV_64FC1);
  68. Debug.Log("mat4=" + mat4.dump());
  69. // 5x5 identity matrix
  70. Mat mat5 = Mat.eye(5, 5, CvType.CV_64FC1);
  71. Debug.Log("mat5=" + mat5.dump());
  72. // 3x3 initialize with a constant
  73. Mat mat6 = new Mat(3, 3, CvType.CV_64FC1, new Scalar(5));
  74. Debug.Log("mat6=" + mat6.dump());
  75. // 3x2 initialize with a uniform distribution random number
  76. Mat mat7 = new Mat(3, 2, CvType.CV_8UC1);
  77. Core.randu(mat7, 0, 256);
  78. Debug.Log("mat7=" + mat7.dump());
  79. // 3x2 initialize with a normal distribution random number
  80. Mat mat8 = new Mat(3, 2, CvType.CV_8UC1);
  81. Core.randn(mat8, 128, 10);
  82. Debug.Log("mat8=" + mat8.dump());
  83. // 2x2x3x4 matrix (4 dimensional array)
  84. int[] sizes = new int[] { 2, 2, 3, 4 };
  85. Mat mat9 = new Mat(sizes, CvType.CV_8UC1, Scalar.all(0));
  86. Debug.Log("mat9.dims=" + mat9.dims());
  87. Debug.Log("mat9.rows=" + mat9.rows() + " //For Mats of 3 dimensions or more, rows == cols == -1");
  88. Debug.Log("mat9.cols=" + mat9.cols());
  89. exampleCodeText.text = @"
  90. //
  91. // initialization example
  92. //
  93. // 3x3 matrix (set array value)
  94. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  95. mat1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  96. Debug.Log (""mat1="" + mat1.dump());
  97. // 2x2 rotation matrix
  98. double angle = 30, a = Math.Cos(angle*Math.PI/180), b = Math.Sin(angle*Math.PI/180);
  99. Mat mat2 = new Mat (2, 2, CvType.CV_64FC1);
  100. mat2.put (0, 0, a, -b, b, a);
  101. Debug.Log (""mat2="" + mat2.dump());
  102. // 5x5 all 1's matrix
  103. Mat mat3 = Mat.ones(5, 5, CvType.CV_64FC1);
  104. Debug.Log (""mat3="" + mat3.dump());
  105. // 5x5 all zero's matrix
  106. Mat mat4 = Mat.zeros(5, 5, CvType.CV_64FC1);
  107. Debug.Log (""mat4="" + mat4.dump());
  108. // 5x5 identity matrix
  109. Mat mat5 = Mat.eye(5, 5, CvType.CV_64FC1);
  110. Debug.Log (""mat5="" + mat5.dump());
  111. // 3x3 initialize with a constant
  112. Mat mat6 = new Mat (3, 3, CvType.CV_64FC1, new Scalar(5));
  113. Debug.Log (""mat6="" + mat6.dump());
  114. // 3x2 initialize with a uniform distribution random number
  115. Mat mat7 = new Mat (3, 2, CvType.CV_8UC1);
  116. Core.randu (mat7, 0, 256);
  117. Debug.Log (""mat7="" + mat7.dump());
  118. // 3x2 initialize with a normal distribution random number
  119. Mat mat8 = new Mat (3, 2, CvType.CV_8UC1);
  120. Core.randn (mat8, 128, 10);
  121. Debug.Log (""mat8="" + mat8.dump());
  122. // 2x2x3x4 matrix (4 dimensional array)
  123. int[] sizes = new int[]{ 2, 2, 3, 4 };
  124. Mat mat9 = new Mat (sizes, CvType.CV_8UC1, Scalar.all (0));
  125. Debug.Log (""mat9.dims="" + mat9.dims());
  126. Debug.Log (""mat9.rows="" + mat9.rows () + "" //For Mats of 3 dimensions or more, rows == cols == -1"");
  127. Debug.Log (""mat9.cols="" + mat9.cols ());
  128. ";
  129. executionResultText.text = "mat1=" + mat1.dump() + "\n";
  130. executionResultText.text += "mat2=" + mat2.dump() + "\n";
  131. executionResultText.text += "mat3=" + mat3.dump() + "\n";
  132. executionResultText.text += "mat4=" + mat4.dump() + "\n";
  133. executionResultText.text += "mat5=" + mat5.dump() + "\n";
  134. executionResultText.text += "mat6=" + mat6.dump() + "\n";
  135. executionResultText.text += "mat7=" + mat7.dump() + "\n";
  136. executionResultText.text += "mat8=" + mat8.dump() + "\n";
  137. executionResultText.text += "mat9.dims=" + mat9.dims() + "\n";
  138. executionResultText.text += "mat9.rows=" + mat9.rows() + " //For Mats of 3 dimensions or more, rows == cols == -1" + "\n";
  139. executionResultText.text += "mat9.cols=" + mat9.cols() + "\n";
  140. UpdateScrollRect();
  141. }
  142. public void OnMultiChannelExampleButtonClick()
  143. {
  144. //
  145. // multi channel example
  146. //
  147. // 64F, channels=1, 3x3
  148. Mat mat1 = new Mat(3, 3, CvType.CV_64FC1);
  149. Debug.Log("mat1");
  150. Debug.Log(" dim:" + mat1.dims() + " elemSize1:" + mat1.elemSize1() + " channel:" + mat1.channels());
  151. // 64F, channels=10, 3x3
  152. Debug.Log("mat2");
  153. Mat mat2 = new Mat(3, 3, CvType.CV_64FC(10));
  154. Debug.Log(" dim:" + mat2.dims() + " elemSize1:" + mat2.elemSize1() + " channels:" + mat2.channels());
  155. // 64F, channles=1, 2x2x3x4 (4 dimensional array)
  156. Debug.Log("mat3");
  157. int[] sizes = new int[] { 2, 2, 3, 4 };
  158. Mat mat3 = new Mat(sizes, CvType.CV_64FC1);
  159. Debug.Log(" dim:" + mat3.dims() + " elemSize1:" + mat3.elemSize1() + " channels:" + mat3.channels());
  160. exampleCodeText.text = @"
  161. //
  162. // multi channel example
  163. //
  164. // 64F, channels=1, 3x3
  165. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  166. Debug.Log (""mat1"");
  167. Debug.Log ("" dim:"" + mat1.dims() + "" elemSize1:"" + mat1.elemSize1() + "" channel:"" + mat1.channels());
  168. // 64F, channels=10, 3x3
  169. Debug.Log (""mat2"");
  170. Mat mat2 = new Mat (3, 3, CvType.CV_64FC(10));
  171. Debug.Log ("" dim:"" + mat2.dims() + "" elemSize1:"" + mat2.elemSize1() + "" channels:"" + mat2.channels());
  172. // 64F, channles=1, 2x2x3x4 (4 dimensional array)
  173. Debug.Log (""mat3"");
  174. int[] sizes = new int[]{ 2, 2, 3, 4 };
  175. Mat mat3 = new Mat (sizes, CvType.CV_64FC1);
  176. Debug.Log ("" dim:"" + mat3.dims() + "" elemSize1:"" + mat3.elemSize1() + "" channels:"" + mat3.channels());
  177. ";
  178. executionResultText.text = "mat1" + "\n";
  179. executionResultText.text += " dim:" + mat1.dims() + " elemSize1:" + mat1.elemSize1() + " channels:" + mat1.channels() + "\n";
  180. executionResultText.text += "mat2" + "\n";
  181. executionResultText.text += " dim:" + mat2.dims() + " elemSize1:" + mat2.elemSize1() + " channels:" + mat2.channels() + "\n";
  182. executionResultText.text += "mat3" + "\n";
  183. executionResultText.text += " dim:" + mat3.dims() + " elemSize1:" + mat3.elemSize1() + " channels:" + mat3.channels() + "\n";
  184. UpdateScrollRect();
  185. }
  186. public void OnDumpExampleButtonClick()
  187. {
  188. //
  189. // dump example
  190. //
  191. // 8U, channels=1, 3x3
  192. Mat mat1 = new Mat(3, 3, CvType.CV_8UC1, new Scalar(1));
  193. // 8U, channels=4, 3x3
  194. Mat mat2 = new Mat(3, 3, CvType.CV_8UC4, new Scalar(1, 2, 3, 4));
  195. // dump
  196. Debug.Log("mat1=" + mat1);
  197. Debug.Log("mat1.dump()=" + mat1.dump());
  198. Debug.Log("mat1=" + mat2);
  199. Debug.Log("mat2.dump()=" + mat2.dump());
  200. exampleCodeText.text = @"
  201. //
  202. // dump example
  203. //
  204. // 8U, channels=1, 3x3
  205. Mat mat1 = new Mat (3, 3, CvType.CV_8UC1, new Scalar(1));
  206. // 8U, channels=4, 3x3
  207. Mat mat2 = new Mat (3, 3, CvType.CV_8UC4, new Scalar(1, 2, 3, 4));
  208. // dump
  209. Debug.Log (""mat1="" + mat1);
  210. Debug.Log (""mat1.dump()="" + mat1.dump());
  211. Debug.Log (""mat1="" + mat2);
  212. Debug.Log (""mat2.dump()="" + mat2.dump());
  213. ";
  214. executionResultText.text = "mat1=" + mat1 + "\n";
  215. executionResultText.text += "mat1.dump()=" + mat1.dump() + "\n";
  216. executionResultText.text += "mat2=" + mat2 + "\n";
  217. executionResultText.text += "mat2.dump()=" + mat2.dump() + "\n";
  218. UpdateScrollRect();
  219. }
  220. public void OnCVExceptionHandlingExampleButtonClick()
  221. {
  222. //
  223. // CVException handling example
  224. //
  225. // 32F, channels=1, 3x3
  226. Mat m1 = new Mat(3, 3, CvType.CV_32FC1);
  227. m1.put(0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);
  228. // 8U, channels=1, 3x3
  229. Mat m2 = new Mat(3, 3, CvType.CV_8UC1);
  230. m2.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  231. // dump
  232. Debug.Log("m1=" + m1);
  233. Debug.Log("m1.dump()=" + m1.dump());
  234. Debug.Log("m2=" + m2);
  235. Debug.Log("m2.dump()=" + m2.dump());
  236. executionResultText.text = "m1=" + m1 + "\n";
  237. executionResultText.text += "m1.dump()=" + m1.dump() + "\n";
  238. executionResultText.text += "m2=" + m2 + "\n";
  239. executionResultText.text += "m2.dump()=" + m2.dump() + "\n";
  240. // CVException handling
  241. // Publish CVException to Debug.LogError.
  242. Utils.setDebugMode(true, false);
  243. Mat m3 = m1 / m2; // element type is different.
  244. Debug.Log("m3=" + m3);
  245. executionResultText.text += "m3=" + m3 + "\n";
  246. Utils.setDebugMode(false);
  247. // Throw CVException.
  248. Utils.setDebugMode(true, true);
  249. try
  250. {
  251. Mat m4 = m1 / m2; // element type is different.
  252. Debug.Log("m4=" + m4);
  253. executionResultText.text += "m4=" + m4 + "\n";
  254. }
  255. catch (Exception e)
  256. {
  257. Debug.Log("CVException: " + e);
  258. executionResultText.text += "CVException: " + e + "\n";
  259. }
  260. Utils.setDebugMode(false);
  261. exampleCodeText.text = @"
  262. //
  263. // CVException handling example
  264. //
  265. // 32F, channels=1, 3x3
  266. Mat m1 = new Mat (3, 3, CvType.CV_32FC1);
  267. m1.put (0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);
  268. // 8U, channels=1, 3x3
  269. Mat m2 = new Mat (3, 3, CvType.CV_8UC1);
  270. m2.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  271. // dump
  272. Debug.Log (""m1="" + m1);
  273. Debug.Log (""m1.dump()="" + m1.dump ());
  274. Debug.Log (""m2="" + m2);
  275. Debug.Log (""m2.dump()="" + m2.dump ());
  276. // CVException handling
  277. // Publish CVException to Debug.LogError.
  278. Utils.setDebugMode (true, false);
  279. Mat m3 = m1 / m2;
  280. Debug.Log(""m3="" + m3);
  281. Utils.setDebugMode (false);
  282. // Throw CVException.
  283. Utils.setDebugMode (true, true);
  284. try
  285. {
  286. Mat m4 = m1 / m2;
  287. Debug.Log(""m4="" + m4);
  288. }
  289. catch (Exception e)
  290. {
  291. Debug.Log (""CVException: "" + e);
  292. }
  293. Utils.setDebugMode (false);
  294. ";
  295. UpdateScrollRect();
  296. }
  297. public void OnPropertyExampleButtonClick()
  298. {
  299. //
  300. // property example
  301. //
  302. // 64F, channels=1, 3x4
  303. Mat mat1 = new Mat(3, 4, CvType.CV_64FC1);
  304. // number of rows
  305. Debug.Log("rows:" + mat1.rows());
  306. // number of columns
  307. Debug.Log("cols:" + mat1.cols());
  308. // number of dimensions
  309. Debug.Log("dims:" + mat1.dims());
  310. // size
  311. Debug.Log("size[]:" + mat1.size().width + ", " + mat1.size().height);
  312. // bit depth ID
  313. Debug.Log("depth (ID):" + mat1.depth() + "(=" + CvType.CV_64F + ")");
  314. // number of channels
  315. Debug.Log("channels:" + mat1.channels());
  316. // size of one element
  317. Debug.Log("elemSize:" + mat1.elemSize() + "[byte]");
  318. // size for one channel in one element
  319. Debug.Log("elemSize1 (elemSize/channels):" + mat1.elemSize1() + "[byte]");
  320. // total number of elements
  321. Debug.Log("total:" + mat1.total());
  322. // size of step
  323. Debug.Log("step (step1*elemSize1):" + mat1.step1() * mat1.elemSize1() + "[byte]");
  324. // total number of channels within one step
  325. Debug.Log("step1 (step/elemSize1):" + mat1.step1());
  326. // is the data continuous?
  327. Debug.Log("isContinuous:" + mat1.isContinuous());
  328. // is it a submatrix?
  329. Debug.Log("isSubmatrix:" + mat1.isSubmatrix());
  330. // is the data empty?
  331. Debug.Log("empty:" + mat1.empty());
  332. Debug.Log("==============================");
  333. // 32FC, channels=5, 4x5, 3x4 Submatrix
  334. Mat mat2 = new Mat(4, 5, CvType.CV_32FC(5));
  335. OpenCVForUnity.CoreModule.Rect roi_rect = new OpenCVForUnity.CoreModule.Rect(0, 0, 3, 4);
  336. Mat r1 = new Mat(mat2, roi_rect);
  337. // number of rows
  338. Debug.Log("rows:" + r1.rows());
  339. // number of columns
  340. Debug.Log("cols:" + r1.cols());
  341. // number of dimensions
  342. Debug.Log("dims:" + r1.dims());
  343. // size
  344. Debug.Log("size[]:" + r1.size().width + ", " + r1.size().height);
  345. // bit depth ID
  346. Debug.Log("depth (ID):" + r1.depth() + "(=" + CvType.CV_32F + ")");
  347. // number of channels
  348. Debug.Log("channels:" + r1.channels());
  349. // size of one element
  350. Debug.Log("elemSize:" + r1.elemSize() + "[byte]");
  351. // size for one channel in one element
  352. Debug.Log("elemSize1 (elemSize/channels):" + r1.elemSize1() + "[byte]");
  353. // total number of elements
  354. Debug.Log("total:" + r1.total());
  355. // size of step
  356. Debug.Log("step (step1*elemSize1):" + r1.step1() * r1.elemSize1() + "[byte]");
  357. // total number of channels within one step
  358. Debug.Log("step1 (step/elemSize1):" + r1.step1());
  359. // is the data continuous?
  360. Debug.Log("isContinuous:" + r1.isContinuous());
  361. // is it a submatrix?
  362. Debug.Log("isSubmatrix:" + r1.isSubmatrix());
  363. // is the data empty?
  364. Debug.Log("empty:" + r1.empty());
  365. Debug.Log("==============================");
  366. // 32S, channles=2, 2x3x3x4x6 (5 dimensional array)
  367. int[] sizes = new int[] { 2, 3, 3, 4, 6 };
  368. Mat mat3 = new Mat(sizes, CvType.CV_32SC2);
  369. // number of rows
  370. Debug.Log("rows:" + mat3.rows());
  371. // number of columns
  372. Debug.Log("cols:" + mat3.cols());
  373. // number of dimensions
  374. Debug.Log("dims:" + mat3.dims());
  375. // size
  376. string size = "";
  377. for (int i = 0; i < mat3.dims(); ++i)
  378. {
  379. size += mat3.size(i) + ", ";
  380. }
  381. Debug.Log("size[]:" + size);
  382. // bit depth ID
  383. Debug.Log("depth (ID):" + mat3.depth() + "(=" + CvType.CV_32S + ")");
  384. // number of channels
  385. Debug.Log("channels:" + mat3.channels());
  386. // size of one element
  387. Debug.Log("elemSize:" + mat3.elemSize() + "[byte]");
  388. // size for one channel in one element
  389. Debug.Log("elemSize1 (elemSize/channels):" + mat3.elemSize1() + "[byte]");
  390. // total number of elements
  391. Debug.Log("total:" + mat3.total());
  392. // size of step
  393. string step = "";
  394. for (int i = 0; i < mat3.dims(); ++i)
  395. {
  396. step += mat3.step1(i) * mat3.elemSize1() + ", ";
  397. }
  398. Debug.Log("step (step1*elemSize1):" + step + "[byte]");
  399. // total number of channels within one step
  400. Debug.Log("step1 (step/elemSize1):" + mat3.step1());
  401. // is the data continuous?
  402. Debug.Log("isContinuous:" + mat3.isContinuous());
  403. // is it a submatrix?
  404. Debug.Log("isSubmatrix:" + mat3.isSubmatrix());
  405. // is the data empty?
  406. Debug.Log("empty:" + mat3.empty());
  407. exampleCodeText.text = @"
  408. //
  409. // property example
  410. //
  411. // 64F, channels=1, 3x4
  412. Mat mat1 = new Mat (3, 4, CvType.CV_64FC1);
  413. // number of rows
  414. Debug.Log (""rows:"" + mat1.rows ());
  415. // number of columns
  416. Debug.Log (""cols:"" + mat1.cols ());
  417. // number of dimensions
  418. Debug.Log (""dims:"" + mat1.dims ());
  419. // size
  420. Debug.Log (""size[]:"" + mat1.size ().width + "", "" + mat1.size ().height);
  421. // bit depth ID
  422. Debug.Log (""depth (ID):"" + mat1.depth () + ""(="" + CvType.CV_64F + "")"");
  423. // number of channels
  424. Debug.Log (""channels:"" + mat1.channels ());
  425. // size of one element
  426. Debug.Log (""elemSize:"" + mat1.elemSize () + ""[byte]"");
  427. // size for one channel in one element
  428. Debug.Log (""elemSize1 (elemSize/channels):"" + mat1.elemSize1 () + ""[byte]"");
  429. // total number of elements
  430. Debug.Log (""total:"" + mat1.total ());
  431. // size of step
  432. Debug.Log (""step (step1*elemSize1):"" + mat1.step1 () * mat1.elemSize1 () + ""[byte]"");
  433. // total number of channels within one step
  434. Debug.Log (""step1 (step/elemSize1):"" + mat1.step1 ());
  435. // is the data continuous?
  436. Debug.Log (""isContinuous:"" + mat1.isContinuous ());
  437. // is it a submatrix?
  438. Debug.Log (""isSubmatrix:"" + mat1.isSubmatrix ());
  439. // is the data empty?
  440. Debug.Log (""empty:"" + mat1.empty ());
  441. Debug.Log (""=============================="");
  442. // 32FC, channels=5, 4x5, 3x4 Submatrix
  443. Mat mat2 = new Mat (4, 5, CvType.CV_32FC (5));
  444. OpenCVForUnity.CoreModule.Rect roi_rect = new OpenCVForUnity.CoreModule.Rect (0, 0, 3, 4);
  445. Mat r1 = new Mat (mat2, roi_rect);
  446. // number of rows
  447. Debug.Log (""rows:"" + r1.rows ());
  448. // number of columns
  449. Debug.Log (""cols:"" + r1.cols ());
  450. // number of dimensions
  451. Debug.Log (""dims:"" + r1.dims ());
  452. // size
  453. Debug.Log (""size[]:"" + r1.size ().width + "", "" + r1.size ().height);
  454. // bit depth ID
  455. Debug.Log (""depth (ID):"" + r1.depth () + ""(="" + CvType.CV_32F + "")"");
  456. // number of channels
  457. Debug.Log (""channels:"" + r1.channels ());
  458. // size of one element
  459. Debug.Log (""elemSize:"" + r1.elemSize () + ""[byte]"");
  460. // size for one channel in one element
  461. Debug.Log (""elemSize1 (elemSize/channels):"" + r1.elemSize1 () + ""[byte]"");
  462. // total number of elements
  463. Debug.Log (""total:"" + r1.total ());
  464. // size of step
  465. Debug.Log (""step (step1*elemSize1):"" + r1.step1 () * r1.elemSize1 () + ""[byte]"");
  466. // total number of channels within one step
  467. Debug.Log (""step1 (step/elemSize1):"" + r1.step1 ());
  468. // is the data continuous?
  469. Debug.Log (""isContinuous:"" + r1.isContinuous ());
  470. // is it a submatrix?
  471. Debug.Log (""isSubmatrix:"" + r1.isSubmatrix ());
  472. // is the data empty?
  473. Debug.Log (""empty:"" + r1.empty ());
  474. Debug.Log (""=============================="");
  475. // 32S, channles=2, 2x3x3x4x6 (5 dimensional array)
  476. int[] sizes = new int[]{ 2, 3, 3, 4, 6 };
  477. Mat mat3 = new Mat (sizes, CvType.CV_32SC2);
  478. // number of rows
  479. Debug.Log (""rows:"" + mat3.rows ());
  480. // number of columns
  481. Debug.Log (""cols:"" + mat3.cols ());
  482. // number of dimensions
  483. Debug.Log (""dims:"" + mat3.dims ());
  484. // size
  485. string size = """";
  486. for (int i = 0; i < mat3.dims (); ++i) {
  487. size += mat3.size (i) + "", "";
  488. }
  489. Debug.Log (""size[]:"" + size);
  490. // bit depth ID
  491. Debug.Log (""depth (ID):"" + mat3.depth () + ""(="" + CvType.CV_32S + "")"");
  492. // number of channels
  493. Debug.Log (""channels:"" + mat3.channels ());
  494. // size of one element
  495. Debug.Log (""elemSize:"" + mat3.elemSize () + ""[byte]"");
  496. // size for one channel in one element
  497. Debug.Log (""elemSize1 (elemSize/channels):"" + mat3.elemSize1 () + ""[byte]"");
  498. // total number of elements
  499. Debug.Log (""total:"" + mat3.total ());
  500. // size of step
  501. string step = """";
  502. for (int i = 0; i < mat3.dims (); ++i) {
  503. step += mat3.step1 (i) * mat3.elemSize1 () + "", "";
  504. }
  505. Debug.Log (""step (step1*elemSize1):"" + step + ""[byte]"");
  506. // total number of channels within one step
  507. Debug.Log (""step1 (step/elemSize1):"" + mat3.step1 ());
  508. // is the data continuous?
  509. Debug.Log (""isContinuous:"" + mat3.isContinuous ());
  510. // is it a submatrix?
  511. Debug.Log (""isSubmatrix:"" + mat3.isSubmatrix ());
  512. // is the data empty?
  513. Debug.Log (""empty:"" + mat3.empty ());
  514. ";
  515. executionResultText.text = "rows:" + mat1.rows() + "\n";
  516. executionResultText.text += "cols:" + mat1.cols() + "\n";
  517. executionResultText.text += "dims:" + mat1.dims() + "\n";
  518. executionResultText.text += "size[]:" + mat1.size().width + ", " + mat1.size().height + "\n";
  519. executionResultText.text += "depth (ID):" + mat1.depth() + "(=" + CvType.CV_64F + ")" + "\n";
  520. executionResultText.text += "channels:" + mat1.channels() + "\n";
  521. executionResultText.text += "elemSize:" + mat1.elemSize() + "[byte]" + "\n";
  522. executionResultText.text += "elemSize1 (elemSize/channels):" + mat1.elemSize1() + "[byte]" + "\n";
  523. executionResultText.text += "total:" + mat1.total() + "\n";
  524. executionResultText.text += "step (step1*elemSize1):" + mat1.step1() * mat1.elemSize1() + "[byte]" + "\n";
  525. executionResultText.text += "step1 (step/elemSize1):" + mat1.step1() + "\n";
  526. executionResultText.text += "isContinuous:" + mat1.isContinuous() + "\n";
  527. executionResultText.text += "isSubmatrix:" + mat1.isSubmatrix() + "\n";
  528. executionResultText.text += "empty:" + mat1.empty() + "\n";
  529. executionResultText.text += "==============================" + "\n";
  530. executionResultText.text += "rows:" + r1.rows() + "\n";
  531. executionResultText.text += "cols:" + r1.cols() + "\n";
  532. executionResultText.text += "dims:" + r1.dims() + "\n";
  533. executionResultText.text += "size[]:" + r1.size().width + ", " + r1.size().height + "\n";
  534. executionResultText.text += "depth (ID):" + r1.depth() + "(=" + CvType.CV_32F + ")" + "\n";
  535. executionResultText.text += "channels:" + r1.channels() + "\n";
  536. executionResultText.text += "elemSize:" + r1.elemSize() + "[byte]" + "\n";
  537. executionResultText.text += "elemSize1 (elemSize/channels):" + r1.elemSize1() + "[byte]" + "\n";
  538. executionResultText.text += "total:" + r1.total() + "\n";
  539. executionResultText.text += "step (step1*elemSize1):" + r1.step1() * r1.elemSize1() + "[byte]" + "\n";
  540. executionResultText.text += "step1 (step/elemSize1):" + r1.step1() + "\n";
  541. executionResultText.text += "isContinuous:" + r1.isContinuous() + "\n";
  542. executionResultText.text += "isSubmatrix:" + r1.isSubmatrix() + "\n";
  543. executionResultText.text += "empty:" + r1.empty() + "\n";
  544. executionResultText.text += "==============================" + "\n";
  545. executionResultText.text += "rows:" + mat3.rows() + "\n";
  546. executionResultText.text += "cols:" + mat3.cols() + "\n";
  547. executionResultText.text += "dims:" + mat3.dims() + "\n";
  548. executionResultText.text += "size[]:" + size + "\n";
  549. executionResultText.text += "depth (ID):" + mat3.depth() + "(=" + CvType.CV_32S + ")" + "\n";
  550. executionResultText.text += "channels:" + mat3.channels() + "\n";
  551. executionResultText.text += "elemSize:" + mat3.elemSize() + "[byte]" + "\n";
  552. executionResultText.text += "elemSize1 (elemSize/channels):" + mat3.elemSize1() + "[byte]" + "\n";
  553. executionResultText.text += "total:" + mat3.total() + "\n";
  554. executionResultText.text += "step (step1*elemSize1):" + step + "[byte]" + "\n";
  555. executionResultText.text += "step1 (step/elemSize1):" + mat3.step1() + "\n";
  556. executionResultText.text += "isContinuous:" + mat3.isContinuous() + "\n";
  557. executionResultText.text += "isSubmatrix:" + mat3.isSubmatrix() + "\n";
  558. executionResultText.text += "empty:" + mat3.empty() + "\n";
  559. UpdateScrollRect();
  560. }
  561. public void OnFourArithmeticOperationExampleButtonClick()
  562. {
  563. //
  564. // four arithmetic operation example
  565. //
  566. // 64F, channels=1, 3x3
  567. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  568. m1.put(0, 0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
  569. Debug.Log("m1=" + m1.dump());
  570. executionResultText.text = "m1=" + m1.dump() + "\n";
  571. // matrix and scalar
  572. Mat m2 = m1 + new Scalar(3);
  573. Mat m3 = m1 - new Scalar(3);
  574. Mat m4 = m1 * 3; //scaling
  575. Mat m5 = m1 / 3;
  576. Debug.Log("m1+3=" + m2.dump());
  577. Debug.Log("m1-3=" + m3.dump());
  578. Debug.Log("m1*3=" + m4.dump());
  579. Debug.Log("m1/3=" + m5.dump());
  580. executionResultText.text += "m1+3=" + m2.dump() + "\n";
  581. executionResultText.text += "m1-3=" + m3.dump() + "\n";
  582. executionResultText.text += "m1*3=" + m4.dump() + "\n";
  583. executionResultText.text += "m1/3=" + m5.dump() + "\n";
  584. // matrix and matrix
  585. Mat m6 = m1 + m1;
  586. Mat m7 = m1.mul(m2);
  587. Mat m8 = m1.mul(m2, 2); //add scaling factor
  588. Debug.Log("m1+m1=" + m6.dump());
  589. Debug.Log("m1.mul(m2)=" + m7.dump());
  590. Debug.Log("m1.mul(m2, 2)=" + m8.dump());
  591. executionResultText.text += "m1+m1=" + m6.dump() + "\n";
  592. executionResultText.text += "m1.mul(m2)=" + m7.dump() + "\n";
  593. executionResultText.text += "m1.mul(m2, 2)=" + m8.dump() + "\n";
  594. // CVException handling
  595. // 8U, channels=1, 3x3
  596. Mat m9 = new Mat(3, 3, CvType.CV_8UC1);
  597. m9.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  598. // 64F, channels=1, 3x3
  599. Mat m10 = new Mat(2, 2, CvType.CV_64FC1);
  600. m10.put(0, 0, 1.0, 2.0, 3.0, 4.0);
  601. // Publish CVException to Debug.LogError.
  602. Utils.setDebugMode(true, false);
  603. Mat m11 = m1 / m9; // element type is different.
  604. Debug.Log("m1/m9=" + m11);
  605. executionResultText.text += "m1/m9=" + m11 + "\n";
  606. Mat m12 = m1 / m10; // matrix size is different.
  607. Debug.Log("m1/m10=" + m12);
  608. executionResultText.text += "m1/m10=" + m12 + "\n";
  609. Utils.setDebugMode(false);
  610. exampleCodeText.text = @"
  611. //
  612. // four arithmetic operation example
  613. //
  614. // 64F, channels=1, 3x3
  615. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  616. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
  617. Debug.Log (""m1="" + m1.dump ());
  618. executionResultText.text = ""m1="" + m1.dump () + ""\n"";
  619. // matrix and scalar
  620. Mat m2 = m1 + new Scalar (3);
  621. Mat m3 = m1 - new Scalar (3);
  622. Mat m4 = m1 * 3; //scaling
  623. Mat m5 = m1 / 3;
  624. Debug.Log (""m1+3="" + m2.dump ());
  625. Debug.Log (""m1-3="" + m3.dump ());
  626. Debug.Log (""m1*3="" + m4.dump ());
  627. Debug.Log (""m1/3="" + m5.dump ());
  628. executionResultText.text += ""m1+3="" + m2.dump () + ""\n"";
  629. executionResultText.text += ""m1-3="" + m3.dump () + ""\n"";
  630. executionResultText.text += ""m1*3="" + m4.dump () + ""\n"";
  631. executionResultText.text += ""m1/3="" + m5.dump () + ""\n"";
  632. // matrix and matrix
  633. Mat m6 = m1 + m1;
  634. Mat m7 = m1.mul (m2);
  635. Mat m8 = m1.mul (m2, 2); //add scaling factor
  636. Debug.Log (""m1+m1="" + m6.dump ());
  637. Debug.Log (""m1.mul(m2)="" + m7.dump ());
  638. Debug.Log (""m1.mul(m2, 2)="" + m8.dump ());
  639. executionResultText.text += ""m1+m1="" + m6.dump () + ""\n"";
  640. executionResultText.text += ""m1.mul(m2)="" + m7.dump () + ""\n"";
  641. executionResultText.text += ""m1.mul(m2, 2)="" + m8.dump () + ""\n"";
  642. // CVException handling
  643. // 8U, channels=1, 3x3
  644. Mat m9 = new Mat (3, 3, CvType.CV_8UC1);
  645. m9.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  646. // 64F, channels=1, 3x3
  647. Mat m10 = new Mat (2, 2, CvType.CV_64FC1);
  648. m10.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  649. // Publish CVException to Debug.LogError.
  650. Utils.setDebugMode (true, false);
  651. Mat m11 = m1 / m9; // element type is different.
  652. Debug.Log (""m1/m9="" + m11);
  653. executionResultText.text += ""m1/m9="" + m11 + ""\n"";
  654. Mat m12 = m1 / m10; // matrix size is different.
  655. Debug.Log (""m1/m10="" + m12);
  656. executionResultText.text += ""m1/m10="" + m12 + ""\n"";
  657. Utils.setDebugMode (false);
  658. ";
  659. UpdateScrollRect();
  660. }
  661. public void OnConvertToExampleButtonClick()
  662. {
  663. //
  664. // convertTo example
  665. //
  666. // 64F, channels=1, 3x3
  667. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  668. m1.put(0, 0, 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3);
  669. Debug.Log("m1=" + m1.dump());
  670. // dst mat, type
  671. Mat m2 = new Mat();
  672. m1.convertTo(m2, CvType.CV_8U);
  673. Debug.Log("m2=" + m2.dump());
  674. // dst mat, type, scale factor, added to the scaled value
  675. Mat m3 = new Mat();
  676. m1.convertTo(m3, CvType.CV_8U, 2, 10);
  677. Debug.Log("m3=" + m3.dump());
  678. exampleCodeText.text = @"
  679. //
  680. // convertTo example
  681. //
  682. // 64F, channels=1, 3x3
  683. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  684. m1.put (0, 0, 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3);
  685. Debug.Log (""m1="" + m1.dump());
  686. // dst mat, type
  687. Mat m2 = new Mat ();
  688. m1.convertTo (m2, CvType.CV_8U);
  689. Debug.Log (""m2="" + m2.dump());
  690. // dst mat, type, scale factor, added to the scaled value
  691. Mat m3 = new Mat ();
  692. m1.convertTo (m3, CvType.CV_8U, 2, 10);
  693. Debug.Log (""m3="" + m3.dump());
  694. ";
  695. executionResultText.text = "m1=" + m1.dump() + "\n";
  696. executionResultText.text += "m2=" + m2.dump() + "\n";
  697. executionResultText.text += "m3=" + m3.dump() + "\n";
  698. UpdateScrollRect();
  699. }
  700. public void OnReshapeExampleButtonClick()
  701. {
  702. //
  703. // reshape example
  704. //
  705. // 64F, channels=1, 3x4
  706. Mat m1 = new Mat(3, 4, CvType.CV_64FC1);
  707. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  708. Debug.Log("m1=" + m1.dump());
  709. Debug.Log("ch=" + m1.channels());
  710. // channels=1, 3x4 -> channels=2, 3x2
  711. Mat m2 = m1.reshape(2);
  712. Debug.Log("m2=" + m2.dump());
  713. Debug.Log("ch=" + m2.channels());
  714. // channels=1, 3x4 -> channels=1, 2x6
  715. Mat m3 = m1.reshape(1, 2);
  716. Debug.Log("m3=" + m3.dump());
  717. Debug.Log("ch=" + m3.channels());
  718. // 2D -> 4D
  719. Mat src = new Mat(6, 5, CvType.CV_8UC3, new Scalar(0));
  720. Mat m4 = src.reshape(1, new int[] { 1, src.channels() * src.cols(), 1, src.rows() });
  721. Debug.Log("m4.dims=" + m4.dims());
  722. string size = "";
  723. for (int i = 0; i < m4.dims(); ++i)
  724. {
  725. size += m4.size(i) + ", ";
  726. }
  727. Debug.Log("size[]=" + size);
  728. Debug.Log("ch=" + m4.channels());
  729. // 3D -> 2D
  730. src = new Mat(new int[] { 4, 6, 7 }, CvType.CV_8UC3, new Scalar(0));
  731. Mat m5 = src.reshape(1, new int[] { src.channels() * src.size(2), src.size(0) * src.size(1) });
  732. Debug.Log("m5=" + m5);
  733. Debug.Log("ch=" + m5.channels());
  734. exampleCodeText.text = @"
  735. //
  736. // reshape example
  737. //
  738. // 64F, channels=1, 3x4
  739. Mat m1 = new Mat (3, 4, CvType.CV_64FC1);
  740. m1.put (0, 0, 1,2,3,4,5,6,7,8,9,10,11,12);
  741. Debug.Log (""m1="" + m1.dump());
  742. Debug.Log (""ch="" + m1.channels());
  743. // channels=1, 3x4 -> channels=2, 3x2
  744. Mat m2 = m1.reshape (2);
  745. Debug.Log (""m2="" + m2.dump ());
  746. Debug.Log (""ch="" + m2.channels ());
  747. // channels=1, 3x4 -> channels=1, 2x6
  748. Mat m3 = m1.reshape (1, 2);
  749. Debug.Log (""m3="" + m3.dump ());
  750. Debug.Log (""ch="" + m3.channels ());
  751. // 2D -> 4D
  752. Mat src = new Mat (6, 5, CvType.CV_8UC3, new Scalar (0));
  753. Mat m4 = src.reshape (1, new int[]{ 1, src.channels () * src.cols (), 1, src.rows () });
  754. Debug.Log (""m4.dims="" + m4.dims ());
  755. string size = """";
  756. for (int i = 0; i < m4.dims (); ++i) {
  757. size += m4.size (i) + "", "";
  758. }
  759. Debug.Log (""size[]="" + size);
  760. Debug.Log (""ch="" + m4.channels ());
  761. // 3D -> 2D
  762. src = new Mat (new int[]{ 4, 6, 7 }, CvType.CV_8UC3, new Scalar (0));
  763. Mat m5 = src.reshape (1, new int[]{ src.channels () * src.size (2), src.size (0) * src.size (1) });
  764. Debug.Log (""m5="" + m5);
  765. Debug.Log (""ch="" + m5.channels ());
  766. ";
  767. executionResultText.text = "m1=" + m1.dump() + "\n";
  768. executionResultText.text += "ch=" + m1.channels() + "\n";
  769. executionResultText.text += "m2=" + m2.dump() + "\n";
  770. executionResultText.text += "ch=" + m2.channels() + "\n";
  771. executionResultText.text += "m3=" + m3.dump() + "\n";
  772. executionResultText.text += "ch=" + m3.channels() + "\n";
  773. executionResultText.text += "m4.dims=" + m4.dims() + "\n";
  774. executionResultText.text += "m4.size[]=" + size + "\n";
  775. executionResultText.text += "ch=" + m4.channels() + "\n";
  776. executionResultText.text += "m5=" + m5 + "\n";
  777. executionResultText.text += "ch=" + m5.channels() + "\n";
  778. UpdateScrollRect();
  779. }
  780. public void OnRangeExampleButtonClick()
  781. {
  782. //
  783. // range example
  784. //
  785. // 64F, channels=1, 3x3
  786. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  787. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  788. Debug.Log("m1=" + m1.dump());
  789. // all rows
  790. Debug.Log("m1.rowRange(Range.all())=" + m1.rowRange(Range.all()).dump());
  791. // rowRange(0,2)
  792. Debug.Log("m1.rowRange(new Range(0,2))=" + m1.rowRange(new Range(0, 2)).dump());
  793. // row(0)
  794. Debug.Log("m1.row(0)=" + m1.row(0).dump());
  795. // all cols
  796. Debug.Log("m1.colRange(Range.all())=" + m1.colRange(Range.all()).dump());
  797. // colRange(0,2)
  798. Debug.Log("m1.colRange(new Range(0,2))=" + m1.colRange(new Range(0, 2)).dump());
  799. // col(0)
  800. Debug.Log("m1.col(0)=" + m1.col(0).dump());
  801. exampleCodeText.text = @"
  802. //
  803. // range example
  804. //
  805. // 64F, channels=1, 3x3
  806. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  807. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  808. Debug.Log (""m1="" + m1.dump());
  809. // all rows
  810. Debug.Log (""m1.rowRange(Range.all())="" + m1.rowRange(Range.all()).dump());
  811. // rowRange(0,2)
  812. Debug.Log (""m1.rowRange(new Range(0,2))="" + m1.rowRange(new Range(0,2)).dump());
  813. // row(0)
  814. Debug.Log (""m1.row(0)="" + m1.row(0).dump());
  815. // all cols
  816. Debug.Log (""m1.colRange(Range.all())="" + m1.colRange(Range.all()).dump());
  817. // colRange(0,2)
  818. Debug.Log (""m1.colRange(new Range(0,2))="" + m1.colRange(new Range(0,2)).dump());
  819. // col(0)
  820. Debug.Log (""m1.col(0)="" + m1.col(0).dump());
  821. ";
  822. executionResultText.text = "m1=" + m1.dump() + "\n";
  823. executionResultText.text += "m1.rowRange(Range.all())=" + m1.rowRange(Range.all()).dump() + "\n";
  824. executionResultText.text += "m1.rowRange(new Range(0,2))=" + m1.rowRange(new Range(0, 2)).dump() + "\n";
  825. executionResultText.text += "m1.row(0)=" + m1.row(0).dump() + "\n";
  826. executionResultText.text += "m1.colRange(Range.all())=" + m1.colRange(Range.all()).dump() + "\n";
  827. executionResultText.text += "m1.colRange(new Range(0,2))=" + m1.colRange(new Range(0, 2)).dump() + "\n";
  828. executionResultText.text += "m1.col(0)=" + m1.col(0).dump() + "\n";
  829. UpdateScrollRect();
  830. }
  831. public void OnShallowCopyAndDeepCopyExampleButtonClick()
  832. {
  833. //
  834. // shallow copy and deep copy example
  835. //
  836. // 3x3 matrix
  837. Mat mat1 = new Mat(3, 3, CvType.CV_64FC1);
  838. mat1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  839. // shallow copy
  840. Mat m_shallow = mat1;
  841. // deep copy (clone, copyTo)
  842. Mat m_deep1 = mat1.clone();
  843. Mat m_deep2 = new Mat();
  844. mat1.copyTo(m_deep2);
  845. Debug.Log("mat1=" + mat1.dump());
  846. Debug.Log("m_shallow=" + m_shallow.dump());
  847. Debug.Log("m_deep1=" + m_deep1.dump());
  848. Debug.Log("m_deep2=" + m_deep2.dump());
  849. executionResultText.text = "mat1=" + mat1.dump() + "\n";
  850. executionResultText.text += "m_shallow=" + m_shallow.dump() + "\n";
  851. executionResultText.text += "m_deep1=" + m_deep1.dump() + "\n";
  852. executionResultText.text += "m_deep2=" + m_deep2.dump() + "\n";
  853. // rewrite (0, 0) element of matrix mat1
  854. mat1.put(0, 0, 100);
  855. Debug.Log("mat1=" + mat1.dump());
  856. Debug.Log("m_shallow=" + m_shallow.dump());
  857. Debug.Log("m_deep1=" + m_deep1.dump());
  858. Debug.Log("m_deep2=" + m_deep2.dump());
  859. executionResultText.text += "mat1=" + mat1.dump() + "\n";
  860. executionResultText.text += "m_shallow=" + m_shallow.dump() + "\n";
  861. executionResultText.text += "m_deep1=" + m_deep1.dump() + "\n";
  862. executionResultText.text += "m_deep2=" + m_deep2.dump() + "\n";
  863. Debug.Log("mat1.Equals(m_shallow)=" + mat1.Equals(m_shallow));
  864. Debug.Log("mat1.Equals(m_deep1)=" + mat1.Equals(m_deep1));
  865. Debug.Log("mat1.Equals(m_deep2)=" + mat1.Equals(m_deep2));
  866. executionResultText.text += "mat1.Equals(m_shallow)=" + mat1.Equals(m_shallow) + "\n";
  867. executionResultText.text += "mat1.Equals(m_deep1)=" + mat1.Equals(m_deep1) + "\n";
  868. executionResultText.text += "mat1.Equals(m_deep2)=" + mat1.Equals(m_deep2) + "\n";
  869. exampleCodeText.text = @"
  870. //
  871. // shallow copy and deep copy example
  872. //
  873. // 3x3 matrix
  874. Mat mat1 = new Mat (3, 3, CvType.CV_64FC1);
  875. mat1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  876. // shallow copy
  877. Mat m_shallow = mat1;
  878. // deep copy (clone, copyTo)
  879. Mat m_deep1 = mat1.clone();
  880. Mat m_deep2 = new Mat();
  881. mat1.copyTo (m_deep2);
  882. Debug.Log (""mat1="" + mat1.dump());
  883. Debug.Log (""m_shallow="" + m_shallow.dump());
  884. Debug.Log (""m_deep1="" + m_deep1.dump());
  885. Debug.Log (""m_deep2="" + m_deep2.dump());
  886. // rewrite (0, 0) element of matrix mat1
  887. mat1.put(0, 0, 100);
  888. Debug.Log (""mat1="" + mat1.dump());
  889. Debug.Log (""m_shallow="" + m_shallow.dump());
  890. Debug.Log (""m_deep1="" + m_deep1.dump());
  891. Debug.Log (""m_deep2="" + m_deep2.dump());
  892. Debug.Log (""mat1.Equals(m_shallow)="" + mat1.Equals(m_shallow));
  893. Debug.Log (""mat1.Equals(m_deep1)="" + mat1.Equals(m_deep1));
  894. Debug.Log (""mat1.Equals(m_deep2)="" + mat1.Equals(m_deep2));
  895. ";
  896. UpdateScrollRect();
  897. }
  898. public void OnMergeExampleButtonClick()
  899. {
  900. //
  901. // simple composition: Merge example
  902. //
  903. // 2x2 matrix
  904. Mat m1 = new Mat(2, 2, CvType.CV_64FC1);
  905. m1.put(0, 0, 1.0, 2.0, 3.0, 4.0);
  906. Mat m2 = new Mat(2, 2, CvType.CV_64FC1);
  907. m2.put(0, 0, 1.1, 2.1, 3.1, 4.1);
  908. Mat m3 = new Mat(2, 2, CvType.CV_64FC1);
  909. m3.put(0, 0, 1.2, 2.2, 3.2, 4.2);
  910. List<Mat> mv = new List<Mat>();
  911. mv.Add(m1);
  912. mv.Add(m2);
  913. mv.Add(m3);
  914. // merge
  915. Mat m_merged = new Mat();
  916. Core.merge(mv, m_merged);
  917. // dump
  918. Debug.Log("m_merged=" + m_merged.dump());
  919. exampleCodeText.text = @"
  920. //
  921. // simple composition: Merge example
  922. //
  923. // 2x2 matrix
  924. Mat m1 = new Mat (2, 2, CvType.CV_64FC1);
  925. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  926. Mat m2 = new Mat (2, 2, CvType.CV_64FC1);
  927. m2.put (0, 0, 1.1, 2.1, 3.1, 4.1);
  928. Mat m3 = new Mat (2, 2, CvType.CV_64FC1);
  929. m3.put (0, 0, 1.2, 2.2, 3.2, 4.2);
  930. List<Mat> mv = new List<Mat>();
  931. mv.Add (m1);
  932. mv.Add (m2);
  933. mv.Add (m3);
  934. // merge
  935. Mat m_merged = new Mat();
  936. Core.merge (mv, m_merged);
  937. // dump
  938. Debug.Log (""m_merged="" + m_merged.dump());
  939. ";
  940. executionResultText.text = "m_merged=" + m_merged.dump() + "\n";
  941. UpdateScrollRect();
  942. }
  943. public void OnMixChannelsExampleButtonClick()
  944. {
  945. //
  946. // complex composition: mixChannels example
  947. //
  948. // 2x2 matrix
  949. Mat m1 = new Mat(2, 2, CvType.CV_64FC1);
  950. m1.put(0, 0, 1.0, 2.0, 3.0, 4.0);
  951. Mat m2 = new Mat(2, 2, CvType.CV_64FC1);
  952. m2.put(0, 0, 1.1, 2.1, 3.1, 4.1);
  953. Mat m3 = new Mat(2, 2, CvType.CV_64FC1);
  954. m3.put(0, 0, 1.2, 2.2, 3.2, 4.2);
  955. List<Mat> mv = new List<Mat>();
  956. mv.Add(m1);
  957. mv.Add(m2);
  958. mv.Add(m3);
  959. // mat for output must be allocated.
  960. Mat m_mixed1 = new Mat(2, 2, CvType.CV_64FC2);
  961. Mat m_mixed2 = new Mat(2, 2, CvType.CV_64FC2);
  962. MatOfInt fromTo = new MatOfInt(0, 0, 1, 1, 1, 3, 2, 2);
  963. List<Mat> mixv = new List<Mat>();
  964. mixv.Add(m_mixed1);
  965. mixv.Add(m_mixed2);
  966. // mix
  967. Core.mixChannels(mv, mixv, fromTo);
  968. // dump
  969. Debug.Log("m_mixed1=" + m_mixed1.dump());
  970. Debug.Log("m_mixed2=" + m_mixed2.dump());
  971. exampleCodeText.text = @"
  972. //
  973. // complex composition: mixChannels example
  974. //
  975. // 2x2 matrix
  976. Mat m1 = new Mat (2, 2, CvType.CV_64FC1);
  977. m1.put (0, 0, 1.0, 2.0, 3.0, 4.0);
  978. Mat m2 = new Mat (2, 2, CvType.CV_64FC1);
  979. m2.put (0, 0, 1.1, 2.1, 3.1, 4.1);
  980. Mat m3 = new Mat (2, 2, CvType.CV_64FC1);
  981. m3.put (0, 0, 1.2, 2.2, 3.2, 4.2);
  982. List<Mat> mv = new List<Mat>();
  983. mv.Add (m1);
  984. mv.Add (m2);
  985. mv.Add (m3);
  986. // mat for output must be allocated.
  987. Mat m_mixed1 = new Mat(2, 2, CvType.CV_64FC2);
  988. Mat m_mixed2 = new Mat(2, 2, CvType.CV_64FC2);
  989. MatOfInt fromTo = new MatOfInt (0,0, 1,1, 1,3, 2,2);
  990. List<Mat> mixv = new List<Mat> ();
  991. mixv.Add (m_mixed1);
  992. mixv.Add (m_mixed2);
  993. // mix
  994. Core.mixChannels (mv, mixv, fromTo);
  995. // dump
  996. Debug.Log (""m_mixed1="" + m_mixed1.dump());
  997. Debug.Log (""m_mixed2="" + m_mixed2.dump());
  998. ";
  999. executionResultText.text = "m_mixed1=" + m_mixed1.dump() + "\n";
  1000. executionResultText.text += "m_mixed2=" + m_mixed2.dump() + "\n";
  1001. UpdateScrollRect();
  1002. }
  1003. public void OnSplitExampleButtonClick()
  1004. {
  1005. //
  1006. // split example
  1007. //
  1008. // channels=3, 2x3 matrix
  1009. Mat m1 = new Mat(2, 3, CvType.CV_64FC3);
  1010. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18);
  1011. List<Mat> planes = new List<Mat>();
  1012. // split
  1013. Core.split(m1, planes);
  1014. // dump
  1015. foreach (Mat item in planes)
  1016. {
  1017. Debug.Log(item.dump());
  1018. }
  1019. exampleCodeText.text = @"
  1020. //
  1021. // split example
  1022. //
  1023. // channels=3, 2x3 matrix
  1024. Mat m1 = new Mat (2, 3, CvType.CV_64FC3);
  1025. m1.put (0, 0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18);
  1026. List<Mat> planes = new List<Mat>();
  1027. // split
  1028. Core.split (m1, planes);
  1029. // dump
  1030. foreach (Mat item in planes) {
  1031. Debug.Log (item.dump());
  1032. }
  1033. ";
  1034. executionResultText.text = "";
  1035. foreach (Mat item in planes)
  1036. {
  1037. executionResultText.text += item.dump() + "\n";
  1038. }
  1039. UpdateScrollRect();
  1040. }
  1041. public void OnReduceExampleButtonClick()
  1042. {
  1043. //
  1044. // reduce example
  1045. //
  1046. // 3x3 matrix
  1047. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  1048. m1.put(0, 0, 1, 5, 3, 4, 2, 6, 7, 8, 9);
  1049. Mat v1 = new Mat();
  1050. Mat v2 = new Mat();
  1051. Mat v3 = new Mat();
  1052. Mat v4 = new Mat();
  1053. // reduce 3 x 3 matrix to one row
  1054. Core.reduce(m1, v1, 0, Core.REDUCE_SUM); // total value of each column
  1055. Core.reduce(m1, v2, 0, Core.REDUCE_AVG); // total average value of each column
  1056. Core.reduce(m1, v3, 0, Core.REDUCE_MIN); // minimum value of each column
  1057. Core.reduce(m1, v4, 0, Core.REDUCE_MAX); // maximum value of each column
  1058. // dump
  1059. Debug.Log("m1=" + m1.dump());
  1060. Debug.Log("v1(sum)=" + v1.dump());
  1061. Debug.Log("v2(avg)=" + v2.dump());
  1062. Debug.Log("v3(min)=" + v3.dump());
  1063. Debug.Log("v4(max)=" + v4.dump());
  1064. executionResultText.text = "m1=" + m1.dump() + "\n";
  1065. executionResultText.text += "v1(sum)=" + v1.dump() + "\n";
  1066. executionResultText.text += "v2(avg)=" + v2.dump() + "\n";
  1067. executionResultText.text += "v3(min)=" + v3.dump() + "\n";
  1068. executionResultText.text += "v4(max)=" + v4.dump() + "\n";
  1069. // reduce 3 x 3 matrix to one col
  1070. Core.reduce(m1, v1, 1, Core.REDUCE_SUM); // total value of each row
  1071. Core.reduce(m1, v2, 1, Core.REDUCE_AVG); // total average value of row
  1072. Core.reduce(m1, v3, 1, Core.REDUCE_MIN); // minimum value of each row
  1073. Core.reduce(m1, v4, 1, Core.REDUCE_MAX); // maximum value of each row
  1074. // dump
  1075. Debug.Log("m1=" + m1.dump());
  1076. Debug.Log("v1(sum)=" + v1.dump());
  1077. Debug.Log("v2(avg)=" + v2.dump());
  1078. Debug.Log("v3(min)=" + v3.dump());
  1079. Debug.Log("v4(max)=" + v4.dump());
  1080. executionResultText.text += "m1=" + m1.dump() + "\n";
  1081. executionResultText.text += "v1(sum)=" + v1.dump() + "\n";
  1082. executionResultText.text += "v2(avg)=" + v2.dump() + "\n";
  1083. executionResultText.text += "v3(min)=" + v3.dump() + "\n";
  1084. executionResultText.text += "v4(max)=" + v4.dump() + "\n";
  1085. exampleCodeText.text = @"
  1086. //
  1087. // reduce example
  1088. //
  1089. // 3x3 matrix
  1090. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1091. m1.put (0, 0, 1, 5, 3, 4, 2, 6, 7, 8, 9);
  1092. Mat v1 = new Mat ();
  1093. Mat v2 = new Mat ();
  1094. Mat v3 = new Mat ();
  1095. Mat v4 = new Mat ();
  1096. // reduce 3 x 3 matrix to one row
  1097. Core.reduce (m1, v1, 0, Core.REDUCE_SUM); // total value of each column
  1098. Core.reduce (m1, v2, 0, Core.REDUCE_AVG); // total average value of each column
  1099. Core.reduce (m1, v3, 0, Core.REDUCE_MIN); // minimum value of each column
  1100. Core.reduce (m1, v4, 0, Core.REDUCE_MAX); // maximum value of each column
  1101. // dump
  1102. Debug.Log (""m1="" + m1.dump());
  1103. Debug.Log (""v1(sum)="" + v1.dump());
  1104. Debug.Log (""v2(avg)="" + v2.dump());
  1105. Debug.Log (""v3(min)="" + v3.dump());
  1106. Debug.Log (""v4(max)="" + v4.dump());
  1107. // reduce 3 x 3 matrix to one col
  1108. Core.reduce (m1, v1, 1, Core.REDUCE_SUM); // total value of each row
  1109. Core.reduce (m1, v2, 1, Core.REDUCE_AVG); // total average value of row
  1110. Core.reduce (m1, v3, 1, Core.REDUCE_MIN); // minimum value of each row
  1111. Core.reduce (m1, v4, 1, Core.REDUCE_MAX); // maximum value of each row
  1112. // dump
  1113. Debug.Log (""m1="" + m1.dump());
  1114. Debug.Log (""v1(sum)="" + v1.dump());
  1115. Debug.Log (""v2(avg)="" + v2.dump());
  1116. Debug.Log (""v3(min)="" + v3.dump());
  1117. Debug.Log (""v4(max)="" + v4.dump());
  1118. ";
  1119. UpdateScrollRect();
  1120. }
  1121. public void OnSubmatrixExampleButtonClick()
  1122. {
  1123. //
  1124. // submatrix (ROI) example
  1125. //
  1126. // 3x3 matrix
  1127. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  1128. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1129. Debug.Log("m1=" + m1.dump());
  1130. executionResultText.text = "m1=" + m1.dump() + "\n";
  1131. // get submatrix (ROI) of range (row[0_2] col[0_2])
  1132. Mat m2 = new Mat(m1, new OpenCVForUnity.CoreModule.Rect(0, 0, 2, 2));
  1133. Debug.Log("m2=" + m2.dump());
  1134. executionResultText.text += "m2=" + m2.dump() + "\n";
  1135. Debug.Log("m2.submat()=" + m2.submat(0, 2, 0, 2).dump());
  1136. executionResultText.text += "m2.submat()=" + m2.submat(0, 2, 0, 2).dump() + "\n";
  1137. // find the parent matrix size of the submatrix (ROI) m2 and its position in it
  1138. Size wholeSize = new Size();
  1139. Point ofs = new Point();
  1140. m2.locateROI(wholeSize, ofs);
  1141. Debug.Log("wholeSize:" + wholeSize.width + "x" + wholeSize.height);
  1142. Debug.Log("offset:" + ofs.x + ", " + ofs.y);
  1143. executionResultText.text += "wholeSize:" + wholeSize.width + "x" + wholeSize.height + "\n";
  1144. executionResultText.text += "offset:" + ofs.x + ", " + ofs.y + "\n";
  1145. // expand the range of submatrix (ROI)
  1146. m2.adjustROI(0, 1, 0, 1);
  1147. Debug.Log("rows=" + m2.rows() + ", " + "cols=" + m2.cols());
  1148. Debug.Log("m2=" + m2.dump());
  1149. executionResultText.text += "rows=" + m2.rows() + ", " + "cols=" + m2.cols() + "\n";
  1150. executionResultText.text += "m2=" + m2.dump() + "\n";
  1151. exampleCodeText.text = @"
  1152. //
  1153. // submatrix (ROI) example
  1154. //
  1155. // 3x3 matrix
  1156. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1157. m1.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1158. Debug.Log (""m1="" + m1.dump ());
  1159. // get submatrix (ROI) of range (row[0_2] col[0_2])
  1160. Mat m2 = new Mat (m1, new OpenCVForUnity.CoreModule.Rect(0,0,2,2));
  1161. Debug.Log (""m2="" + m2.dump());
  1162. Debug.Log (""m2.submat()="" + m2.submat(0,2,0,2).dump());
  1163. // find the parent matrix size of the submatrix (ROI) m2 and its position in it
  1164. Size wholeSize = new Size ();
  1165. Point ofs = new Point ();
  1166. m2.locateROI (wholeSize, ofs);
  1167. Debug.Log (""wholeSize:"" + wholeSize.width + ""x"" + wholeSize.height);
  1168. Debug.Log (""offset:"" + ofs.x + "", "" + ofs.y);
  1169. // expand the range of submatrix (ROI)
  1170. m2.adjustROI(0, 1, 0, 1);
  1171. Debug.Log (""rows="" + m2.rows() + "", "" + ""cols="" + m2.cols());
  1172. Debug.Log (""m2="" + m2.dump());
  1173. ";
  1174. UpdateScrollRect();
  1175. }
  1176. public void OnRandShuffleExampleButtonClick()
  1177. {
  1178. //
  1179. // randShuffle example
  1180. //
  1181. // 4x5 matrix
  1182. Mat m1 = new Mat(4, 5, CvType.CV_64FC1);
  1183. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
  1184. Debug.Log("m1(original)=" + m1.dump());
  1185. executionResultText.text = "m1(original)=" + m1.dump() + "\n";
  1186. // shuffle
  1187. Core.randShuffle(m1, UnityEngine.Random.value);
  1188. Debug.Log("m1(shuffle)=" + m1.dump());
  1189. executionResultText.text += "m1(shuffle)=" + m1.dump() + "\n";
  1190. // submatrix
  1191. Mat m2 = new Mat(m1, new OpenCVForUnity.CoreModule.Rect(1, 1, 3, 2));
  1192. Debug.Log("m2(sub-matrix)=" + m2.dump());
  1193. executionResultText.text += "m2(sub-matrix)=" + m2.dump() + "\n";
  1194. Core.randShuffle(m2, UnityEngine.Random.value);
  1195. Debug.Log("m2(sub-matrix)=" + m2.dump());
  1196. Debug.Log("m1=" + m1.dump());
  1197. executionResultText.text += "m2(shuffle sub-matrix)=" + m2.dump() + "\n";
  1198. executionResultText.text += "m1=" + m1.dump() + "\n";
  1199. exampleCodeText.text = @"
  1200. //
  1201. // randShuffle example
  1202. //
  1203. // 4x5 matrix
  1204. Mat m1 = new Mat (4, 5, CvType.CV_64FC1);
  1205. m1.put (0, 0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
  1206. Debug.Log (""m1(original)="" + m1.dump ());
  1207. // shuffle
  1208. Core.randShuffle (m1, UnityEngine.Random.value);
  1209. Debug.Log (""m1(shuffle)="" + m1.dump ());
  1210. // submatrix
  1211. Mat m2 = new Mat (m1, new OpenCVForUnity.CoreModule.Rect(1,1,3,2));
  1212. Debug.Log (""m2(sub-matrix)="" + m2.dump());
  1213. Core.randShuffle (m2, UnityEngine.Random.value);
  1214. Debug.Log (""m2(sub-matrix)="" + m2.dump());
  1215. Debug.Log (""m1="" + m1.dump ());
  1216. ";
  1217. UpdateScrollRect();
  1218. }
  1219. public void OnSortExampleButtonClick()
  1220. {
  1221. //
  1222. // sort example
  1223. //
  1224. // 5x5 matrix
  1225. Mat m1 = new Mat(5, 5, CvType.CV_8UC1);
  1226. Core.randu(m1, 0, 25);
  1227. Debug.Log("m1=" + m1.dump());
  1228. executionResultText.text = "m1=" + m1.dump() + "\n";
  1229. Mat dst_mat = new Mat();
  1230. // sort ascending
  1231. Core.sort(m1, dst_mat, Core.SORT_EVERY_ROW | Core.SORT_ASCENDING);
  1232. Debug.Log("ROW|ASCENDING:" + dst_mat.dump());
  1233. executionResultText.text += "ROW|ASCENDING:" + dst_mat.dump() + "\n";
  1234. // sort descending
  1235. Core.sort(m1, dst_mat, Core.SORT_EVERY_ROW | Core.SORT_DESCENDING);
  1236. Debug.Log("ROW|DESCENDING:" + dst_mat.dump());
  1237. executionResultText.text += "ROW|DESCENDING:" + dst_mat.dump() + "\n";
  1238. // sort ascending
  1239. Core.sort(m1, dst_mat, Core.SORT_EVERY_COLUMN | Core.SORT_ASCENDING);
  1240. Debug.Log("COLUMN|ASCENDING:" + dst_mat.dump());
  1241. executionResultText.text += "COLUMN|ASCENDING:" + dst_mat.dump() + "\n";
  1242. // sort descending
  1243. Core.sort(m1, dst_mat, Core.SORT_EVERY_COLUMN | Core.SORT_DESCENDING);
  1244. Debug.Log("COLUMN|DESCENDING:" + dst_mat.dump());
  1245. executionResultText.text += "COLUMN|DESCENDING:" + dst_mat.dump() + "\n";
  1246. exampleCodeText.text = @"
  1247. //
  1248. // sort example
  1249. //
  1250. // 5x5 matrix
  1251. Mat m1 = new Mat (5, 5, CvType.CV_8UC1);
  1252. Core.randu (m1, 0, 25);
  1253. Debug.Log (""m1="" + m1.dump ());
  1254. executionResultText.text = ""m1="" + m1.dump() + ""\n"";
  1255. Mat dst_mat = new Mat ();
  1256. // sort ascending
  1257. Core.sort (m1, dst_mat, Core.SORT_EVERY_ROW|Core.SORT_ASCENDING);
  1258. Debug.Log (""ROW|ASCENDING:"" + dst_mat.dump ());
  1259. // sort descending
  1260. Core.sort (m1, dst_mat, Core.SORT_EVERY_ROW|Core.SORT_DESCENDING);
  1261. Debug.Log (""ROW|DESCENDING:"" + dst_mat.dump ());
  1262. // sort ascending
  1263. Core.sort (m1, dst_mat, Core.SORT_EVERY_COLUMN|Core.SORT_ASCENDING);
  1264. Debug.Log (""COLUMN|ASCENDING:"" + dst_mat.dump ());
  1265. // sort descending
  1266. Core.sort (m1, dst_mat, Core.SORT_EVERY_COLUMN|Core.SORT_DESCENDING);
  1267. Debug.Log (""COLUMN|DESCENDING:"" + dst_mat.dump ());
  1268. ";
  1269. UpdateScrollRect();
  1270. }
  1271. public void OnComparisonExampleButtonClick()
  1272. {
  1273. //
  1274. // comparison example
  1275. //
  1276. // 3x3 matrix
  1277. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  1278. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1279. Mat m2 = new Mat(3, 3, CvType.CV_64FC1);
  1280. m2.put(0, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1);
  1281. Debug.Log("m1=" + m1.dump());
  1282. Debug.Log("m2=" + m2.dump());
  1283. executionResultText.text = "m1=" + m1.dump() + "\n";
  1284. executionResultText.text += "m2=" + m2.dump() + "\n";
  1285. Mat dst_mat = new Mat();
  1286. // GT (M1 > M2)
  1287. Core.compare(m1, m2, dst_mat, Core.CMP_GT);
  1288. Debug.Log("GT (M1 > M2)=" + dst_mat.dump());
  1289. executionResultText.text += "GT (M1 > M2)=" + dst_mat.dump() + "\n";
  1290. // GE (M1 >= M2)
  1291. Core.compare(m1, m2, dst_mat, Core.CMP_GE);
  1292. Debug.Log("GE (M1 >= M2)=" + dst_mat.dump());
  1293. executionResultText.text += "GE (M1 >= M2)=" + dst_mat.dump() + "\n";
  1294. // EQ (M1 == M2)
  1295. Core.compare(m1, m2, dst_mat, Core.CMP_EQ);
  1296. Debug.Log("EQ (M1 == M2)=" + dst_mat.dump());
  1297. executionResultText.text += "EQ (M1 == M2)=" + dst_mat.dump() + "\n";
  1298. // NE (M1 != M2)
  1299. Core.compare(m1, m2, dst_mat, Core.CMP_NE);
  1300. Debug.Log("NE (M1 != M2)=" + dst_mat.dump());
  1301. executionResultText.text += "NE (M1 != M2)=" + dst_mat.dump() + "\n";
  1302. // LE (M1 <= M2)
  1303. Core.compare(m1, m2, dst_mat, Core.CMP_LE);
  1304. Debug.Log("LE (M1 <= M2)=" + dst_mat.dump());
  1305. executionResultText.text += "LE (M1 <= M2)=" + dst_mat.dump() + "\n";
  1306. // LT (M1 < M2)
  1307. Core.compare(m1, m2, dst_mat, Core.CMP_LT);
  1308. Debug.Log("LT (M1 < M2)=" + dst_mat.dump());
  1309. executionResultText.text += "LT (M1 < M2)=" + dst_mat.dump() + "\n";
  1310. exampleCodeText.text = @"
  1311. //
  1312. // comparison example
  1313. //
  1314. // 3x3 matrix
  1315. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1316. m1.put (0, 0, 1,2,3,4,5,6,7,8,9);
  1317. Mat m2 = new Mat (3, 3, CvType.CV_64FC1);
  1318. m2.put (0, 0, 10,11,12,13,14,15,16,17,18);
  1319. Debug.Log (""m1="" + m1.dump ());
  1320. Debug.Log (""m2="" + m2.dump ());
  1321. Mat dst_mat = new Mat ();
  1322. // GT (M1 > M2)
  1323. Core.compare (m1, m2, dst_mat, Core.CMP_GT);
  1324. Debug.Log (""GT (M1 > M2)="" + dst_mat.dump ());
  1325. // GE (M1 >= M2)
  1326. Core.compare (m1, m2, dst_mat, Core.CMP_GE);
  1327. Debug.Log (""GE (M1 >= M2)="" + dst_mat.dump ());
  1328. // EQ (M1 == M2)
  1329. Core.compare (m1, m2, dst_mat, Core.CMP_EQ);
  1330. Debug.Log (""EQ (M1 == M2)="" + dst_mat.dump ());
  1331. // NE (M1 != M2)
  1332. Core.compare (m1, m2, dst_mat, Core.CMP_NE);
  1333. Debug.Log (""NE (M1 != M2)="" + dst_mat.dump ());
  1334. // LE (M1 <= M2)
  1335. Core.compare (m1, m2, dst_mat, Core.CMP_LE);
  1336. Debug.Log (""LE (M1 <= M2)="" + dst_mat.dump ());
  1337. // LT (M1 < M2)
  1338. Core.compare (m1, m2, dst_mat, Core.CMP_LT);
  1339. Debug.Log (""LT (M1 < M2)="" + dst_mat.dump ());
  1340. ";
  1341. UpdateScrollRect();
  1342. }
  1343. public void OnOperatorsExampleButtonClick()
  1344. {
  1345. //
  1346. // operators example
  1347. //
  1348. // 3x3 matrix
  1349. Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
  1350. m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1351. Mat m2 = new Mat(3, 3, CvType.CV_64FC1);
  1352. m2.put(0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18);
  1353. Scalar s = new Scalar(5);
  1354. double alpha = 3;
  1355. Debug.Log("m1=" + m1.dump());
  1356. Debug.Log("m2=" + m2.dump());
  1357. Debug.Log("s=" + s);
  1358. Debug.Log("alpha=" + alpha);
  1359. executionResultText.text = "m1=" + m1.dump() + "\n";
  1360. executionResultText.text += "m2=" + m2.dump() + "\n";
  1361. executionResultText.text += "s=" + s + "\n";
  1362. executionResultText.text += "alpha=" + alpha + "\n";
  1363. // Addition, subtraction, negation: A+B, A-B, A+s, A-s, s+A, s-A, -A
  1364. // (M1 + M2 = Core.add (M1, M2, M_dst))
  1365. Debug.Log("m1+m2=" + (m1 + m2).dump());
  1366. executionResultText.text += "m1+m2=" + (m1 + m2).dump() + "\n";
  1367. // (M1 + s = Core.add (M1, s, M_dst))
  1368. Debug.Log("m1+s=" + (m1 + s).dump());
  1369. executionResultText.text += "m1+s=" + (m1 + s).dump() + "\n";
  1370. // (M1 - M2 = Core.subtract (M1, M2, M_dst))
  1371. Debug.Log("m1-m2=" + (m1 - m2).dump());
  1372. executionResultText.text += "m1-m2=" + (m1 - m2).dump() + "\n";
  1373. // (M1 - s = Core.subtract (M1, s, M_dst))
  1374. Debug.Log("m1-s=" + (m1 - s).dump());
  1375. executionResultText.text += "m1-s=" + (m1 - s).dump() + "\n";
  1376. // (-M1 = Core.multiply (M1, Scalar.all (-1), M_dst))
  1377. Debug.Log("-m1=" + (-m1).dump());
  1378. executionResultText.text += "-m1=" + (-m1).dump() + "\n";
  1379. // Scaling: A*alpha A/alpha
  1380. // (M1 * 3 = Core.multiply (M1, Scalar.all (3), M_dst))
  1381. Debug.Log("m1*alpha=" + (m1 * alpha).dump());
  1382. executionResultText.text += "m1*alpha=" + (m1 * alpha).dump() + "\n";
  1383. // (M1 / 3 = Core.divide (M1, Scalar.all (3), M_dst))
  1384. Debug.Log("m1/alpha=" + (m1 / alpha).dump());
  1385. executionResultText.text += "m1/alpha=" + (m1 / alpha).dump() + "\n";
  1386. // Per-element multiplication and division: A.mul(B), A/B, alpha/A
  1387. // (M1.mul(M2) = M1.mul (M2))
  1388. Debug.Log("m1.mul(m2)=" + (m1.mul(m2)).dump());
  1389. executionResultText.text += "m1.mul(m2)=" + (m1.mul(m2)).dump() + "\n";
  1390. // (M1 / M2 = Core.divide (M1, M2, M_dst))
  1391. Debug.Log("m1/m2=" + (m1 / m2).dump());
  1392. executionResultText.text += "m1/m2=" + (m1 / m2).dump() + "\n";
  1393. // (3 / M1 = Core.divide (new Mat (M1.size (), M1.type (), Scalar.all (3)), M1, M_dst))
  1394. Debug.Log("alpha/m2=" + (alpha / m2).dump());
  1395. executionResultText.text += "alpha/m2=" + (alpha / m2).dump() + "\n";
  1396. // Matrix multiplication: A*B
  1397. // (M1 * M2 = Core.gemm (M1, M2, 1, new Mat (), 0, M_dst))
  1398. Debug.Log("m1*m2=" + (m1 * m2).dump());
  1399. executionResultText.text += "m1*m2=" + (m1 * m2).dump() + "\n";
  1400. // Bitwise logical operations: A logicop B, A logicop s, s logicop A, ~A, where logicop is one of : &, |, ^.
  1401. // (M1 & M2 = Core.bitwise_and (M1, M2, M_dst))
  1402. Debug.Log("m1&m2=" + (m1 & m2).dump());
  1403. executionResultText.text += "m1&m2=" + (m1 & m2).dump() + "\n";
  1404. // (M1 | M2 = Core.bitwise_or (M1, M2, M_dst))
  1405. Debug.Log("m1|m2=" + (m1 | m2).dump());
  1406. executionResultText.text += "m1|m2=" + (m1 | m2).dump() + "\n";
  1407. // (M1 ^ M2 = Core.bitwise_xor (M1, M2, M_dst))
  1408. Debug.Log("m1^m2=" + (m1 ^ m2).dump());
  1409. executionResultText.text += "m1^m2=" + (m1 ^ m2).dump() + "\n";
  1410. // (~M1 = Core.bitwise_not (M1, M_dst))
  1411. Debug.Log("~m1=" + (~m1).dump());
  1412. executionResultText.text += "~m1=" + (~m1).dump() + "\n";
  1413. // Note.
  1414. // The assignment operator behavior is different from OpenCV (c ++).
  1415. // For example, C = A + B will not be expanded to cv :: add (A, B, C).
  1416. // Also cannot assign a scalar to Mat like C = s.
  1417. // In c#, it is not possible to explicitly overload compound assignment operators such as “A *= B“.
  1418. // Instead, binary operator overloading is used implicitly.
  1419. // Therefore, whenever an operator is used, a new mat is created and assigned.
  1420. exampleCodeText.text = @"
  1421. //
  1422. // operators example
  1423. //
  1424. // 3x3 matrix
  1425. Mat m1 = new Mat (3, 3, CvType.CV_64FC1);
  1426. m1.put (0, 0, 1,2,3,4,5,6,7,8,9);
  1427. Mat m2 = new Mat (3, 3, CvType.CV_64FC1);
  1428. m2.put (0, 0, 10,11,12,13,14,15,16,17,18);
  1429. Scalar s = new Scalar (5);
  1430. double alpha = 3;
  1431. Debug.Log (""m1="" + m1.dump ());
  1432. Debug.Log (""m2="" + m2.dump ());
  1433. Debug.Log (""s="" + s);
  1434. Debug.Log (""alpha="" + alpha);
  1435. // Addition, subtraction, negation: A+B, A-B, A+s, A-s, s+A, s-A, -A
  1436. // (M1 + M2 = Core.add (M1, M2, M_dst))
  1437. Debug.Log (""m1+m2="" + (m1 + m2).dump());
  1438. // (M1 + s = Core.add (M1, s, M_dst))
  1439. Debug.Log (""m1+s="" + (m1 + s).dump());
  1440. // (M1 - M2 = Core.subtract (M1, M2, M_dst))
  1441. Debug.Log (""m1-m2="" + (m1 - m2).dump());
  1442. // (M1 - s = Core.subtract (M1, s, M_dst))
  1443. Debug.Log (""m1-s="" + (m1 - s).dump());
  1444. // (-M1 = Core.multiply (M1, Scalar.all (-1), M_dst))
  1445. Debug.Log (""-m1="" + (-m1).dump());
  1446. // Scaling: A*alpha A/alpha
  1447. // (M1 * 3 = Core.multiply (M1, Scalar.all (3), M_dst))
  1448. Debug.Log (""m1*alpha="" + (m1*alpha).dump());
  1449. // (M1 / 3 = Core.divide (M1, Scalar.all (3), M_dst))
  1450. Debug.Log (""m1/alpha="" + (m1/alpha).dump());
  1451. // Per-element multiplication and division: A.mul(B), A/B, alpha/A
  1452. // (M1.mul(M2) = M1.mul (M2))
  1453. Debug.Log (""m1.mul(m2)="" + (m1.mul(m2)).dump());
  1454. // (M1 / M2 = Core.divide (M1, M2, M_dst))
  1455. Debug.Log (""m1/m2="" + (m1 / m2).dump());
  1456. // (3 / M1 = Core.divide (new Mat (M1.size (), M1.type (), Scalar.all (3)), M1, M_dst))
  1457. Debug.Log (""alpha/m2="" + (alpha / m2).dump());
  1458. // Matrix multiplication: A*B
  1459. // (M1 * M2 = Core.gemm (M1, M2, 1, new Mat (), 0, M_dst))
  1460. Debug.Log (""m1*m2="" + (m1 * m2).dump());
  1461. // Bitwise logical operations: A logicop B, A logicop s, s logicop A, ~A, where logicop is one of : &, |, ^.
  1462. // (M1 & M2 = Core.bitwise_and (M1, M2, M_dst))
  1463. Debug.Log (""m1&m2="" + (m1 & m2).dump());
  1464. // (M1 | M2 = Core.bitwise_or (M1, M2, M_dst))
  1465. Debug.Log (""m1|m2="" + (m1 | m2).dump());
  1466. // (M1 ^ M2 = Core.bitwise_xor (M1, M2, M_dst))
  1467. Debug.Log (""m1^m2="" + (m1 ^ m2).dump());
  1468. // (~M1 = Core.bitwise_not (M1, M_dst))
  1469. Debug.Log (""~m1="" + (~m1).dump());
  1470. // Note.
  1471. // The assignment operator behavior is different from OpenCV (c ++).
  1472. // For example, C = A + B will not be expanded to cv :: add (A, B, C).
  1473. // Also cannot assign a scalar to Mat like C = s.
  1474. // In c#, it is not possible to explicitly overload compound assignment operators such as “A *= B“.
  1475. // Instead, binary operator overloading is used implicitly.
  1476. // Therefore, whenever an operator is used, a new mat is created and assigned.
  1477. ";
  1478. UpdateScrollRect();
  1479. }
  1480. public void OnGetAndPutExampleButtonClick()
  1481. {
  1482. //
  1483. // get and put example
  1484. //
  1485. // channels=4 3x3 matrix
  1486. Mat m1 = new Mat(3, 3, CvType.CV_8UC4, new Scalar(1, 2, 3, 4));
  1487. Debug.Log("m1=" + m1.dump());
  1488. executionResultText.text = "m1=" + m1.dump() + "\n";
  1489. // get an element value.
  1490. double[] m1_1_1 = m1.get(1, 1);
  1491. Debug.Log("m1[1,1]=" + m1_1_1[0] + ", " + m1_1_1[1] + ", " + m1_1_1[2] + ", " + m1_1_1[3]);
  1492. executionResultText.text += "m1[1,1]=" + m1_1_1[0] + ", " + m1_1_1[1] + ", " + m1_1_1[2] + ", " + m1_1_1[3] + "\n";
  1493. // get an array of all element values.
  1494. byte[] m1_array = new byte[m1.total() * m1.channels()];
  1495. m1.get(0, 0, m1_array);
  1496. string dump_str = "";
  1497. foreach (var i in m1_array)
  1498. {
  1499. dump_str += i + ", ";
  1500. }
  1501. Debug.Log("m1_array=" + dump_str);
  1502. executionResultText.text += "m1_array=" + dump_str + "\n";
  1503. // another faster way. (use MatUtils.copyFromMat())
  1504. MatUtils.copyFromMat(m1, m1_array);
  1505. dump_str = "";
  1506. foreach (var i in m1_array)
  1507. {
  1508. dump_str += i + ", ";
  1509. }
  1510. Debug.Log("m1_array (use MatUtils.copyFromMat())=" + dump_str);
  1511. executionResultText.text += "m1_array (use MatUtils.copyFromMat())=" + dump_str + "\n";
  1512. // put an element value in a matrix.
  1513. Mat m2 = m1.clone();
  1514. m2.put(1, 1, 5, 6, 7, 8);
  1515. Debug.Log("m2=" + m2.dump());
  1516. executionResultText.text += "m2=" + m2.dump() + "\n";
  1517. // put an array of element values in a matrix.
  1518. byte[] m2_arr = new byte[] {
  1519. 5,
  1520. 6,
  1521. 7,
  1522. 8,
  1523. 5,
  1524. 6,
  1525. 7,
  1526. 8,
  1527. 5,
  1528. 6,
  1529. 7,
  1530. 8,
  1531. 5,
  1532. 6,
  1533. 7,
  1534. 8,
  1535. 5,
  1536. 6,
  1537. 7,
  1538. 8,
  1539. 5,
  1540. 6,
  1541. 7,
  1542. 8,
  1543. 5,
  1544. 6,
  1545. 7,
  1546. 8,
  1547. 5,
  1548. 6,
  1549. 7,
  1550. 8,
  1551. 5,
  1552. 6,
  1553. 7,
  1554. 8
  1555. };
  1556. m2.put(0, 0, m2_arr);
  1557. Debug.Log("m2=" + m2.dump());
  1558. executionResultText.text += "m2=" + m2.dump() + "\n";
  1559. // another faster way. (use MatUtils.copyToMat())
  1560. MatUtils.copyToMat(m2_arr, m2);
  1561. Debug.Log("m2 (use MatUtils.copyToMat())=" + m2.dump());
  1562. executionResultText.text += "m2 (use MatUtils.copyToMat())=" + m2.dump() + "\n";
  1563. // fill element values (setTo method)
  1564. m2.setTo(new Scalar(100, 100, 100, 100));
  1565. Debug.Log("m2=" + m2.dump());
  1566. executionResultText.text += "m2=" + m2.dump() + "\n";
  1567. exampleCodeText.text = @"
  1568. //
  1569. // get and put example
  1570. //
  1571. // channels=4 3x3 matrix
  1572. Mat m1 = new Mat (3, 3, CvType.CV_8UC4 , new Scalar(1,2,3,4));
  1573. Debug.Log (""m1="" + m1.dump ());
  1574. // get an element value.
  1575. double[] m1_1_1 = m1.get(1,1);
  1576. Debug.Log (""m1[1,1]="" + m1_1_1[0] + "", "" + m1_1_1[1] + "", "" + m1_1_1[2] + "", "" + m1_1_1[3]);
  1577. // get an array of all element values.
  1578. byte[] m1_array = new byte[m1.total () * m1.channels()];
  1579. m1.get (0, 0, m1_array);
  1580. string dump_str = """";
  1581. foreach (var i in m1_array){
  1582. dump_str += i + "", "";
  1583. }
  1584. Debug.Log (""m1_array="" + dump_str);
  1585. // another faster way. (use MatUtils.copyFromMat())
  1586. MatUtils.copyFromMat (m1, m1_array);
  1587. dump_str = """";
  1588. foreach (var i in m1_array) {
  1589. dump_str += i + "", "";
  1590. }
  1591. Debug.Log (""m1_array (use MatUtils.copyFromMat())="" + dump_str);
  1592. // put an element value in a matrix.
  1593. Mat m2 = m1.clone ();
  1594. m2.put (1, 1, 5,6,7,8);
  1595. Debug.Log (""m2="" + m2.dump ());
  1596. // put an array of element values in a matrix.
  1597. 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};
  1598. m2.put (0, 0, m2_arr);
  1599. Debug.Log (""m2="" + m2.dump ());
  1600. // another faster way. (use MatUtils.copyToMat())
  1601. MatUtils.copyToMat (m2_arr, m2);
  1602. Debug.Log (""m2 (use MatUtils.copyToMat())="" + m2.dump ());
  1603. // fill element values (setTo method)
  1604. m2.setTo(new Scalar(100,100,100,100));
  1605. Debug.Log (""m2="" + m2.dump ());
  1606. ";
  1607. UpdateScrollRect();
  1608. }
  1609. public void OnAccessingPixelValueExampleButtonClick()
  1610. {
  1611. //
  1612. // accessing pixel value example
  1613. //
  1614. // How access pixel value in an OpenCV Mat.
  1615. // channels=4 512x512 matrix (RGBA color image)
  1616. Mat imgMat = new Mat(512, 512, CvType.CV_8UC4, new Scalar(0, 0, 0, 255));
  1617. System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
  1618. //
  1619. // 1. Use get and put method.
  1620. //
  1621. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1622. watch.Start();
  1623. int rows = imgMat.rows();
  1624. int cols = imgMat.cols();
  1625. for (int i0 = 0; i0 < rows; i0++)
  1626. {
  1627. for (int i1 = 0; i1 < cols; i1++)
  1628. {
  1629. byte[] p = new byte[4];
  1630. imgMat.get(i0, i1, p);
  1631. p[0] = (byte)(p[0] + 127); // R
  1632. p[1] = (byte)(p[1] + 127); // G
  1633. p[2] = (byte)(p[2] + 127); // B
  1634. imgMat.put(i0, i1, p);
  1635. }
  1636. }
  1637. watch.Stop();
  1638. Debug.Log("1. Use get and put method. time: " + watch.ElapsedMilliseconds + " ms");
  1639. executionResultText.text = "1. Use get and put method. time: " + watch.ElapsedMilliseconds + " ms" + "\n";
  1640. //
  1641. // 2. Use MatIndexer.
  1642. //
  1643. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1644. watch.Reset();
  1645. watch.Start();
  1646. MatIndexer indexer = new MatIndexer(imgMat);
  1647. rows = imgMat.rows();
  1648. cols = imgMat.cols();
  1649. for (int i0 = 0; i0 < rows; i0++)
  1650. {
  1651. for (int i1 = 0; i1 < cols; i1++)
  1652. {
  1653. byte[] p = new byte[4];
  1654. indexer.get(i0, i1, p);
  1655. p[0] = (byte)(p[0] + 127); // R
  1656. p[1] = (byte)(p[1] + 127); // G
  1657. p[2] = (byte)(p[2] + 127); // B
  1658. indexer.put(i0, i1, p);
  1659. }
  1660. }
  1661. watch.Stop();
  1662. Debug.Log("2. Use MatIndexer. time: " + watch.ElapsedMilliseconds + " ms");
  1663. executionResultText.text += "2. Use MatIndexer. time: " + watch.ElapsedMilliseconds + " ms" + "\n";
  1664. //
  1665. // 3. Use MatUtils.copyFromMat and MatUtils.copyToMat method.
  1666. //
  1667. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1668. watch.Reset();
  1669. watch.Start();
  1670. // Copies an OpenCV Mat data to a pixel data Array.
  1671. byte[] img_array = new byte[imgMat.total() * imgMat.channels()];
  1672. MatUtils.copyFromMat(imgMat, img_array);
  1673. long step0 = imgMat.step1(0);
  1674. long step1 = imgMat.step1(1);
  1675. rows = imgMat.rows();
  1676. cols = imgMat.cols();
  1677. for (int i0 = 0; i0 < rows; i0++)
  1678. {
  1679. for (int i1 = 0; i1 < cols; i1++)
  1680. {
  1681. long p1 = step0 * i0 + step1 * i1;
  1682. long p2 = p1 + 1;
  1683. long p3 = p1 + 2;
  1684. img_array[p1] = (byte)(img_array[p1] + 127); // R
  1685. img_array[p2] = (byte)(img_array[p2] + 127); // G
  1686. img_array[p3] = (byte)(img_array[p3] + 127); // B
  1687. }
  1688. }
  1689. // Copies a pixel data Array to an OpenCV Mat data.
  1690. MatUtils.copyToMat(img_array, imgMat);
  1691. watch.Stop();
  1692. Debug.Log("3. Use MatUtils.copyFromMat and MatUtils.copyToMat method. time: " + watch.ElapsedMilliseconds + " ms");
  1693. executionResultText.text += "3. Use MatUtils.copyFromMat and MatUtils.copyToMat method. time: " + watch.ElapsedMilliseconds + " ms" + "\n";
  1694. #if OPENCV_USE_UNSAFE_CODE
  1695. //
  1696. // 4. Use pointer access.
  1697. //
  1698. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1699. watch.Reset();
  1700. watch.Start();
  1701. step0 = imgMat.step1(0);
  1702. step1 = imgMat.step1(1);
  1703. long ptrVal = imgMat.dataAddr();
  1704. unsafe
  1705. {
  1706. rows = imgMat.rows();
  1707. cols = imgMat.cols();
  1708. for (int i0 = 0; i0 < rows; i0++)
  1709. {
  1710. for (int i1 = 0; i1 < cols; i1++)
  1711. {
  1712. byte* p1 = (byte*)(ptrVal + (step0 * i0) + (step1 * i1));
  1713. byte* p2 = p1 + 1;
  1714. byte* p3 = p1 + 2;
  1715. *p1 = (byte)(*p1 + 127); // R
  1716. *p2 = (byte)(*p2 + 127); // G
  1717. *p3 = (byte)(*p3 + 127); // B
  1718. }
  1719. }
  1720. }
  1721. watch.Stop();
  1722. Debug.Log("4. Use pointer access. time: " + watch.ElapsedMilliseconds + " ms");
  1723. executionResultText.text += "4. Use pointer access. time: " + watch.ElapsedMilliseconds + " ms" + "\n";
  1724. #endif
  1725. exampleCodeText.text = @"
  1726. //
  1727. // accessing pixel value example
  1728. //
  1729. // How access pixel value in an OpenCV Mat.
  1730. // channels=4 512x512 matrix (RGBA color image)
  1731. Mat imgMat = new Mat (512, 512, CvType.CV_8UC4, new Scalar(0, 0, 0, 255));
  1732. System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
  1733. //
  1734. // 1. Use get and put method.
  1735. //
  1736. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1737. watch.Start();
  1738. int rows = imgMat.rows();
  1739. int cols = imgMat.cols();
  1740. for (int i0 = 0; i0 < rows; i0++)
  1741. {
  1742. for (int i1 = 0; i1 < cols; i1++)
  1743. {
  1744. byte[] p = new byte[4];
  1745. imgMat.get(i0, i1, p);
  1746. p[0] = (byte)(p[0] + 127); // R
  1747. p[1] = (byte)(p[1] + 127); // G
  1748. p[2] = (byte)(p[2] + 127); // B
  1749. imgMat.put(i0, i1, p);
  1750. }
  1751. }
  1752. watch.Stop();
  1753. Debug.Log(""1.Use get and put method. time: "" + watch.ElapsedMilliseconds + "" ms"");
  1754. //
  1755. // 2. Use MatIndexer.
  1756. //
  1757. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1758. watch.Reset();
  1759. watch.Start();
  1760. MatIndexer indexer = new MatIndexer(imgMat);
  1761. rows = imgMat.rows();
  1762. cols = imgMat.cols();
  1763. for (int i0 = 0; i0 < rows; i0++)
  1764. {
  1765. for (int i1 = 0; i1 < cols; i1++)
  1766. {
  1767. byte[] p = new byte[4];
  1768. indexer.get(i0, i1, p);
  1769. p[0] = (byte)(p[0] + 127); // R
  1770. p[1] = (byte)(p[1] + 127); // G
  1771. p[2] = (byte)(p[2] + 127); // B
  1772. indexer.put(i0, i1, p);
  1773. }
  1774. }
  1775. watch.Stop();
  1776. Debug.Log(""2. Use MatIndexer. time: "" + watch.ElapsedMilliseconds + "" ms"");
  1777. //
  1778. // 3. Use MatUtils.copyFromMat and MatUtils.copyToMat method.
  1779. //
  1780. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1781. watch.Reset();
  1782. watch.Start();
  1783. // Copies an OpenCV Mat data to a pixel data Array.
  1784. byte[] img_array = new byte[imgMat.total() * imgMat.channels()];
  1785. MatUtils.copyFromMat(imgMat, img_array);
  1786. long step0 = imgMat.step1(0);
  1787. long step1 = imgMat.step1(1);
  1788. rows = imgMat.rows();
  1789. cols = imgMat.cols();
  1790. for (int i0 = 0; i0 < rows; i0++)
  1791. {
  1792. for (int i1 = 0; i1 < cols; i1++)
  1793. {
  1794. long p1 = step0 * i0 + step1 * i1;
  1795. long p2 = p1 + 1;
  1796. long p3 = p1 + 2;
  1797. img_array[p1] = (byte)(img_array[p1] + 127); // R
  1798. img_array[p2] = (byte)(img_array[p2] + 127); // G
  1799. img_array[p3] = (byte)(img_array[p3] + 127); // B
  1800. }
  1801. }
  1802. // Copies a pixel data Array to an OpenCV Mat data.
  1803. MatUtils.copyToMat(img_array, imgMat);
  1804. watch.Stop();
  1805. Debug.Log(""3. Use MatUtils.copyFromMat and MatUtils.copyToMat method. time: "" + watch.ElapsedMilliseconds + "" ms"");
  1806. #if OPENCV_USE_UNSAFE_CODE
  1807. //
  1808. // 4. Use pointer access.
  1809. //
  1810. imgMat.setTo(new Scalar(0, 0, 0, 255));
  1811. watch.Reset();
  1812. watch.Start();
  1813. step0 = imgMat.step1(0);
  1814. step1 = imgMat.step1(1);
  1815. long ptrVal = imgMat.dataAddr();
  1816. unsafe
  1817. {
  1818. rows = imgMat.rows();
  1819. cols = imgMat.cols();
  1820. for (int i0 = 0; i0 < rows; i0++)
  1821. {
  1822. for (int i1 = 0; i1 < cols; i1++)
  1823. {
  1824. byte* p1 = (byte*)(ptrVal + (step0 * i0) + (step1 * i1));
  1825. byte* p2 = p1 + 1;
  1826. byte* p3 = p1 + 2;
  1827. *p1 = (byte)(*p1 + 127); // R
  1828. *p2 = (byte)(*p2 + 127); // G
  1829. *p3 = (byte)(*p3 + 127); // B
  1830. }
  1831. }
  1832. }
  1833. watch.Stop();
  1834. Debug.Log(""4. Use pointer access. time: "" + watch.ElapsedMilliseconds + "" ms"");
  1835. #endif
  1836. ";
  1837. UpdateScrollRect();
  1838. }
  1839. }
  1840. }