test_utils.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // Author: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
  42. //
  43. // LICENSE AGREEMENT
  44. // Copyright (c) 2015 The Regents of the University of California (Regents)
  45. //
  46. // Redistribution and use in source and binary forms, with or without
  47. // modification, are permitted provided that the following conditions are met:
  48. // 1. Redistributions of source code must retain the above copyright
  49. // notice, this list of conditions and the following disclaimer.
  50. // 2. Redistributions in binary form must reproduce the above copyright
  51. // notice, this list of conditions and the following disclaimer in the
  52. // documentation and/or other materials provided with the distribution.
  53. // 3. Neither the name of the University nor the
  54. // names of its contributors may be used to endorse or promote products
  55. // derived from this software without specific prior written permission.
  56. //
  57. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
  58. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  59. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  60. // DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
  61. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  62. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  63. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  64. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  65. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  66. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  67. //
  68. if (typeof module !== 'undefined' && module.exports) {
  69. // The environment is Node.js
  70. var cv = require('./opencv.js'); // eslint-disable-line no-var
  71. }
  72. QUnit.module('Utils', {});
  73. QUnit.test('Test vectors', function(assert) {
  74. {
  75. let pointVector = new cv.PointVector();
  76. for (let i=0; i<100; ++i) {
  77. pointVector.push_back({x: i, y: 2*i});
  78. }
  79. assert.equal(pointVector.size(), 100);
  80. let index = 10;
  81. let item = pointVector.get(index);
  82. assert.equal(item.x, index);
  83. assert.equal(item.y, 2*index);
  84. index = 0;
  85. item = pointVector.get(index);
  86. assert.equal(item.x, index);
  87. assert.equal(item.y, 2*index);
  88. index = 99;
  89. item = pointVector.get(index);
  90. assert.equal(item.x, index);
  91. assert.equal(item.y, 2*index);
  92. pointVector.delete();
  93. }
  94. {
  95. let pointVector = new cv.PointVector();
  96. for (let i=0; i<100; ++i) {
  97. pointVector.push_back(new cv.Point(i, 2*i));
  98. }
  99. pointVector.push_back(new cv.Point());
  100. assert.equal(pointVector.size(), 101);
  101. let index = 10;
  102. let item = pointVector.get(index);
  103. assert.equal(item.x, index);
  104. assert.equal(item.y, 2*index);
  105. index = 0;
  106. item = pointVector.get(index);
  107. assert.equal(item.x, index);
  108. assert.equal(item.y, 2*index);
  109. index = 99;
  110. item = pointVector.get(index);
  111. assert.equal(item.x, index);
  112. assert.equal(item.y, 2*index);
  113. index = 100;
  114. item = pointVector.get(index);
  115. assert.equal(item.x, 0);
  116. assert.equal(item.y, 0);
  117. pointVector.delete();
  118. }
  119. });
  120. QUnit.test('Test Rect', function(assert) {
  121. let rectVector = new cv.RectVector();
  122. let rect = {x: 1, y: 2, width: 3, height: 4};
  123. rectVector.push_back(rect);
  124. rectVector.push_back(new cv.Rect());
  125. rectVector.push_back(new cv.Rect(rect));
  126. rectVector.push_back(new cv.Rect({x: 5, y: 6}, {width: 7, height: 8}));
  127. rectVector.push_back(new cv.Rect(9, 10, 11, 12));
  128. assert.equal(rectVector.size(), 5);
  129. let item = rectVector.get(0);
  130. assert.equal(item.x, 1);
  131. assert.equal(item.y, 2);
  132. assert.equal(item.width, 3);
  133. assert.equal(item.height, 4);
  134. item = rectVector.get(1);
  135. assert.equal(item.x, 0);
  136. assert.equal(item.y, 0);
  137. assert.equal(item.width, 0);
  138. assert.equal(item.height, 0);
  139. item = rectVector.get(2);
  140. assert.equal(item.x, 1);
  141. assert.equal(item.y, 2);
  142. assert.equal(item.width, 3);
  143. assert.equal(item.height, 4);
  144. item = rectVector.get(3);
  145. assert.equal(item.x, 5);
  146. assert.equal(item.y, 6);
  147. assert.equal(item.width, 7);
  148. assert.equal(item.height, 8);
  149. item = rectVector.get(4);
  150. assert.equal(item.x, 9);
  151. assert.equal(item.y, 10);
  152. assert.equal(item.width, 11);
  153. assert.equal(item.height, 12);
  154. rectVector.delete();
  155. });
  156. QUnit.test('Test Size', function(assert) {
  157. {
  158. let mat = new cv.Mat();
  159. mat.create({width: 5, height: 10}, cv.CV_8UC4);
  160. let size = mat.size();
  161. assert.ok(mat.type() === cv.CV_8UC4);
  162. assert.ok(size.height === 10);
  163. assert.ok(size.width === 5);
  164. assert.ok(mat.channels() === 4);
  165. mat.delete();
  166. }
  167. {
  168. let mat = new cv.Mat();
  169. mat.create(new cv.Size(5, 10), cv.CV_8UC4);
  170. let size = mat.size();
  171. assert.ok(mat.type() === cv.CV_8UC4);
  172. assert.ok(size.height === 10);
  173. assert.ok(size.width === 5);
  174. assert.ok(mat.channels() === 4);
  175. mat.delete();
  176. }
  177. });
  178. QUnit.test('test_rotated_rect', function(assert) {
  179. {
  180. let rect = {center: {x: 100, y: 100}, size: {height: 100, width: 50}, angle: 30};
  181. assert.equal(rect.center.x, 100);
  182. assert.equal(rect.center.y, 100);
  183. assert.equal(rect.angle, 30);
  184. assert.equal(rect.size.height, 100);
  185. assert.equal(rect.size.width, 50);
  186. }
  187. {
  188. let rect = new cv.RotatedRect();
  189. assert.equal(rect.center.x, 0);
  190. assert.equal(rect.center.y, 0);
  191. assert.equal(rect.angle, 0);
  192. assert.equal(rect.size.height, 0);
  193. assert.equal(rect.size.width, 0);
  194. let points = cv.RotatedRect.points(rect);
  195. assert.equal(points[0].x, 0);
  196. assert.equal(points[0].y, 0);
  197. assert.equal(points[1].x, 0);
  198. assert.equal(points[1].y, 0);
  199. assert.equal(points[2].x, 0);
  200. assert.equal(points[2].y, 0);
  201. assert.equal(points[3].x, 0);
  202. assert.equal(points[3].y, 0);
  203. }
  204. {
  205. let rect = new cv.RotatedRect({x: 100, y: 100}, {height: 100, width: 50}, 30);
  206. assert.equal(rect.center.x, 100);
  207. assert.equal(rect.center.y, 100);
  208. assert.equal(rect.angle, 30);
  209. assert.equal(rect.size.height, 100);
  210. assert.equal(rect.size.width, 50);
  211. let points = cv.RotatedRect.points(rect);
  212. assert.equal(points[0].x, cv.RotatedRect.boundingRect2f(rect).x);
  213. assert.equal(points[1].y, cv.RotatedRect.boundingRect2f(rect).y);
  214. }
  215. });