test_mat.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. // //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //
  41. // //////////////////////////////////////////////////////////////////////////////////////
  42. // Author: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
  43. //
  44. // LICENSE AGREEMENT
  45. // Copyright (c) 2015 The Regents of the University of California (Regents)
  46. //
  47. // Redistribution and use in source and binary forms, with or without
  48. // modification, are permitted provided that the following conditions are met:
  49. // 1. Redistributions of source code must retain the above copyright
  50. // notice, this list of conditions and the following disclaimer.
  51. // 2. Redistributions in binary form must reproduce the above copyright
  52. // notice, this list of conditions and the following disclaimer in the
  53. // documentation and/or other materials provided with the distribution.
  54. // 3. Neither the name of the University nor the
  55. // names of its contributors may be used to endorse or promote products
  56. // derived from this software without specific prior written permission.
  57. //
  58. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
  59. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  60. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  61. // DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
  62. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  63. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  64. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  65. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  66. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  67. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68. //
  69. QUnit.module('CoreMat', {});
  70. QUnit.test('test_mat_creation', function(assert) {
  71. // Mat constructors.
  72. // Mat::Mat(int rows, int cols, int type)
  73. {
  74. let mat = new cv.Mat(10, 20, cv.CV_8UC3);
  75. assert.equal(mat.type(), cv.CV_8UC3);
  76. assert.equal(mat.depth(), cv.CV_8U);
  77. assert.equal(mat.channels(), 3);
  78. assert.ok(mat.empty() === false);
  79. let size = mat.size();
  80. assert.equal(size.height, 10);
  81. assert.equal(size.width, 20);
  82. mat.delete();
  83. }
  84. // Mat::Mat(const Mat &)
  85. {
  86. // Copy from another Mat
  87. let mat1 = new cv.Mat(10, 20, cv.CV_8UC3);
  88. let mat2 = new cv.Mat(mat1);
  89. assert.equal(mat2.type(), mat1.type());
  90. assert.equal(mat2.depth(), mat1.depth());
  91. assert.equal(mat2.channels(), mat1.channels());
  92. assert.equal(mat2.empty(), mat1.empty());
  93. let size1 = mat1.size;
  94. let size2 = mat2.size();
  95. assert.ok(size1[0] === size2[0]);
  96. assert.ok(size1[1] === size2[1]);
  97. mat1.delete();
  98. mat2.delete();
  99. }
  100. // Mat::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
  101. {
  102. // 10 * 10 and one channel
  103. let data = cv._malloc(10 * 10 * 1);
  104. let mat = new cv.Mat(10, 10, cv.CV_8UC1, data, 0);
  105. assert.equal(mat.type(), cv.CV_8UC1);
  106. assert.equal(mat.depth(), cv.CV_8U);
  107. assert.equal(mat.channels(), 1);
  108. assert.ok(mat.empty() === false);
  109. let size = mat.size();
  110. assert.ok(size.height === 10);
  111. assert.ok(size.width === 10);
  112. mat.delete();
  113. }
  114. // Mat::Mat(int rows, int cols, int type, const Scalar& scalar)
  115. {
  116. // 2 * 2 8UC4 mat
  117. let mat = new cv.Mat(2, 2, cv.CV_8UC4, [0, 1, 2, 3]);
  118. for (let r = 0; r < mat.rows; r++) {
  119. for (let c = 0; c < mat.cols; c++) {
  120. let element = mat.ptr(r, c);
  121. assert.equal(element[0], 0);
  122. assert.equal(element[1], 1);
  123. assert.equal(element[2], 2);
  124. assert.equal(element[3], 3);
  125. }
  126. }
  127. mat.delete();
  128. }
  129. // Mat::create(int, int, int)
  130. {
  131. let mat = new cv.Mat();
  132. mat.create(10, 5, cv.CV_8UC3);
  133. let size = mat.size();
  134. assert.ok(mat.type() === cv.CV_8UC3);
  135. assert.ok(size.height === 10);
  136. assert.ok(size.width === 5);
  137. assert.ok(mat.channels() === 3);
  138. mat.delete();
  139. }
  140. // Mat::create(Size, int)
  141. {
  142. let mat = new cv.Mat();
  143. mat.create({height: 10, width: 5}, cv.CV_8UC4);
  144. let size = mat.size();
  145. assert.ok(mat.type() === cv.CV_8UC4);
  146. assert.ok(size.height === 10);
  147. assert.ok(size.width === 5);
  148. assert.ok(mat.channels() === 4);
  149. mat.delete();
  150. }
  151. // clone
  152. {
  153. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  154. let mat2 = mat.clone();
  155. assert.equal(mat.channels, mat2.channels);
  156. assert.equal(mat.size().height, mat2.size().height);
  157. assert.equal(mat.size().width, mat2.size().width);
  158. assert.deepEqual(mat.data, mat2.data);
  159. mat.delete();
  160. mat2.delete();
  161. }
  162. // copyTo
  163. {
  164. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  165. let mat2 = new cv.Mat();
  166. mat.copyTo(mat2);
  167. assert.equal(mat.channels, mat2.channels);
  168. assert.equal(mat.size().height, mat2.size().height);
  169. assert.equal(mat.size().width, mat2.size().width);
  170. assert.deepEqual(mat.data, mat2.data);
  171. mat.delete();
  172. mat2.delete();
  173. }
  174. // copyTo1
  175. {
  176. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  177. let mat2 = new cv.Mat();
  178. let mask = new cv.Mat(5, 5, cv.CV_8UC1, new cv.Scalar(1));
  179. mat.copyTo(mat2, mask);
  180. assert.equal(mat.channels, mat2.channels);
  181. assert.equal(mat.size().height, mat2.size().height);
  182. assert.equal(mat.size().width, mat2.size().width);
  183. assert.deepEqual(mat.data, mat2.data);
  184. mat.delete();
  185. mat2.delete();
  186. mask.delete();
  187. }
  188. // matFromArray
  189. {
  190. let arrayC1 = [0, -1, 2, -3];
  191. let arrayC2 = [0, -1, 2, -3, 4, -5, 6, -7];
  192. let arrayC3 = [0, -1, 2, -3, 4, -5, 6, -7, 9, -9, 10, -11];
  193. let arrayC4 = [0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, 13, 14, 15];
  194. let mat8UC1 = cv.matFromArray(2, 2, cv.CV_8UC1, arrayC1);
  195. let mat8UC2 = cv.matFromArray(2, 2, cv.CV_8UC2, arrayC2);
  196. let mat8UC3 = cv.matFromArray(2, 2, cv.CV_8UC3, arrayC3);
  197. let mat8UC4 = cv.matFromArray(2, 2, cv.CV_8UC4, arrayC4);
  198. let mat8SC1 = cv.matFromArray(2, 2, cv.CV_8SC1, arrayC1);
  199. let mat8SC2 = cv.matFromArray(2, 2, cv.CV_8SC2, arrayC2);
  200. let mat8SC3 = cv.matFromArray(2, 2, cv.CV_8SC3, arrayC3);
  201. let mat8SC4 = cv.matFromArray(2, 2, cv.CV_8SC4, arrayC4);
  202. let mat16UC1 = cv.matFromArray(2, 2, cv.CV_16UC1, arrayC1);
  203. let mat16UC2 = cv.matFromArray(2, 2, cv.CV_16UC2, arrayC2);
  204. let mat16UC3 = cv.matFromArray(2, 2, cv.CV_16UC3, arrayC3);
  205. let mat16UC4 = cv.matFromArray(2, 2, cv.CV_16UC4, arrayC4);
  206. let mat16SC1 = cv.matFromArray(2, 2, cv.CV_16SC1, arrayC1);
  207. let mat16SC2 = cv.matFromArray(2, 2, cv.CV_16SC2, arrayC2);
  208. let mat16SC3 = cv.matFromArray(2, 2, cv.CV_16SC3, arrayC3);
  209. let mat16SC4 = cv.matFromArray(2, 2, cv.CV_16SC4, arrayC4);
  210. let mat32SC1 = cv.matFromArray(2, 2, cv.CV_32SC1, arrayC1);
  211. let mat32SC2 = cv.matFromArray(2, 2, cv.CV_32SC2, arrayC2);
  212. let mat32SC3 = cv.matFromArray(2, 2, cv.CV_32SC3, arrayC3);
  213. let mat32SC4 = cv.matFromArray(2, 2, cv.CV_32SC4, arrayC4);
  214. let mat32FC1 = cv.matFromArray(2, 2, cv.CV_32FC1, arrayC1);
  215. let mat32FC2 = cv.matFromArray(2, 2, cv.CV_32FC2, arrayC2);
  216. let mat32FC3 = cv.matFromArray(2, 2, cv.CV_32FC3, arrayC3);
  217. let mat32FC4 = cv.matFromArray(2, 2, cv.CV_32FC4, arrayC4);
  218. let mat64FC1 = cv.matFromArray(2, 2, cv.CV_64FC1, arrayC1);
  219. let mat64FC2 = cv.matFromArray(2, 2, cv.CV_64FC2, arrayC2);
  220. let mat64FC3 = cv.matFromArray(2, 2, cv.CV_64FC3, arrayC3);
  221. let mat64FC4 = cv.matFromArray(2, 2, cv.CV_64FC4, arrayC4);
  222. assert.deepEqual(mat8UC1.data, new Uint8Array(arrayC1));
  223. assert.deepEqual(mat8UC2.data, new Uint8Array(arrayC2));
  224. assert.deepEqual(mat8UC3.data, new Uint8Array(arrayC3));
  225. assert.deepEqual(mat8UC4.data, new Uint8Array(arrayC4));
  226. assert.deepEqual(mat8SC1.data8S, new Int8Array(arrayC1));
  227. assert.deepEqual(mat8SC2.data8S, new Int8Array(arrayC2));
  228. assert.deepEqual(mat8SC3.data8S, new Int8Array(arrayC3));
  229. assert.deepEqual(mat8SC4.data8S, new Int8Array(arrayC4));
  230. assert.deepEqual(mat16UC1.data16U, new Uint16Array(arrayC1));
  231. assert.deepEqual(mat16UC2.data16U, new Uint16Array(arrayC2));
  232. assert.deepEqual(mat16UC3.data16U, new Uint16Array(arrayC3));
  233. assert.deepEqual(mat16UC4.data16U, new Uint16Array(arrayC4));
  234. assert.deepEqual(mat16SC1.data16S, new Int16Array(arrayC1));
  235. assert.deepEqual(mat16SC2.data16S, new Int16Array(arrayC2));
  236. assert.deepEqual(mat16SC3.data16S, new Int16Array(arrayC3));
  237. assert.deepEqual(mat16SC4.data16S, new Int16Array(arrayC4));
  238. assert.deepEqual(mat32SC1.data32S, new Int32Array(arrayC1));
  239. assert.deepEqual(mat32SC2.data32S, new Int32Array(arrayC2));
  240. assert.deepEqual(mat32SC3.data32S, new Int32Array(arrayC3));
  241. assert.deepEqual(mat32SC4.data32S, new Int32Array(arrayC4));
  242. assert.deepEqual(mat32FC1.data32F, new Float32Array(arrayC1));
  243. assert.deepEqual(mat32FC2.data32F, new Float32Array(arrayC2));
  244. assert.deepEqual(mat32FC3.data32F, new Float32Array(arrayC3));
  245. assert.deepEqual(mat32FC4.data32F, new Float32Array(arrayC4));
  246. assert.deepEqual(mat64FC1.data64F, new Float64Array(arrayC1));
  247. assert.deepEqual(mat64FC2.data64F, new Float64Array(arrayC2));
  248. assert.deepEqual(mat64FC3.data64F, new Float64Array(arrayC3));
  249. assert.deepEqual(mat64FC4.data64F, new Float64Array(arrayC4));
  250. mat8UC1.delete();
  251. mat8UC2.delete();
  252. mat8UC3.delete();
  253. mat8UC4.delete();
  254. mat8SC1.delete();
  255. mat8SC2.delete();
  256. mat8SC3.delete();
  257. mat8SC4.delete();
  258. mat16UC1.delete();
  259. mat16UC2.delete();
  260. mat16UC3.delete();
  261. mat16UC4.delete();
  262. mat16SC1.delete();
  263. mat16SC2.delete();
  264. mat16SC3.delete();
  265. mat16SC4.delete();
  266. mat32SC1.delete();
  267. mat32SC2.delete();
  268. mat32SC3.delete();
  269. mat32SC4.delete();
  270. mat32FC1.delete();
  271. mat32FC2.delete();
  272. mat32FC3.delete();
  273. mat32FC4.delete();
  274. mat64FC1.delete();
  275. mat64FC2.delete();
  276. mat64FC3.delete();
  277. mat64FC4.delete();
  278. }
  279. // matFromImageData
  280. {
  281. // Only test in browser
  282. if (typeof window === 'undefined') {
  283. return;
  284. }
  285. let canvas = window.document.createElement('canvas');
  286. canvas.width = 2;
  287. canvas.height = 2;
  288. let ctx = canvas.getContext('2d');
  289. ctx.fillStyle='#FF0000';
  290. ctx.fillRect(0, 0, 1, 1);
  291. ctx.fillRect(1, 1, 1, 1);
  292. let imageData = ctx.getImageData(0, 0, 2, 2);
  293. let mat = cv.matFromImageData(imageData);
  294. assert.deepEqual(mat.data, new Uint8Array(imageData.data));
  295. mat.delete();
  296. }
  297. // Mat(mat)
  298. {
  299. let mat = new cv.Mat(2, 2, cv.CV_8UC4, new cv.Scalar(1, 0, 1, 0));
  300. let mat1 = new cv.Mat(mat);
  301. let mat2 = mat;
  302. assert.equal(mat.rows, mat1.rows);
  303. assert.equal(mat.cols, mat1.cols);
  304. assert.equal(mat.type(), mat1.type());
  305. assert.deepEqual(mat.data, mat1.data);
  306. mat.delete();
  307. assert.equal(mat1.isDeleted(), false);
  308. assert.equal(mat2.isDeleted(), true);
  309. mat1.delete();
  310. }
  311. // mat.setTo
  312. {
  313. let mat = new cv.Mat(2, 2, cv.CV_8UC4);
  314. let s = [0, 1, 2, 3];
  315. mat.setTo(s);
  316. assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s));
  317. assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
  318. assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s));
  319. assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));
  320. let s1 = [0, 0, 0, 0];
  321. mat.setTo(s1);
  322. let mask = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 0, 1]);
  323. mat.setTo(s, mask);
  324. assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s1));
  325. assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
  326. assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s1));
  327. assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));
  328. mat.delete();
  329. mask.delete();
  330. }
  331. });
  332. QUnit.test('test_mat_ptr', function(assert) {
  333. const RValue = 3;
  334. const GValue = 7;
  335. const BValue = 197;
  336. // cv.CV_8UC1 + Mat::ptr(int).
  337. {
  338. let mat = new cv.Mat(10, 10, cv.CV_8UC1);
  339. let view = mat.data;
  340. // Alter matrix[2, 1].
  341. let step = 10;
  342. view[2 * step + 1] = RValue;
  343. // Access matrix[2, 1].
  344. view = mat.ptr(2);
  345. assert.equal(view[1], RValue);
  346. mat.delete();
  347. }
  348. // cv.CV_8UC3 + Mat::ptr(int).
  349. {
  350. let mat = new cv.Mat(10, 10, cv.CV_8UC3);
  351. let view = mat.data;
  352. // Alter matrix[2, 1].
  353. let step = 3 * 10;
  354. view[2 * step + 3] = RValue;
  355. view[2 * step + 3 + 1] = GValue;
  356. view[2 * step + 3 + 2] = BValue;
  357. // Access matrix[2, 1].
  358. view = mat.ptr(2);
  359. assert.equal(view[3], RValue);
  360. assert.equal(view[3 + 1], GValue);
  361. assert.equal(view[3 + 2], BValue);
  362. mat.delete();
  363. }
  364. // cv.CV_8UC3 + Mat::ptr(int, int).
  365. {
  366. let mat = new cv.Mat(10, 10, cv.CV_8UC3);
  367. let view = mat.data;
  368. // Alter matrix[2, 1].
  369. let step = 3 * 10;
  370. view[2 * step + 3] = RValue;
  371. view[2 * step + 3 + 1] = GValue;
  372. view[2 * step + 3 + 2] = BValue;
  373. // Access matrix[2, 1].
  374. view = mat.ptr(2, 1);
  375. assert.equal(view[0], RValue);
  376. assert.equal(view[1], GValue);
  377. assert.equal(view[2], BValue);
  378. mat.delete();
  379. }
  380. const RValueF32 = 3.3;
  381. const GValueF32 = 7.3;
  382. const BValueF32 = 197.3;
  383. const EPSILON = 0.001;
  384. // cv.CV_32FC1 + Mat::ptr(int).
  385. {
  386. let mat = new cv.Mat(10, 10, cv.CV_32FC1);
  387. let view = mat.data32F;
  388. // Alter matrix[2, 1].
  389. let step = 10;
  390. view[2 * step + 1] = RValueF32;
  391. // Access matrix[2, 1].
  392. view = mat.floatPtr(2);
  393. assert.ok(Math.abs(view[1] - RValueF32) < EPSILON);
  394. mat.delete();
  395. }
  396. // cv.CV_32FC3 + Mat::ptr(int).
  397. {
  398. let mat = new cv.Mat(10, 10, cv.CV_32FC3);
  399. let view = mat.data32F;
  400. // Alter matrix[2, 1].
  401. let step = mat.step1(0);
  402. view[2 * step + 3] = RValueF32;
  403. view[2 * step + 3 + 1] = GValueF32;
  404. view[2 * step + 3 + 2] = BValueF32;
  405. // Access matrix[2, 1].
  406. view = mat.floatPtr(2);
  407. assert.ok(Math.abs(view[3] - RValueF32) < EPSILON);
  408. assert.ok(Math.abs(view[3 + 1] - GValueF32) < EPSILON);
  409. assert.ok(Math.abs(view[3 + 2] - BValueF32) < EPSILON);
  410. mat.delete();
  411. }
  412. // cv.CV_32FC3 + Mat::ptr(int, int).
  413. {
  414. let mat = new cv.Mat(10, 10, cv.CV_32FC3);
  415. let view = mat.data32F;
  416. // Alter matrix[2, 1].
  417. let step = mat.step1(0);
  418. view[2 * step + 3] = RValueF32;
  419. view[2 * step + 3 + 1] = GValueF32;
  420. view[2 * step + 3 + 2] = BValueF32;
  421. // Access matrix[2, 1].
  422. view = mat.floatPtr(2, 1);
  423. assert.ok(Math.abs(view[0] - RValueF32) < EPSILON);
  424. assert.ok(Math.abs(view[1] - GValueF32) < EPSILON);
  425. assert.ok(Math.abs(view[2] - BValueF32) < EPSILON);
  426. mat.delete();
  427. }
  428. });
  429. QUnit.test('test_mat_zeros', function(assert) {
  430. let zeros = new Uint8Array(10*10).fill(0);
  431. // Mat::zeros(int, int, int)
  432. {
  433. let mat = cv.Mat.zeros(10, 10, cv.CV_8UC1);
  434. let view = mat.data;
  435. assert.deepEqual(view, zeros);
  436. mat.delete();
  437. }
  438. // Mat::zeros(Size, int)
  439. {
  440. let mat = cv.Mat.zeros({height: 10, width: 10}, cv.CV_8UC1);
  441. let view = mat.data;
  442. assert.deepEqual(view, zeros);
  443. mat.delete();
  444. }
  445. });
  446. QUnit.test('test_mat_ones', function(assert) {
  447. let ones = new Uint8Array(10*10).fill(1);
  448. // Mat::ones(int, int, int)
  449. {
  450. let mat = cv.Mat.ones(10, 10, cv.CV_8UC1);
  451. let view = mat.data;
  452. assert.deepEqual(view, ones);
  453. }
  454. // Mat::ones(Size, int)
  455. {
  456. let mat = cv.Mat.ones({height: 10, width: 10}, cv.CV_8UC1);
  457. let view = mat.data;
  458. assert.deepEqual(view, ones);
  459. }
  460. });
  461. QUnit.test('test_mat_eye', function(assert) {
  462. let eye4by4 = new Uint8Array([1, 0, 0, 0,
  463. 0, 1, 0, 0,
  464. 0, 0, 1, 0,
  465. 0, 0, 0, 1]);
  466. // Mat::eye(int, int, int)
  467. {
  468. let mat = cv.Mat.eye(4, 4, cv.CV_8UC1);
  469. let view = mat.data;
  470. assert.deepEqual(view, eye4by4);
  471. }
  472. // Mat::eye(Size, int)
  473. {
  474. let mat = cv.Mat.eye({height: 4, width: 4}, cv.CV_8UC1);
  475. let view = mat.data;
  476. assert.deepEqual(view, eye4by4);
  477. }
  478. });
  479. QUnit.test('test_mat_miscs', function(assert) {
  480. // Mat::col(int)
  481. {
  482. let mat = cv.matFromArray(2, 2, cv.CV_8UC2, [1, 2, 3, 4, 5, 6, 7, 8]);
  483. let col = mat.col(1);
  484. assert.equal(col.isContinuous(), false);
  485. assert.equal(col.ptr(0, 0)[0], 3);
  486. assert.equal(col.ptr(0, 0)[1], 4);
  487. assert.equal(col.ptr(1, 0)[0], 7);
  488. assert.equal(col.ptr(1, 0)[1], 8);
  489. col.delete();
  490. mat.delete();
  491. }
  492. // Mat::row(int)
  493. {
  494. let mat = cv.Mat.zeros(5, 5, cv.CV_8UC2);
  495. let row = mat.row(1);
  496. let view = row.data;
  497. assert.equal(view[0], 0);
  498. assert.equal(view[4], 0);
  499. row.delete();
  500. mat.delete();
  501. }
  502. // Mat::convertTo(Mat, int, double, double)
  503. {
  504. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  505. let grayMat = cv.Mat.zeros(5, 5, cv.CV_8UC1);
  506. mat.convertTo(grayMat, cv.CV_8U, 2, 1);
  507. // dest = 2 * source(x, y) + 1.
  508. let view = grayMat.data;
  509. assert.equal(view[0], (1 * 2) + 1);
  510. mat.convertTo(grayMat, cv.CV_8U);
  511. // dest = 1 * source(x, y) + 0.
  512. assert.equal(view[0], 1);
  513. mat.convertTo(grayMat, cv.CV_8U, 2);
  514. // dest = 2 * source(x, y) + 0.
  515. assert.equal(view[0], 2);
  516. grayMat.delete();
  517. mat.delete();
  518. }
  519. // split
  520. {
  521. const R =7;
  522. const G =13;
  523. const B =29;
  524. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  525. let view = mat.data;
  526. view[0] = R;
  527. view[1] = G;
  528. view[2] = B;
  529. let bgrPlanes = new cv.MatVector();
  530. cv.split(mat, bgrPlanes);
  531. assert.equal(bgrPlanes.size(), 3);
  532. let rMat = bgrPlanes.get(0);
  533. view = rMat.data;
  534. assert.equal(view[0], R);
  535. let gMat = bgrPlanes.get(1);
  536. view = gMat.data;
  537. assert.equal(view[0], G);
  538. let bMat = bgrPlanes.get(2);
  539. view = bMat.data;
  540. assert.equal(view[0], B);
  541. mat.delete();
  542. rMat.delete();
  543. gMat.delete();
  544. bgrPlanes.delete();
  545. bMat.delete();
  546. }
  547. // elemSize
  548. {
  549. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  550. assert.equal(mat.elemSize(), 3);
  551. assert.equal(mat.elemSize1(), 1);
  552. let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
  553. assert.equal(mat2.elemSize(), 1);
  554. assert.equal(mat2.elemSize1(), 1);
  555. let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
  556. assert.equal(mat3.elemSize(), 2 * 3);
  557. assert.equal(mat3.elemSize1(), 2);
  558. mat.delete();
  559. mat2.delete();
  560. mat3.delete();
  561. }
  562. // step
  563. {
  564. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  565. assert.equal(mat.step[0], 15);
  566. assert.equal(mat.step[1], 3);
  567. let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
  568. assert.equal(mat2.step[0], 5);
  569. assert.equal(mat2.step[1], 1);
  570. let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
  571. assert.equal(mat3.step[0], 30);
  572. assert.equal(mat3.step[1], 6);
  573. mat.delete();
  574. mat2.delete();
  575. mat3.delete();
  576. }
  577. // dot
  578. {
  579. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  580. let mat2 = cv.Mat.eye(5, 5, cv.CV_8UC1);
  581. assert.equal(mat.dot(mat), 25);
  582. assert.equal(mat.dot(mat2), 5);
  583. assert.equal(mat2.dot(mat2), 5);
  584. mat.delete();
  585. mat2.delete();
  586. }
  587. // mul
  588. {
  589. const FACTOR = 5;
  590. let mat = cv.Mat.ones(4, 4, cv.CV_8UC1);
  591. let mat2 = cv.Mat.eye(4, 4, cv.CV_8UC1);
  592. let expected = new Uint8Array([FACTOR, 0, 0, 0,
  593. 0, FACTOR, 0, 0,
  594. 0, 0, FACTOR, 0,
  595. 0, 0, 0, FACTOR]);
  596. let mat3 = mat.mul(mat2, FACTOR);
  597. assert.deepEqual(mat3.data, expected);
  598. mat.delete();
  599. mat2.delete();
  600. mat3.delete();
  601. }
  602. });
  603. QUnit.test('test mat access', function(assert) {
  604. // test memory view
  605. {
  606. let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
  607. let dataPtr = cv._malloc(8);
  608. let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
  609. dataHeap.set(new Uint8Array(data.buffer));
  610. let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);
  611. let unsignedCharView = new Uint8Array(data.buffer);
  612. let charView = new Int8Array(data.buffer);
  613. let shortView = new Int16Array(data.buffer);
  614. let unsignedShortView = new Uint16Array(data.buffer);
  615. let intView = new Int32Array(data.buffer);
  616. let float32View = new Float32Array(data.buffer);
  617. let float64View = new Float64Array(data.buffer);
  618. assert.deepEqual(unsignedCharView, mat.data);
  619. assert.deepEqual(charView, mat.data8S);
  620. assert.deepEqual(shortView, mat.data16S);
  621. assert.deepEqual(unsignedShortView, mat.data16U);
  622. assert.deepEqual(intView, mat.data32S);
  623. assert.deepEqual(float32View, mat.data32F);
  624. assert.deepEqual(float64View, mat.data64F);
  625. }
  626. // test ucharAt(i)
  627. {
  628. let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
  629. let dataPtr = cv._malloc(8);
  630. let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
  631. dataHeap.set(new Uint8Array(data.buffer));
  632. let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);
  633. assert.equal(mat.ucharAt(0), 0);
  634. assert.equal(mat.ucharAt(1), 0);
  635. assert.equal(mat.ucharAt(2), 0);
  636. assert.equal(mat.ucharAt(3), 255);
  637. assert.equal(mat.ucharAt(4), 0);
  638. assert.equal(mat.ucharAt(5), 1);
  639. assert.equal(mat.ucharAt(6), 2);
  640. assert.equal(mat.ucharAt(7), 3);
  641. }
  642. // test ushortAt(i)
  643. {
  644. let data = new Uint16Array([0, 1000, 65000, 255, 0, 1, 2, 3]);
  645. let dataPtr = cv._malloc(16);
  646. let dataHeap = new Uint16Array(cv.HEAPU8.buffer, dataPtr, 8);
  647. dataHeap.set(new Uint16Array(data.buffer));
  648. let mat = new cv.Mat(8, 1, cv.CV_16SC1, dataPtr, 0);
  649. assert.equal(mat.ushortAt(0), 0);
  650. assert.equal(mat.ushortAt(1), 1000);
  651. assert.equal(mat.ushortAt(2), 65000);
  652. assert.equal(mat.ushortAt(3), 255);
  653. assert.equal(mat.ushortAt(4), 0);
  654. assert.equal(mat.ushortAt(5), 1);
  655. assert.equal(mat.ushortAt(6), 2);
  656. assert.equal(mat.ushortAt(7), 3);
  657. }
  658. // test intAt(i)
  659. {
  660. let data = new Int32Array([0, -1000, 65000, 255, -2000000, -1, 2, 3]);
  661. let dataPtr = cv._malloc(32);
  662. let dataHeap = new Int32Array(cv.HEAPU32.buffer, dataPtr, 8);
  663. dataHeap.set(new Int32Array(data.buffer));
  664. let mat = new cv.Mat(8, 1, cv.CV_32SC1, dataPtr, 0);
  665. assert.equal(mat.intAt(0), 0);
  666. assert.equal(mat.intAt(1), -1000);
  667. assert.equal(mat.intAt(2), 65000);
  668. assert.equal(mat.intAt(3), 255);
  669. assert.equal(mat.intAt(4), -2000000);
  670. assert.equal(mat.intAt(5), -1);
  671. assert.equal(mat.intAt(6), 2);
  672. assert.equal(mat.intAt(7), 3);
  673. }
  674. // test floatAt(i)
  675. {
  676. const EPSILON = 0.001;
  677. let data = new Float32Array([0, -10.5, 650.001, 255, -20.1, -1.2, 2, 3.5]);
  678. let dataPtr = cv._malloc(32);
  679. let dataHeap = new Float32Array(cv.HEAPU32.buffer, dataPtr, 8);
  680. dataHeap.set(new Float32Array(data.buffer));
  681. let mat = new cv.Mat(8, 1, cv.CV_32FC1, dataPtr, 0);
  682. assert.equal(Math.abs(mat.floatAt(0)-0) < EPSILON, true);
  683. assert.equal(Math.abs(mat.floatAt(1)+10.5) < EPSILON, true);
  684. assert.equal(Math.abs(mat.floatAt(2)-650.001) < EPSILON, true);
  685. assert.equal(Math.abs(mat.floatAt(3)-255) < EPSILON, true);
  686. assert.equal(Math.abs(mat.floatAt(4)+20.1) < EPSILON, true);
  687. assert.equal(Math.abs(mat.floatAt(5)+1.2) < EPSILON, true);
  688. assert.equal(Math.abs(mat.floatAt(6)-2) < EPSILON, true);
  689. assert.equal(Math.abs(mat.floatAt(7)-3.5) < EPSILON, true);
  690. }
  691. // test intAt(i,j)
  692. {
  693. let mat = cv.Mat.eye({height: 3, width: 3}, cv.CV_32SC1);
  694. assert.equal(mat.intAt(0, 0), 1);
  695. assert.equal(mat.intAt(0, 1), 0);
  696. assert.equal(mat.intAt(0, 2), 0);
  697. assert.equal(mat.intAt(1, 0), 0);
  698. assert.equal(mat.intAt(1, 1), 1);
  699. assert.equal(mat.intAt(1, 2), 0);
  700. assert.equal(mat.intAt(2, 0), 0);
  701. assert.equal(mat.intAt(2, 1), 0);
  702. assert.equal(mat.intAt(2, 2), 1);
  703. mat.delete();
  704. }
  705. });
  706. QUnit.test('test_mat_operations', function(assert) {
  707. // test minMaxLoc
  708. {
  709. let src = cv.Mat.ones(4, 4, cv.CV_8UC1);
  710. src.data[2] = 0;
  711. src.data[5] = 2;
  712. let result = cv.minMaxLoc(src);
  713. assert.equal(result.minVal, 0);
  714. assert.equal(result.maxVal, 2);
  715. assert.deepEqual(result.minLoc, {x: 2, y: 0});
  716. assert.deepEqual(result.maxLoc, {x: 1, y: 1});
  717. src.delete();
  718. }
  719. });
  720. QUnit.test('test_mat_roi', function(assert) {
  721. // test minMaxLoc
  722. {
  723. let mat = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
  724. let roi = mat.roi(new cv.Rect(1, 1, 1, 1));
  725. assert.equal(roi.rows, 1);
  726. assert.equal(roi.cols, 1);
  727. assert.deepEqual(roi.data, new Uint8Array([mat.ucharAt(1, 1)]));
  728. mat.delete();
  729. roi.delete();
  730. }
  731. });
  732. QUnit.test('test_mat_range', function(assert) {
  733. {
  734. let src = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
  735. let mat = src.colRange(0, 1);
  736. assert.equal(mat.isContinuous(), false);
  737. assert.equal(mat.rows, 2);
  738. assert.equal(mat.cols, 1);
  739. assert.equal(mat.ucharAt(0), 0);
  740. assert.equal(mat.ucharAt(1), 2);
  741. mat.delete();
  742. mat = src.colRange({start: 0, end: 1});
  743. assert.equal(mat.isContinuous(), false);
  744. assert.equal(mat.rows, 2);
  745. assert.equal(mat.cols, 1);
  746. assert.equal(mat.ucharAt(0), 0);
  747. assert.equal(mat.ucharAt(1), 2);
  748. mat.delete();
  749. mat = src.rowRange(1, 2);
  750. assert.equal(mat.rows, 1);
  751. assert.equal(mat.cols, 2);
  752. assert.deepEqual(mat.data, new Uint8Array([2, 3]));
  753. mat.delete();
  754. mat = src.rowRange({start: 1, end: 2});
  755. assert.equal(mat.rows, 1);
  756. assert.equal(mat.cols, 2);
  757. assert.deepEqual(mat.data, new Uint8Array([2, 3]));
  758. mat.delete();
  759. src.delete();
  760. }
  761. });
  762. QUnit.test('test_mat_diag', function(assert) {
  763. // test diag
  764. {
  765. let mat = cv.matFromArray(3, 3, cv.CV_8UC1, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
  766. let d = mat.diag();
  767. let d1 = mat.diag(1);
  768. let d2 = mat.diag(-1);
  769. assert.equal(mat.isContinuous(), true);
  770. assert.equal(d.isContinuous(), false);
  771. assert.equal(d1.isContinuous(), false);
  772. assert.equal(d2.isContinuous(), false);
  773. assert.equal(d.ucharAt(0), 0);
  774. assert.equal(d.ucharAt(1), 4);
  775. assert.equal(d.ucharAt(2), 8);
  776. assert.equal(d1.ucharAt(0), 1);
  777. assert.equal(d1.ucharAt(1), 5);
  778. assert.equal(d2.ucharAt(0), 3);
  779. assert.equal(d2.ucharAt(1), 7);
  780. mat.delete();
  781. d.delete();
  782. d1.delete();
  783. d2.delete();
  784. }
  785. });