test_mat.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. if (typeof module !== 'undefined' && module.exports) {
  70. // The environment is Node.js
  71. var cv = require('./opencv.js'); // eslint-disable-line no-var
  72. }
  73. QUnit.module('Core', {});
  74. QUnit.test('test_mat_creation', function(assert) {
  75. // Mat constructors.
  76. // Mat::Mat(int rows, int cols, int type)
  77. {
  78. let mat = new cv.Mat(10, 20, cv.CV_8UC3);
  79. assert.equal(mat.type(), cv.CV_8UC3);
  80. assert.equal(mat.depth(), cv.CV_8U);
  81. assert.equal(mat.channels(), 3);
  82. assert.ok(mat.empty() === false);
  83. let size = mat.size();
  84. assert.equal(size.height, 10);
  85. assert.equal(size.width, 20);
  86. mat.delete();
  87. }
  88. // Mat::Mat(const Mat &)
  89. {
  90. // Copy from another Mat
  91. let mat1 = new cv.Mat(10, 20, cv.CV_8UC3);
  92. let mat2 = new cv.Mat(mat1);
  93. assert.equal(mat2.type(), mat1.type());
  94. assert.equal(mat2.depth(), mat1.depth());
  95. assert.equal(mat2.channels(), mat1.channels());
  96. assert.equal(mat2.empty(), mat1.empty());
  97. let size1 = mat1.size;
  98. let size2 = mat2.size();
  99. assert.ok(size1[0] === size2[0]);
  100. assert.ok(size1[1] === size2[1]);
  101. mat1.delete();
  102. mat2.delete();
  103. }
  104. // Mat::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
  105. {
  106. // 10 * 10 and one channel
  107. let data = cv._malloc(10 * 10 * 1);
  108. let mat = new cv.Mat(10, 10, cv.CV_8UC1, data, 0);
  109. assert.equal(mat.type(), cv.CV_8UC1);
  110. assert.equal(mat.depth(), cv.CV_8U);
  111. assert.equal(mat.channels(), 1);
  112. assert.ok(mat.empty() === false);
  113. let size = mat.size();
  114. assert.ok(size.height === 10);
  115. assert.ok(size.width === 10);
  116. mat.delete();
  117. }
  118. // Mat::Mat(int rows, int cols, int type, const Scalar& scalar)
  119. {
  120. // 2 * 2 8UC4 mat
  121. let mat = new cv.Mat(2, 2, cv.CV_8UC4, [0, 1, 2, 3]);
  122. for (let r = 0; r < mat.rows; r++) {
  123. for (let c = 0; c < mat.cols; c++) {
  124. let element = mat.ptr(r, c);
  125. assert.equal(element[0], 0);
  126. assert.equal(element[1], 1);
  127. assert.equal(element[2], 2);
  128. assert.equal(element[3], 3);
  129. }
  130. }
  131. mat.delete();
  132. }
  133. // Mat::create(int, int, int)
  134. {
  135. let mat = new cv.Mat();
  136. mat.create(10, 5, cv.CV_8UC3);
  137. let size = mat.size();
  138. assert.ok(mat.type() === cv.CV_8UC3);
  139. assert.ok(size.height === 10);
  140. assert.ok(size.width === 5);
  141. assert.ok(mat.channels() === 3);
  142. mat.delete();
  143. }
  144. // Mat::create(Size, int)
  145. {
  146. let mat = new cv.Mat();
  147. mat.create({height: 10, width: 5}, cv.CV_8UC4);
  148. let size = mat.size();
  149. assert.ok(mat.type() === cv.CV_8UC4);
  150. assert.ok(size.height === 10);
  151. assert.ok(size.width === 5);
  152. assert.ok(mat.channels() === 4);
  153. mat.delete();
  154. }
  155. // clone
  156. {
  157. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  158. let mat2 = mat.clone();
  159. assert.equal(mat.channels, mat2.channels);
  160. assert.equal(mat.size().height, mat2.size().height);
  161. assert.equal(mat.size().width, mat2.size().width);
  162. assert.deepEqual(mat.data, mat2.data);
  163. mat.delete();
  164. mat2.delete();
  165. }
  166. // copyTo
  167. {
  168. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  169. let mat2 = new cv.Mat();
  170. mat.copyTo(mat2);
  171. assert.equal(mat.channels, mat2.channels);
  172. assert.equal(mat.size().height, mat2.size().height);
  173. assert.equal(mat.size().width, mat2.size().width);
  174. assert.deepEqual(mat.data, mat2.data);
  175. mat.delete();
  176. mat2.delete();
  177. }
  178. // copyTo1
  179. {
  180. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  181. let mat2 = new cv.Mat();
  182. let mask = new cv.Mat(5, 5, cv.CV_8UC1, new cv.Scalar(1));
  183. mat.copyTo(mat2, mask);
  184. assert.equal(mat.channels, mat2.channels);
  185. assert.equal(mat.size().height, mat2.size().height);
  186. assert.equal(mat.size().width, mat2.size().width);
  187. assert.deepEqual(mat.data, mat2.data);
  188. mat.delete();
  189. mat2.delete();
  190. mask.delete();
  191. }
  192. // matFromArray
  193. {
  194. let arrayC1 = [0, -1, 2, -3];
  195. let arrayC2 = [0, -1, 2, -3, 4, -5, 6, -7];
  196. let arrayC3 = [0, -1, 2, -3, 4, -5, 6, -7, 9, -9, 10, -11];
  197. let arrayC4 = [0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, 13, 14, 15];
  198. let mat8UC1 = cv.matFromArray(2, 2, cv.CV_8UC1, arrayC1);
  199. let mat8UC2 = cv.matFromArray(2, 2, cv.CV_8UC2, arrayC2);
  200. let mat8UC3 = cv.matFromArray(2, 2, cv.CV_8UC3, arrayC3);
  201. let mat8UC4 = cv.matFromArray(2, 2, cv.CV_8UC4, arrayC4);
  202. let mat8SC1 = cv.matFromArray(2, 2, cv.CV_8SC1, arrayC1);
  203. let mat8SC2 = cv.matFromArray(2, 2, cv.CV_8SC2, arrayC2);
  204. let mat8SC3 = cv.matFromArray(2, 2, cv.CV_8SC3, arrayC3);
  205. let mat8SC4 = cv.matFromArray(2, 2, cv.CV_8SC4, arrayC4);
  206. let mat16UC1 = cv.matFromArray(2, 2, cv.CV_16UC1, arrayC1);
  207. let mat16UC2 = cv.matFromArray(2, 2, cv.CV_16UC2, arrayC2);
  208. let mat16UC3 = cv.matFromArray(2, 2, cv.CV_16UC3, arrayC3);
  209. let mat16UC4 = cv.matFromArray(2, 2, cv.CV_16UC4, arrayC4);
  210. let mat16SC1 = cv.matFromArray(2, 2, cv.CV_16SC1, arrayC1);
  211. let mat16SC2 = cv.matFromArray(2, 2, cv.CV_16SC2, arrayC2);
  212. let mat16SC3 = cv.matFromArray(2, 2, cv.CV_16SC3, arrayC3);
  213. let mat16SC4 = cv.matFromArray(2, 2, cv.CV_16SC4, arrayC4);
  214. let mat32SC1 = cv.matFromArray(2, 2, cv.CV_32SC1, arrayC1);
  215. let mat32SC2 = cv.matFromArray(2, 2, cv.CV_32SC2, arrayC2);
  216. let mat32SC3 = cv.matFromArray(2, 2, cv.CV_32SC3, arrayC3);
  217. let mat32SC4 = cv.matFromArray(2, 2, cv.CV_32SC4, arrayC4);
  218. let mat32FC1 = cv.matFromArray(2, 2, cv.CV_32FC1, arrayC1);
  219. let mat32FC2 = cv.matFromArray(2, 2, cv.CV_32FC2, arrayC2);
  220. let mat32FC3 = cv.matFromArray(2, 2, cv.CV_32FC3, arrayC3);
  221. let mat32FC4 = cv.matFromArray(2, 2, cv.CV_32FC4, arrayC4);
  222. let mat64FC1 = cv.matFromArray(2, 2, cv.CV_64FC1, arrayC1);
  223. let mat64FC2 = cv.matFromArray(2, 2, cv.CV_64FC2, arrayC2);
  224. let mat64FC3 = cv.matFromArray(2, 2, cv.CV_64FC3, arrayC3);
  225. let mat64FC4 = cv.matFromArray(2, 2, cv.CV_64FC4, arrayC4);
  226. assert.deepEqual(mat8UC1.data, new Uint8Array(arrayC1));
  227. assert.deepEqual(mat8UC2.data, new Uint8Array(arrayC2));
  228. assert.deepEqual(mat8UC3.data, new Uint8Array(arrayC3));
  229. assert.deepEqual(mat8UC4.data, new Uint8Array(arrayC4));
  230. assert.deepEqual(mat8SC1.data8S, new Int8Array(arrayC1));
  231. assert.deepEqual(mat8SC2.data8S, new Int8Array(arrayC2));
  232. assert.deepEqual(mat8SC3.data8S, new Int8Array(arrayC3));
  233. assert.deepEqual(mat8SC4.data8S, new Int8Array(arrayC4));
  234. assert.deepEqual(mat16UC1.data16U, new Uint16Array(arrayC1));
  235. assert.deepEqual(mat16UC2.data16U, new Uint16Array(arrayC2));
  236. assert.deepEqual(mat16UC3.data16U, new Uint16Array(arrayC3));
  237. assert.deepEqual(mat16UC4.data16U, new Uint16Array(arrayC4));
  238. assert.deepEqual(mat16SC1.data16S, new Int16Array(arrayC1));
  239. assert.deepEqual(mat16SC2.data16S, new Int16Array(arrayC2));
  240. assert.deepEqual(mat16SC3.data16S, new Int16Array(arrayC3));
  241. assert.deepEqual(mat16SC4.data16S, new Int16Array(arrayC4));
  242. assert.deepEqual(mat32SC1.data32S, new Int32Array(arrayC1));
  243. assert.deepEqual(mat32SC2.data32S, new Int32Array(arrayC2));
  244. assert.deepEqual(mat32SC3.data32S, new Int32Array(arrayC3));
  245. assert.deepEqual(mat32SC4.data32S, new Int32Array(arrayC4));
  246. assert.deepEqual(mat32FC1.data32F, new Float32Array(arrayC1));
  247. assert.deepEqual(mat32FC2.data32F, new Float32Array(arrayC2));
  248. assert.deepEqual(mat32FC3.data32F, new Float32Array(arrayC3));
  249. assert.deepEqual(mat32FC4.data32F, new Float32Array(arrayC4));
  250. assert.deepEqual(mat64FC1.data64F, new Float64Array(arrayC1));
  251. assert.deepEqual(mat64FC2.data64F, new Float64Array(arrayC2));
  252. assert.deepEqual(mat64FC3.data64F, new Float64Array(arrayC3));
  253. assert.deepEqual(mat64FC4.data64F, new Float64Array(arrayC4));
  254. mat8UC1.delete();
  255. mat8UC2.delete();
  256. mat8UC3.delete();
  257. mat8UC4.delete();
  258. mat8SC1.delete();
  259. mat8SC2.delete();
  260. mat8SC3.delete();
  261. mat8SC4.delete();
  262. mat16UC1.delete();
  263. mat16UC2.delete();
  264. mat16UC3.delete();
  265. mat16UC4.delete();
  266. mat16SC1.delete();
  267. mat16SC2.delete();
  268. mat16SC3.delete();
  269. mat16SC4.delete();
  270. mat32SC1.delete();
  271. mat32SC2.delete();
  272. mat32SC3.delete();
  273. mat32SC4.delete();
  274. mat32FC1.delete();
  275. mat32FC2.delete();
  276. mat32FC3.delete();
  277. mat32FC4.delete();
  278. mat64FC1.delete();
  279. mat64FC2.delete();
  280. mat64FC3.delete();
  281. mat64FC4.delete();
  282. }
  283. // matFromImageData
  284. {
  285. // Only test in browser
  286. if (typeof window === 'undefined') {
  287. return;
  288. }
  289. let canvas = window.document.createElement('canvas');
  290. canvas.width = 2;
  291. canvas.height = 2;
  292. let ctx = canvas.getContext('2d');
  293. ctx.fillStyle='#FF0000';
  294. ctx.fillRect(0, 0, 1, 1);
  295. ctx.fillRect(1, 1, 1, 1);
  296. let imageData = ctx.getImageData(0, 0, 2, 2);
  297. let mat = cv.matFromImageData(imageData);
  298. assert.deepEqual(mat.data, new Uint8Array(imageData.data));
  299. mat.delete();
  300. }
  301. // Mat(mat)
  302. {
  303. let mat = new cv.Mat(2, 2, cv.CV_8UC4, new cv.Scalar(1, 0, 1, 0));
  304. let mat1 = new cv.Mat(mat);
  305. let mat2 = mat;
  306. assert.equal(mat.rows, mat1.rows);
  307. assert.equal(mat.cols, mat1.cols);
  308. assert.equal(mat.type(), mat1.type());
  309. assert.deepEqual(mat.data, mat1.data);
  310. mat.delete();
  311. assert.equal(mat1.isDeleted(), false);
  312. assert.equal(mat2.isDeleted(), true);
  313. mat1.delete();
  314. }
  315. // mat.setTo
  316. {
  317. let mat = new cv.Mat(2, 2, cv.CV_8UC4);
  318. let s = [0, 1, 2, 3];
  319. mat.setTo(s);
  320. assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s));
  321. assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
  322. assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s));
  323. assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));
  324. let s1 = [0, 0, 0, 0];
  325. mat.setTo(s1);
  326. let mask = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 0, 1]);
  327. mat.setTo(s, mask);
  328. assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s1));
  329. assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
  330. assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s1));
  331. assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));
  332. mat.delete();
  333. mask.delete();
  334. }
  335. });
  336. QUnit.test('test_mat_ptr', function(assert) {
  337. const RValue = 3;
  338. const GValue = 7;
  339. const BValue = 197;
  340. // cv.CV_8UC1 + Mat::ptr(int).
  341. {
  342. let mat = new cv.Mat(10, 10, cv.CV_8UC1);
  343. let view = mat.data;
  344. // Alter matrix[2, 1].
  345. let step = 10;
  346. view[2 * step + 1] = RValue;
  347. // Access matrix[2, 1].
  348. view = mat.ptr(2);
  349. assert.equal(view[1], RValue);
  350. mat.delete();
  351. }
  352. // cv.CV_8UC3 + Mat::ptr(int).
  353. {
  354. let mat = new cv.Mat(10, 10, cv.CV_8UC3);
  355. let view = mat.data;
  356. // Alter matrix[2, 1].
  357. let step = 3 * 10;
  358. view[2 * step + 3] = RValue;
  359. view[2 * step + 3 + 1] = GValue;
  360. view[2 * step + 3 + 2] = BValue;
  361. // Access matrix[2, 1].
  362. view = mat.ptr(2);
  363. assert.equal(view[3], RValue);
  364. assert.equal(view[3 + 1], GValue);
  365. assert.equal(view[3 + 2], BValue);
  366. mat.delete();
  367. }
  368. // cv.CV_8UC3 + Mat::ptr(int, int).
  369. {
  370. let mat = new cv.Mat(10, 10, cv.CV_8UC3);
  371. let view = mat.data;
  372. // Alter matrix[2, 1].
  373. let step = 3 * 10;
  374. view[2 * step + 3] = RValue;
  375. view[2 * step + 3 + 1] = GValue;
  376. view[2 * step + 3 + 2] = BValue;
  377. // Access matrix[2, 1].
  378. view = mat.ptr(2, 1);
  379. assert.equal(view[0], RValue);
  380. assert.equal(view[1], GValue);
  381. assert.equal(view[2], BValue);
  382. mat.delete();
  383. }
  384. const RValueF32 = 3.3;
  385. const GValueF32 = 7.3;
  386. const BValueF32 = 197.3;
  387. const EPSILON = 0.001;
  388. // cv.CV_32FC1 + Mat::ptr(int).
  389. {
  390. let mat = new cv.Mat(10, 10, cv.CV_32FC1);
  391. let view = mat.data32F;
  392. // Alter matrix[2, 1].
  393. let step = 10;
  394. view[2 * step + 1] = RValueF32;
  395. // Access matrix[2, 1].
  396. view = mat.floatPtr(2);
  397. assert.ok(Math.abs(view[1] - RValueF32) < EPSILON);
  398. mat.delete();
  399. }
  400. // cv.CV_32FC3 + Mat::ptr(int).
  401. {
  402. let mat = new cv.Mat(10, 10, cv.CV_32FC3);
  403. let view = mat.data32F;
  404. // Alter matrix[2, 1].
  405. let step = mat.step1(0);
  406. view[2 * step + 3] = RValueF32;
  407. view[2 * step + 3 + 1] = GValueF32;
  408. view[2 * step + 3 + 2] = BValueF32;
  409. // Access matrix[2, 1].
  410. view = mat.floatPtr(2);
  411. assert.ok(Math.abs(view[3] - RValueF32) < EPSILON);
  412. assert.ok(Math.abs(view[3 + 1] - GValueF32) < EPSILON);
  413. assert.ok(Math.abs(view[3 + 2] - BValueF32) < EPSILON);
  414. mat.delete();
  415. }
  416. // cv.CV_32FC3 + Mat::ptr(int, int).
  417. {
  418. let mat = new cv.Mat(10, 10, cv.CV_32FC3);
  419. let view = mat.data32F;
  420. // Alter matrix[2, 1].
  421. let step = mat.step1(0);
  422. view[2 * step + 3] = RValueF32;
  423. view[2 * step + 3 + 1] = GValueF32;
  424. view[2 * step + 3 + 2] = BValueF32;
  425. // Access matrix[2, 1].
  426. view = mat.floatPtr(2, 1);
  427. assert.ok(Math.abs(view[0] - RValueF32) < EPSILON);
  428. assert.ok(Math.abs(view[1] - GValueF32) < EPSILON);
  429. assert.ok(Math.abs(view[2] - BValueF32) < EPSILON);
  430. mat.delete();
  431. }
  432. });
  433. QUnit.test('test_mat_zeros', function(assert) {
  434. let zeros = new Uint8Array(10*10).fill(0);
  435. // Mat::zeros(int, int, int)
  436. {
  437. let mat = cv.Mat.zeros(10, 10, cv.CV_8UC1);
  438. let view = mat.data;
  439. assert.deepEqual(view, zeros);
  440. mat.delete();
  441. }
  442. // Mat::zeros(Size, int)
  443. {
  444. let mat = cv.Mat.zeros({height: 10, width: 10}, cv.CV_8UC1);
  445. let view = mat.data;
  446. assert.deepEqual(view, zeros);
  447. mat.delete();
  448. }
  449. });
  450. QUnit.test('test_mat_ones', function(assert) {
  451. let ones = new Uint8Array(10*10).fill(1);
  452. // Mat::ones(int, int, int)
  453. {
  454. let mat = cv.Mat.ones(10, 10, cv.CV_8UC1);
  455. let view = mat.data;
  456. assert.deepEqual(view, ones);
  457. }
  458. // Mat::ones(Size, int)
  459. {
  460. let mat = cv.Mat.ones({height: 10, width: 10}, cv.CV_8UC1);
  461. let view = mat.data;
  462. assert.deepEqual(view, ones);
  463. }
  464. });
  465. QUnit.test('test_mat_eye', function(assert) {
  466. let eye4by4 = new Uint8Array([1, 0, 0, 0,
  467. 0, 1, 0, 0,
  468. 0, 0, 1, 0,
  469. 0, 0, 0, 1]);
  470. // Mat::eye(int, int, int)
  471. {
  472. let mat = cv.Mat.eye(4, 4, cv.CV_8UC1);
  473. let view = mat.data;
  474. assert.deepEqual(view, eye4by4);
  475. }
  476. // Mat::eye(Size, int)
  477. {
  478. let mat = cv.Mat.eye({height: 4, width: 4}, cv.CV_8UC1);
  479. let view = mat.data;
  480. assert.deepEqual(view, eye4by4);
  481. }
  482. });
  483. QUnit.test('test_mat_miscs', function(assert) {
  484. // Mat::col(int)
  485. {
  486. let mat = cv.matFromArray(2, 2, cv.CV_8UC2, [1, 2, 3, 4, 5, 6, 7, 8]);
  487. let col = mat.col(1);
  488. assert.equal(col.isContinuous(), false);
  489. assert.equal(col.ptr(0, 0)[0], 3);
  490. assert.equal(col.ptr(0, 0)[1], 4);
  491. assert.equal(col.ptr(1, 0)[0], 7);
  492. assert.equal(col.ptr(1, 0)[1], 8);
  493. col.delete();
  494. mat.delete();
  495. }
  496. // Mat::row(int)
  497. {
  498. let mat = cv.Mat.zeros(5, 5, cv.CV_8UC2);
  499. let row = mat.row(1);
  500. let view = row.data;
  501. assert.equal(view[0], 0);
  502. assert.equal(view[4], 0);
  503. row.delete();
  504. mat.delete();
  505. }
  506. // Mat::convertTo(Mat, int, double, double)
  507. {
  508. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  509. let grayMat = cv.Mat.zeros(5, 5, cv.CV_8UC1);
  510. mat.convertTo(grayMat, cv.CV_8U, 2, 1);
  511. // dest = 2 * source(x, y) + 1.
  512. let view = grayMat.data;
  513. assert.equal(view[0], (1 * 2) + 1);
  514. mat.convertTo(grayMat, cv.CV_8U);
  515. // dest = 1 * source(x, y) + 0.
  516. assert.equal(view[0], 1);
  517. mat.convertTo(grayMat, cv.CV_8U, 2);
  518. // dest = 2 * source(x, y) + 0.
  519. assert.equal(view[0], 2);
  520. grayMat.delete();
  521. mat.delete();
  522. }
  523. // split
  524. {
  525. const R =7;
  526. const G =13;
  527. const B =29;
  528. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  529. let view = mat.data;
  530. view[0] = R;
  531. view[1] = G;
  532. view[2] = B;
  533. let bgrPlanes = new cv.MatVector();
  534. cv.split(mat, bgrPlanes);
  535. assert.equal(bgrPlanes.size(), 3);
  536. let rMat = bgrPlanes.get(0);
  537. view = rMat.data;
  538. assert.equal(view[0], R);
  539. let gMat = bgrPlanes.get(1);
  540. view = gMat.data;
  541. assert.equal(view[0], G);
  542. let bMat = bgrPlanes.get(2);
  543. view = bMat.data;
  544. assert.equal(view[0], B);
  545. mat.delete();
  546. rMat.delete();
  547. gMat.delete();
  548. bgrPlanes.delete();
  549. bMat.delete();
  550. }
  551. // elemSize
  552. {
  553. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  554. assert.equal(mat.elemSize(), 3);
  555. assert.equal(mat.elemSize1(), 1);
  556. let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
  557. assert.equal(mat2.elemSize(), 1);
  558. assert.equal(mat2.elemSize1(), 1);
  559. let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
  560. assert.equal(mat3.elemSize(), 2 * 3);
  561. assert.equal(mat3.elemSize1(), 2);
  562. mat.delete();
  563. mat2.delete();
  564. mat3.delete();
  565. }
  566. // step
  567. {
  568. let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
  569. assert.equal(mat.step[0], 15);
  570. assert.equal(mat.step[1], 3);
  571. let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
  572. assert.equal(mat2.step[0], 5);
  573. assert.equal(mat2.step[1], 1);
  574. let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
  575. assert.equal(mat3.step[0], 30);
  576. assert.equal(mat3.step[1], 6);
  577. mat.delete();
  578. mat2.delete();
  579. mat3.delete();
  580. }
  581. // dot
  582. {
  583. let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
  584. let mat2 = cv.Mat.eye(5, 5, cv.CV_8UC1);
  585. assert.equal(mat.dot(mat), 25);
  586. assert.equal(mat.dot(mat2), 5);
  587. assert.equal(mat2.dot(mat2), 5);
  588. mat.delete();
  589. mat2.delete();
  590. }
  591. // mul
  592. {
  593. const FACTOR = 5;
  594. let mat = cv.Mat.ones(4, 4, cv.CV_8UC1);
  595. let mat2 = cv.Mat.eye(4, 4, cv.CV_8UC1);
  596. let expected = new Uint8Array([FACTOR, 0, 0, 0,
  597. 0, FACTOR, 0, 0,
  598. 0, 0, FACTOR, 0,
  599. 0, 0, 0, FACTOR]);
  600. let mat3 = mat.mul(mat2, FACTOR);
  601. assert.deepEqual(mat3.data, expected);
  602. mat.delete();
  603. mat2.delete();
  604. mat3.delete();
  605. }
  606. });
  607. QUnit.test('test mat access', function(assert) {
  608. // test memory view
  609. {
  610. let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
  611. let dataPtr = cv._malloc(8);
  612. let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
  613. dataHeap.set(new Uint8Array(data.buffer));
  614. let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);
  615. let unsignedCharView = new Uint8Array(data.buffer);
  616. let charView = new Int8Array(data.buffer);
  617. let shortView = new Int16Array(data.buffer);
  618. let unsignedShortView = new Uint16Array(data.buffer);
  619. let intView = new Int32Array(data.buffer);
  620. let float32View = new Float32Array(data.buffer);
  621. let float64View = new Float64Array(data.buffer);
  622. assert.deepEqual(unsignedCharView, mat.data);
  623. assert.deepEqual(charView, mat.data8S);
  624. assert.deepEqual(shortView, mat.data16S);
  625. assert.deepEqual(unsignedShortView, mat.data16U);
  626. assert.deepEqual(intView, mat.data32S);
  627. assert.deepEqual(float32View, mat.data32F);
  628. assert.deepEqual(float64View, mat.data64F);
  629. }
  630. // test ucharAt(i)
  631. {
  632. let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
  633. let dataPtr = cv._malloc(8);
  634. let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
  635. dataHeap.set(new Uint8Array(data.buffer));
  636. let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);
  637. assert.equal(mat.ucharAt(0), 0);
  638. assert.equal(mat.ucharAt(1), 0);
  639. assert.equal(mat.ucharAt(2), 0);
  640. assert.equal(mat.ucharAt(3), 255);
  641. assert.equal(mat.ucharAt(4), 0);
  642. assert.equal(mat.ucharAt(5), 1);
  643. assert.equal(mat.ucharAt(6), 2);
  644. assert.equal(mat.ucharAt(7), 3);
  645. }
  646. // test ushortAt(i)
  647. {
  648. let data = new Uint16Array([0, 1000, 65000, 255, 0, 1, 2, 3]);
  649. let dataPtr = cv._malloc(16);
  650. let dataHeap = new Uint16Array(cv.HEAPU8.buffer, dataPtr, 8);
  651. dataHeap.set(new Uint16Array(data.buffer));
  652. let mat = new cv.Mat(8, 1, cv.CV_16SC1, dataPtr, 0);
  653. assert.equal(mat.ushortAt(0), 0);
  654. assert.equal(mat.ushortAt(1), 1000);
  655. assert.equal(mat.ushortAt(2), 65000);
  656. assert.equal(mat.ushortAt(3), 255);
  657. assert.equal(mat.ushortAt(4), 0);
  658. assert.equal(mat.ushortAt(5), 1);
  659. assert.equal(mat.ushortAt(6), 2);
  660. assert.equal(mat.ushortAt(7), 3);
  661. }
  662. // test intAt(i)
  663. {
  664. let data = new Int32Array([0, -1000, 65000, 255, -2000000, -1, 2, 3]);
  665. let dataPtr = cv._malloc(32);
  666. let dataHeap = new Int32Array(cv.HEAPU32.buffer, dataPtr, 8);
  667. dataHeap.set(new Int32Array(data.buffer));
  668. let mat = new cv.Mat(8, 1, cv.CV_32SC1, dataPtr, 0);
  669. assert.equal(mat.intAt(0), 0);
  670. assert.equal(mat.intAt(1), -1000);
  671. assert.equal(mat.intAt(2), 65000);
  672. assert.equal(mat.intAt(3), 255);
  673. assert.equal(mat.intAt(4), -2000000);
  674. assert.equal(mat.intAt(5), -1);
  675. assert.equal(mat.intAt(6), 2);
  676. assert.equal(mat.intAt(7), 3);
  677. }
  678. // test floatAt(i)
  679. {
  680. const EPSILON = 0.001;
  681. let data = new Float32Array([0, -10.5, 650.001, 255, -20.1, -1.2, 2, 3.5]);
  682. let dataPtr = cv._malloc(32);
  683. let dataHeap = new Float32Array(cv.HEAPU32.buffer, dataPtr, 8);
  684. dataHeap.set(new Float32Array(data.buffer));
  685. let mat = new cv.Mat(8, 1, cv.CV_32FC1, dataPtr, 0);
  686. assert.equal(Math.abs(mat.floatAt(0)-0) < EPSILON, true);
  687. assert.equal(Math.abs(mat.floatAt(1)+10.5) < EPSILON, true);
  688. assert.equal(Math.abs(mat.floatAt(2)-650.001) < EPSILON, true);
  689. assert.equal(Math.abs(mat.floatAt(3)-255) < EPSILON, true);
  690. assert.equal(Math.abs(mat.floatAt(4)+20.1) < EPSILON, true);
  691. assert.equal(Math.abs(mat.floatAt(5)+1.2) < EPSILON, true);
  692. assert.equal(Math.abs(mat.floatAt(6)-2) < EPSILON, true);
  693. assert.equal(Math.abs(mat.floatAt(7)-3.5) < EPSILON, true);
  694. }
  695. // test intAt(i,j)
  696. {
  697. let mat = cv.Mat.eye({height: 3, width: 3}, cv.CV_32SC1);
  698. assert.equal(mat.intAt(0, 0), 1);
  699. assert.equal(mat.intAt(0, 1), 0);
  700. assert.equal(mat.intAt(0, 2), 0);
  701. assert.equal(mat.intAt(1, 0), 0);
  702. assert.equal(mat.intAt(1, 1), 1);
  703. assert.equal(mat.intAt(1, 2), 0);
  704. assert.equal(mat.intAt(2, 0), 0);
  705. assert.equal(mat.intAt(2, 1), 0);
  706. assert.equal(mat.intAt(2, 2), 1);
  707. mat.delete();
  708. }
  709. });
  710. QUnit.test('test_mat_operations', function(assert) {
  711. // test minMaxLoc
  712. {
  713. let src = cv.Mat.ones(4, 4, cv.CV_8UC1);
  714. src.data[2] = 0;
  715. src.data[5] = 2;
  716. let result = cv.minMaxLoc(src);
  717. assert.equal(result.minVal, 0);
  718. assert.equal(result.maxVal, 2);
  719. assert.deepEqual(result.minLoc, {x: 2, y: 0});
  720. assert.deepEqual(result.maxLoc, {x: 1, y: 1});
  721. src.delete();
  722. }
  723. });
  724. QUnit.test('test_mat_roi', function(assert) {
  725. // test minMaxLoc
  726. {
  727. let mat = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
  728. let roi = mat.roi(new cv.Rect(1, 1, 1, 1));
  729. assert.equal(roi.rows, 1);
  730. assert.equal(roi.cols, 1);
  731. assert.deepEqual(roi.data, new Uint8Array([mat.ucharAt(1, 1)]));
  732. mat.delete();
  733. roi.delete();
  734. }
  735. });
  736. QUnit.test('test_mat_range', function(assert) {
  737. {
  738. let src = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
  739. let mat = src.colRange(0, 1);
  740. assert.equal(mat.isContinuous(), false);
  741. assert.equal(mat.rows, 2);
  742. assert.equal(mat.cols, 1);
  743. assert.equal(mat.ucharAt(0), 0);
  744. assert.equal(mat.ucharAt(1), 2);
  745. mat.delete();
  746. mat = src.colRange({start: 0, end: 1});
  747. assert.equal(mat.isContinuous(), false);
  748. assert.equal(mat.rows, 2);
  749. assert.equal(mat.cols, 1);
  750. assert.equal(mat.ucharAt(0), 0);
  751. assert.equal(mat.ucharAt(1), 2);
  752. mat.delete();
  753. mat = src.rowRange(1, 2);
  754. assert.equal(mat.rows, 1);
  755. assert.equal(mat.cols, 2);
  756. assert.deepEqual(mat.data, new Uint8Array([2, 3]));
  757. mat.delete();
  758. mat = src.rowRange({start: 1, end: 2});
  759. assert.equal(mat.rows, 1);
  760. assert.equal(mat.cols, 2);
  761. assert.deepEqual(mat.data, new Uint8Array([2, 3]));
  762. mat.delete();
  763. src.delete();
  764. }
  765. });
  766. QUnit.test('test_mat_diag', function(assert) {
  767. // test diag
  768. {
  769. let mat = cv.matFromArray(3, 3, cv.CV_8UC1, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
  770. let d = mat.diag();
  771. let d1 = mat.diag(1);
  772. let d2 = mat.diag(-1);
  773. assert.equal(mat.isContinuous(), true);
  774. assert.equal(d.isContinuous(), false);
  775. assert.equal(d1.isContinuous(), false);
  776. assert.equal(d2.isContinuous(), false);
  777. assert.equal(d.ucharAt(0), 0);
  778. assert.equal(d.ucharAt(1), 4);
  779. assert.equal(d.ucharAt(2), 8);
  780. assert.equal(d1.ucharAt(0), 1);
  781. assert.equal(d1.ucharAt(1), 5);
  782. assert.equal(d2.ucharAt(0), 3);
  783. assert.equal(d2.ucharAt(1), 7);
  784. mat.delete();
  785. d.delete();
  786. d1.delete();
  787. d2.delete();
  788. }
  789. });