test_imgproc.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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('Image Processing', {});
  70. QUnit.test('applyColorMap', function(assert) {
  71. {
  72. let src = cv.matFromArray(2, 1, cv.CV_8U, [50,100]);
  73. cv.applyColorMap(src, src, cv.COLORMAP_BONE);
  74. // Verify result.
  75. let expected = new Uint8Array([60,44,44,119,89,87]);
  76. assert.deepEqual(src.data, expected);
  77. src.delete();
  78. }
  79. });
  80. QUnit.test('blendLinear', function(assert) {
  81. {
  82. let src1 = cv.matFromArray(2, 1, cv.CV_8U, [50,100]);
  83. let src2 = cv.matFromArray(2, 1, cv.CV_8U, [200,20]);
  84. let weights1 = cv.matFromArray(2, 1, cv.CV_32F, [0.4,0.5]);
  85. let weights2 = cv.matFromArray(2, 1, cv.CV_32F, [0.6,0.5]);
  86. let dst = new cv.Mat();
  87. cv.blendLinear(src1, src2, weights1, weights2, dst);
  88. // Verify result.
  89. let expected = new Uint8Array([140,60]);
  90. assert.deepEqual(dst.data, expected);
  91. src1.delete();
  92. src2.delete();
  93. weights1.delete();
  94. weights2.delete();
  95. dst.delete();
  96. }
  97. });
  98. QUnit.test('createHanningWindow', function(assert) {
  99. {
  100. let dst = new cv.Mat();
  101. cv.createHanningWindow(dst, new cv.Size(5, 3), cv.CV_32F);
  102. // Verify result.
  103. let expected = cv.matFromArray(3, 5, cv.CV_32F, [0.,0.,0.,0.,0.,0.,0.70710677,1.,0.70710677,0.,0.,0.,0.,0.,0.]);
  104. assert.deepEqual(dst.data, expected.data);
  105. dst.delete();
  106. expected.delete();
  107. }
  108. });
  109. QUnit.test('test_imgProc', function(assert) {
  110. // calcHist
  111. {
  112. let vec1 = new cv.Mat.ones(new cv.Size(20, 20), cv.CV_8UC1); // eslint-disable-line new-cap
  113. let source = new cv.MatVector();
  114. source.push_back(vec1);
  115. let channels = [0];
  116. let histSize = [256];
  117. let ranges =[0, 256];
  118. let hist = new cv.Mat();
  119. let mask = new cv.Mat();
  120. let binSize = cv._malloc(4);
  121. let binView = new Int32Array(cv.HEAP8.buffer, binSize);
  122. binView[0] = 10;
  123. cv.calcHist(source, channels, mask, hist, histSize, ranges, false);
  124. // hist should contains a N X 1 array.
  125. let size = hist.size();
  126. assert.equal(size.height, 256);
  127. assert.equal(size.width, 1);
  128. // default parameters
  129. cv.calcHist(source, channels, mask, hist, histSize, ranges);
  130. size = hist.size();
  131. assert.equal(size.height, 256);
  132. assert.equal(size.width, 1);
  133. // Do we need to verify data in histogram?
  134. // let dataView = hist.data;
  135. // Free resource
  136. cv._free(binSize);
  137. mask.delete();
  138. hist.delete();
  139. }
  140. // cvtColor
  141. {
  142. let source = new cv.Mat(10, 10, cv.CV_8UC3);
  143. let dest = new cv.Mat();
  144. cv.cvtColor(source, dest, cv.COLOR_BGR2GRAY, 0);
  145. assert.equal(dest.channels(), 1);
  146. cv.cvtColor(source, dest, cv.COLOR_BGR2GRAY);
  147. assert.equal(dest.channels(), 1);
  148. cv.cvtColor(source, dest, cv.COLOR_BGR2BGRA, 0);
  149. assert.equal(dest.channels(), 4);
  150. cv.cvtColor(source, dest, cv.COLOR_BGR2BGRA);
  151. assert.equal(dest.channels(), 4);
  152. dest.delete();
  153. source.delete();
  154. }
  155. // equalizeHist
  156. {
  157. let source = new cv.Mat(10, 10, cv.CV_8UC1);
  158. let dest = new cv.Mat();
  159. cv.equalizeHist(source, dest);
  160. // eualizeHist changes the content of a image, but does not alter meta data
  161. // of it.
  162. assert.equal(source.channels(), dest.channels());
  163. assert.equal(source.type(), dest.type());
  164. dest.delete();
  165. source.delete();
  166. }
  167. // floodFill
  168. {
  169. let center = new cv.Point(5, 5);
  170. let rect = new cv.Rect(0, 0, 0, 0);
  171. let img = new cv.Mat.zeros(10, 10, cv.CV_8UC1);
  172. let color = new cv.Scalar (255);
  173. cv.circle(img, center, 3, color, 1);
  174. let edge = new cv.Mat();
  175. cv.Canny(img, edge, 100, 255);
  176. cv.copyMakeBorder(edge, edge, 1, 1, 1, 1, cv.BORDER_REPLICATE);
  177. let expected_img_data = new Uint8Array([
  178. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  179. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  180. 0, 0, 0, 0, 0, 255, 0, 0, 0, 0,
  181. 0, 0, 0, 255, 255, 255, 255, 255, 0, 0,
  182. 0, 0, 0, 255, 0, 255, 0, 255, 0, 0,
  183. 0, 0, 255, 255, 255, 255, 0, 0, 255, 0,
  184. 0, 0, 0, 255, 0, 0, 0, 255, 0, 0,
  185. 0, 0, 0, 255, 255, 0, 255, 255, 0, 0,
  186. 0, 0, 0, 0, 0, 255, 0, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
  188. let img_elem = 10*10*1;
  189. let expected_img_data_ptr = cv._malloc(img_elem);
  190. let expected_img_data_heap = new Uint8Array(cv.HEAPU8.buffer,
  191. expected_img_data_ptr,
  192. img_elem);
  193. expected_img_data_heap.set(new Uint8Array(expected_img_data.buffer));
  194. let expected_img = new cv.Mat( 10, 10, cv.CV_8UC1, expected_img_data_ptr, 0);
  195. let expected_rect = new cv.Rect(3,3,3,3);
  196. let compare_result = new cv.Mat(10, 10, cv.CV_8UC1);
  197. cv.floodFill(img, edge, center, color, rect);
  198. cv.compare (img, expected_img, compare_result, cv.CMP_EQ);
  199. // expect every pixels are the same.
  200. assert.equal (cv.countNonZero(compare_result), img.total());
  201. assert.equal (rect.x, expected_rect.x);
  202. assert.equal (rect.y, expected_rect.y);
  203. assert.equal (rect.width, expected_rect.width);
  204. assert.equal (rect.height, expected_rect.height);
  205. img.delete();
  206. edge.delete();
  207. expected_img.delete();
  208. compare_result.delete();
  209. }
  210. });
  211. QUnit.test('Drawing Functions', function(assert) {
  212. // fillPoly
  213. {
  214. let img_width = 6;
  215. let img_height = 6;
  216. let img = new cv.Mat.zeros(img_height, img_width, cv.CV_8UC1);
  217. let npts = 4;
  218. let square_point_data = new Uint8Array([
  219. 1, 1,
  220. 4, 1,
  221. 4, 4,
  222. 1, 4]);
  223. let square_points = cv.matFromArray(npts, 1, cv.CV_32SC2, square_point_data);
  224. let pts = new cv.MatVector();
  225. pts.push_back (square_points);
  226. let color = new cv.Scalar (255);
  227. let expected_img_data = new Uint8Array([
  228. 0, 0, 0, 0, 0, 0,
  229. 0, 255, 255, 255, 255, 0,
  230. 0, 255, 255, 255, 255, 0,
  231. 0, 255, 255, 255, 255, 0,
  232. 0, 255, 255, 255, 255, 0,
  233. 0, 0, 0, 0, 0, 0]);
  234. let expected_img = cv.matFromArray(img_height, img_width, cv.CV_8UC1, expected_img_data);
  235. cv.fillPoly(img, pts, color);
  236. let compare_result = new cv.Mat(img_height, img_width, cv.CV_8UC1);
  237. cv.compare (img, expected_img, compare_result, cv.CMP_EQ);
  238. // expect every pixels are the same.
  239. assert.equal (cv.countNonZero(compare_result), img.total());
  240. img.delete();
  241. square_points.delete();
  242. pts.delete();
  243. expected_img.delete();
  244. compare_result.delete();
  245. }
  246. // fillConvexPoly
  247. {
  248. let img_width = 6;
  249. let img_height = 6;
  250. let img = new cv.Mat.zeros(img_height, img_width, cv.CV_8UC1);
  251. let npts = 4;
  252. let square_point_data = new Uint8Array([
  253. 1, 1,
  254. 4, 1,
  255. 4, 4,
  256. 1, 4]);
  257. let square_points = cv.matFromArray(npts, 1, cv.CV_32SC2, square_point_data);
  258. let color = new cv.Scalar (255);
  259. let expected_img_data = new Uint8Array([
  260. 0, 0, 0, 0, 0, 0,
  261. 0, 255, 255, 255, 255, 0,
  262. 0, 255, 255, 255, 255, 0,
  263. 0, 255, 255, 255, 255, 0,
  264. 0, 255, 255, 255, 255, 0,
  265. 0, 0, 0, 0, 0, 0]);
  266. let expected_img = cv.matFromArray(img_height, img_width, cv.CV_8UC1, expected_img_data);
  267. cv.fillConvexPoly(img, square_points, color);
  268. let compare_result = new cv.Mat(img_height, img_width, cv.CV_8UC1);
  269. cv.compare (img, expected_img, compare_result, cv.CMP_EQ);
  270. // expect every pixels are the same.
  271. assert.equal (cv.countNonZero(compare_result), img.total());
  272. img.delete();
  273. square_points.delete();
  274. expected_img.delete();
  275. compare_result.delete();
  276. }
  277. });
  278. QUnit.test('test_segmentation', function(assert) {
  279. const THRESHOLD = 127.0;
  280. const THRESHOLD_MAX = 210.0;
  281. // threshold
  282. {
  283. let source = new cv.Mat(1, 5, cv.CV_8UC1);
  284. let sourceView = source.data;
  285. sourceView[0] = 0; // < threshold
  286. sourceView[1] = 100; // < threshold
  287. sourceView[2] = 200; // > threshold
  288. let dest = new cv.Mat();
  289. cv.threshold(source, dest, THRESHOLD, THRESHOLD_MAX, cv.THRESH_BINARY);
  290. let destView = dest.data;
  291. assert.equal(destView[0], 0);
  292. assert.equal(destView[1], 0);
  293. assert.equal(destView[2], THRESHOLD_MAX);
  294. }
  295. // adaptiveThreshold
  296. {
  297. let source = cv.Mat.zeros(1, 5, cv.CV_8UC1);
  298. let sourceView = source.data;
  299. sourceView[0] = 50;
  300. sourceView[1] = 150;
  301. sourceView[2] = 200;
  302. let dest = new cv.Mat();
  303. const C = 0;
  304. const blockSize = 3;
  305. cv.adaptiveThreshold(source, dest, THRESHOLD_MAX,
  306. cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, blockSize, C);
  307. let destView = dest.data;
  308. assert.equal(destView[0], 0);
  309. assert.equal(destView[1], THRESHOLD_MAX);
  310. assert.equal(destView[2], THRESHOLD_MAX);
  311. }
  312. });
  313. QUnit.test('test_shape', function(assert) {
  314. // moments
  315. {
  316. let points = new cv.Mat(1, 4, cv.CV_32SC2);
  317. let data32S = points.data32S;
  318. data32S[0]=50;
  319. data32S[1]=56;
  320. data32S[2]=53;
  321. data32S[3]=53;
  322. data32S[4]=46;
  323. data32S[5]=54;
  324. data32S[6]=49;
  325. data32S[7]=51;
  326. let m = cv.moments(points, false);
  327. let area = cv.contourArea(points, false);
  328. assert.equal(m.m00, 0);
  329. assert.equal(m.m01, 0);
  330. assert.equal(m.m10, 0);
  331. assert.equal(area, 0);
  332. // default parameters
  333. m = cv.moments(points);
  334. area = cv.contourArea(points);
  335. assert.equal(m.m00, 0);
  336. assert.equal(m.m01, 0);
  337. assert.equal(m.m10, 0);
  338. assert.equal(area, 0);
  339. points.delete();
  340. }
  341. });
  342. QUnit.test('test_min_enclosing', function(assert) {
  343. // minEnclosingCircle
  344. {
  345. let points = new cv.Mat(4, 1, cv.CV_32FC2);
  346. points.data32F[0] = 0;
  347. points.data32F[1] = 0;
  348. points.data32F[2] = 1;
  349. points.data32F[3] = 0;
  350. points.data32F[4] = 1;
  351. points.data32F[5] = 1;
  352. points.data32F[6] = 0;
  353. points.data32F[7] = 1;
  354. let circle = cv.minEnclosingCircle(points);
  355. assert.deepEqual(circle.center, {x: 0.5, y: 0.5});
  356. assert.ok(Math.abs(circle.radius - Math.sqrt(2) / 2) < 0.001);
  357. points.delete();
  358. }
  359. // minEnclosingTriangle
  360. {
  361. let dst = cv.Mat.zeros(80, 80, cv.CV_8U);
  362. let contours = new cv.MatVector();
  363. let hierarchy = new cv.Mat();
  364. let triangle = new cv.Mat();
  365. cv.drawMarker(dst, new cv.Point(40, 40), new cv.Scalar(255));
  366. cv.findContoursLinkRuns(dst,contours,hierarchy);
  367. cv.minEnclosingTriangle(contours.get(0),triangle);
  368. // Verify result.
  369. const triangleData = triangle.data32F;
  370. assert.deepEqual(triangleData[0], triangleData[4]);
  371. assert.deepEqual(triangleData[1], 20);
  372. assert.deepEqual(triangleData[2], 30);
  373. assert.deepEqual(triangleData[3], 40);
  374. assert.deepEqual(triangleData[5], 60);
  375. dst.delete();
  376. contours.delete();
  377. hierarchy.delete();
  378. triangle.delete();
  379. }
  380. });
  381. QUnit.test('test_filter', function(assert) {
  382. // blur
  383. {
  384. let mat1 = cv.Mat.ones(5, 5, cv.CV_8UC3);
  385. let mat2 = new cv.Mat();
  386. cv.blur(mat1, mat2, {height: 3, width: 3}, {x: -1, y: -1}, cv.BORDER_DEFAULT);
  387. // Verify result.
  388. let size = mat2.size();
  389. assert.equal(mat2.channels(), 3);
  390. assert.equal(size.height, 5);
  391. assert.equal(size.width, 5);
  392. cv.blur(mat1, mat2, {height: 3, width: 3}, {x: -1, y: -1});
  393. // Verify result.
  394. size = mat2.size();
  395. assert.equal(mat2.channels(), 3);
  396. assert.equal(size.height, 5);
  397. assert.equal(size.width, 5);
  398. cv.blur(mat1, mat2, {height: 3, width: 3});
  399. // Verify result.
  400. size = mat2.size();
  401. assert.equal(mat2.channels(), 3);
  402. assert.equal(size.height, 5);
  403. assert.equal(size.width, 5);
  404. mat1.delete();
  405. mat2.delete();
  406. }
  407. // GaussianBlur
  408. {
  409. let mat1 = cv.Mat.ones(7, 7, cv.CV_8UC1);
  410. let mat2 = new cv.Mat();
  411. cv.GaussianBlur(mat1, mat2, new cv.Size(3, 3), 0, 0, // eslint-disable-line new-cap
  412. cv.BORDER_DEFAULT);
  413. // Verify result.
  414. let size = mat2.size();
  415. assert.equal(mat2.channels(), 1);
  416. assert.equal(size.height, 7);
  417. assert.equal(size.width, 7);
  418. mat1.delete();
  419. mat2.delete();
  420. }
  421. // spatialGradient
  422. {
  423. let src = cv.matFromArray(4, 4, cv.CV_8U, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
  424. let dx = new cv.Mat();
  425. let dy = new cv.Mat();
  426. cv.spatialGradient(src, dx, dy);
  427. // Verify result.
  428. let expected_dx = new cv.Mat();
  429. let expected_dy = new cv.Mat();
  430. cv.Sobel(src, expected_dx, cv.CV_16SC1, 1, 0, 3);
  431. cv.Sobel(src, expected_dy, cv.CV_16SC1, 0, 1, 3);
  432. assert.deepEqual(dx.data, expected_dx.data);
  433. assert.deepEqual(dy.data, expected_dy.data);
  434. src.delete();
  435. dx.delete();
  436. dy.delete();
  437. expected_dx.delete();
  438. expected_dy.delete();
  439. }
  440. // sqrBoxFilter
  441. {
  442. let src = cv.matFromArray(2, 3, cv.CV_8U, [1,2,1,1,2,1]);
  443. let dst = new cv.Mat();
  444. cv.sqrBoxFilter(src, dst, cv.CV_32F, new cv.Size(3, 3));
  445. // Verify result.
  446. let expected = cv.matFromArray(2, 3, cv.CV_32F,[3.0,2.0,3.0,3.0,2.0,3.0]);
  447. assert.deepEqual(dst.data, expected.data);
  448. src.delete();
  449. dst.delete();
  450. expected.delete();
  451. }
  452. // stackBlur
  453. {
  454. let src = cv.matFromArray(2, 3, cv.CV_8U, [10,25,30,45,50,60]);
  455. cv.stackBlur(src, src, new cv.Size(3, 3));
  456. // Verify result.
  457. let expected = new Uint8Array([14,22,29,46,51,58]);
  458. assert.deepEqual(src.data, expected);
  459. src.delete();
  460. }
  461. // medianBlur
  462. {
  463. let mat1 = cv.Mat.ones(9, 9, cv.CV_8UC3);
  464. let mat2 = new cv.Mat();
  465. cv.medianBlur(mat1, mat2, 3);
  466. // Verify result.
  467. let size = mat2.size();
  468. assert.equal(mat2.channels(), 3);
  469. assert.equal(size.height, 9);
  470. assert.equal(size.width, 9);
  471. mat1.delete();
  472. mat2.delete();
  473. }
  474. // bilateralFilter
  475. {
  476. let mat1 = cv.Mat.ones(11, 11, cv.CV_8UC3);
  477. let mat2 = new cv.Mat();
  478. cv.bilateralFilter(mat1, mat2, 3, 6, 1.5, cv.BORDER_DEFAULT);
  479. // Verify result.
  480. let size = mat2.size();
  481. assert.equal(mat2.channels(), 3);
  482. assert.equal(size.height, 11);
  483. assert.equal(size.width, 11);
  484. // default parameters
  485. cv.bilateralFilter(mat1, mat2, 3, 6, 1.5);
  486. // Verify result.
  487. size = mat2.size();
  488. assert.equal(mat2.channels(), 3);
  489. assert.equal(size.height, 11);
  490. assert.equal(size.width, 11);
  491. mat1.delete();
  492. mat2.delete();
  493. }
  494. });
  495. QUnit.test('test_watershed', function(assert) {
  496. {
  497. let mat = cv.Mat.ones(11, 11, cv.CV_8UC3);
  498. let out = new cv.Mat(11, 11, cv.CV_32SC1);
  499. cv.watershed(mat, out);
  500. // Verify result.
  501. let size = out.size();
  502. assert.equal(out.channels(), 1);
  503. assert.equal(size.height, 11);
  504. assert.equal(size.width, 11);
  505. assert.equal(out.elemSize1(), 4);
  506. mat.delete();
  507. out.delete();
  508. }
  509. });
  510. QUnit.test('test_distanceTransform', function(assert) {
  511. {
  512. let mat = cv.Mat.ones(11, 11, cv.CV_8UC1);
  513. let out = new cv.Mat(11, 11, cv.CV_32FC1);
  514. let labels = new cv.Mat(11, 11, cv.CV_32FC1);
  515. const maskSize = 3;
  516. cv.distanceTransform(mat, out, cv.DIST_L2, maskSize, cv.CV_32F);
  517. // Verify result.
  518. let size = out.size();
  519. assert.equal(out.channels(), 1);
  520. assert.equal(size.height, 11);
  521. assert.equal(size.width, 11);
  522. assert.equal(out.elemSize1(), 4);
  523. cv.distanceTransformWithLabels(mat, out, labels, cv.DIST_L2, maskSize,
  524. cv.DIST_LABEL_CCOMP);
  525. // Verify result.
  526. size = out.size();
  527. assert.equal(out.channels(), 1);
  528. assert.equal(size.height, 11);
  529. assert.equal(size.width, 11);
  530. assert.equal(out.elemSize1(), 4);
  531. size = labels.size();
  532. assert.equal(labels.channels(), 1);
  533. assert.equal(size.height, 11);
  534. assert.equal(size.width, 11);
  535. assert.equal(labels.elemSize1(), 4);
  536. mat.delete();
  537. out.delete();
  538. labels.delete();
  539. }
  540. });
  541. QUnit.test('test_integral', function(assert) {
  542. {
  543. let mat = cv.Mat.eye({height: 100, width: 100}, cv.CV_8UC3);
  544. let sum = new cv.Mat();
  545. let sqSum = new cv.Mat();
  546. let title = new cv.Mat();
  547. cv.integral(mat, sum, -1);
  548. // Verify result.
  549. let size = sum.size();
  550. assert.equal(sum.channels(), 3);
  551. assert.equal(size.height, 100+1);
  552. assert.equal(size.width, 100+1);
  553. cv.integral2(mat, sum, sqSum, -1, -1);
  554. // Verify result.
  555. size = sum.size();
  556. assert.equal(sum.channels(), 3);
  557. assert.equal(size.height, 100+1);
  558. assert.equal(size.width, 100+1);
  559. size = sqSum.size();
  560. assert.equal(sqSum.channels(), 3);
  561. assert.equal(size.height, 100+1);
  562. assert.equal(size.width, 100+1);
  563. mat.delete();
  564. sum.delete();
  565. sqSum.delete();
  566. title.delete();
  567. }
  568. });
  569. QUnit.test('test_rotatedRectangleIntersection', function(assert) {
  570. {
  571. let dst = cv.Mat.zeros(80, 80, cv.CV_8U);
  572. let contours = new cv.MatVector();
  573. let hierarchy = new cv.Mat();
  574. let intersectionPoints = new cv.Mat();
  575. cv.drawMarker(dst, new cv.Point(40, 40), new cv.Scalar(255));
  576. cv.findContoursLinkRuns(dst,contours,hierarchy);
  577. let rr1 = cv.minAreaRect(contours.get(0));
  578. let rr2 = cv.minAreaRect(contours.get(0));
  579. let rr3 = new cv.RotatedRect({x: 40, y: 40}, {height: 10, width: 20}, 45);
  580. let intersectionType = cv.rotatedRectangleIntersection(rr1, rr2, intersectionPoints);
  581. // Verify result.
  582. assert.deepEqual(intersectionType, cv.INTERSECT_FULL);
  583. intersectionPoints.convertTo(intersectionPoints, cv.CV_32S);
  584. let intersectionPointsData = intersectionPoints.data32S;
  585. assert.deepEqual(intersectionPointsData[0], 40);
  586. assert.deepEqual(intersectionPointsData[1], 50);
  587. assert.deepEqual(intersectionPointsData[2], 30);
  588. assert.deepEqual(intersectionPointsData[3], 40);
  589. assert.deepEqual(intersectionPointsData[4], 40);
  590. assert.deepEqual(intersectionPointsData[5], 30);
  591. assert.deepEqual(intersectionPointsData[6], 50);
  592. assert.deepEqual(intersectionPointsData[7], 40);
  593. intersectionType = cv.rotatedRectangleIntersection(rr1, rr3, intersectionPoints);
  594. // Verify result.
  595. assert.deepEqual(intersectionType, cv.INTERSECT_PARTIAL);
  596. intersectionPoints.convertTo(intersectionPoints, cv.CV_32S);
  597. intersectionPointsData = intersectionPoints.data32S;
  598. assert.deepEqual(intersectionPointsData[0], 39);
  599. assert.deepEqual(intersectionPointsData[1], 31);
  600. assert.deepEqual(intersectionPointsData[2], 49);
  601. assert.deepEqual(intersectionPointsData[3], 41);
  602. assert.deepEqual(intersectionPointsData[4], 41);
  603. assert.deepEqual(intersectionPointsData[5], 49);
  604. assert.deepEqual(intersectionPointsData[6], 31);
  605. assert.deepEqual(intersectionPointsData[7], 39);
  606. dst.delete();
  607. contours.delete();
  608. hierarchy.delete();
  609. intersectionPoints.delete();
  610. }
  611. });
  612. QUnit.test('warpPolar', function(assert) {
  613. const lines = new cv.Mat(255, 255, cv.CV_8U, new cv.Scalar(0));
  614. for (let r = 0; r < lines.rows; r++) {
  615. lines.row(r).setTo(new cv.Scalar(r));
  616. }
  617. cv.warpPolar(lines, lines, { width: 5, height: 5 }, new cv.Point(2, 2), 3,
  618. cv.INTER_CUBIC | cv.WARP_FILL_OUTLIERS | cv.WARP_INVERSE_MAP);
  619. assert.ok(lines instanceof cv.Mat);
  620. assert.deepEqual(Array.from(lines.data), [
  621. 159, 172, 191, 210, 223,
  622. 146, 159, 191, 223, 236,
  623. 128, 128, 0, 0, 0,
  624. 109, 96, 64, 32, 19,
  625. 96, 83, 64, 45, 32
  626. ]);
  627. });
  628. QUnit.test('IntelligentScissorsMB', function(assert) {
  629. const lines = new cv.Mat(50, 100, cv.CV_8U, new cv.Scalar(0));
  630. lines.row(10).setTo(new cv.Scalar(255));
  631. assert.ok(lines instanceof cv.Mat);
  632. let tool = new cv.segmentation_IntelligentScissorsMB();
  633. tool.applyImage(lines);
  634. assert.ok(lines instanceof cv.Mat);
  635. lines.delete();
  636. tool.buildMap(new cv.Point(10, 10));
  637. let contour = new cv.Mat();
  638. tool.getContour(new cv.Point(50, 10), contour);
  639. assert.equal(contour.type(), cv.CV_32SC2);
  640. assert.ok(contour.total() == 41, contour.total());
  641. tool.getContour(new cv.Point(80, 10), contour);
  642. assert.equal(contour.type(), cv.CV_32SC2);
  643. assert.ok(contour.total() == 71, contour.total());
  644. });