MatBasicProcessingExample.cs 64 KB

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