OpenCVTest.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. % Matlab binding test cases
  2. % Uses Matlab's builtin testing framework
  3. classdef OpenCVTest < matlab.unittest.TestCase
  4. methods(Test)
  5. % -------------------------------------------------------------------------
  6. % EXCEPTIONS
  7. % Check that errors and exceptions are thrown correctly
  8. % -------------------------------------------------------------------------
  9. % check that std exception is thrown
  10. function stdException(testcase)
  11. try
  12. std_exception();
  13. testcase.verifyFail();
  14. catch
  15. % TODO: Catch more specific exception
  16. testcase.verifyTrue(true);
  17. end
  18. end
  19. % check that OpenCV exceptions are correctly caught
  20. function cvException(testcase)
  21. try
  22. cv_exception();
  23. testcase.verifyFail();
  24. catch
  25. % TODO: Catch more specific exception
  26. testcase.verifyTrue(true);
  27. end
  28. end
  29. % check that all exceptions are caught
  30. function allException(testcase)
  31. try
  32. exception();
  33. testcase.verifyFail();
  34. catch
  35. % TODO: Catch more specific exception
  36. testcase.verifyTrue(true);
  37. end
  38. end
  39. % -------------------------------------------------------------------------
  40. % SIZES AND FILLS
  41. % Check that matrices are correctly filled and resized
  42. % -------------------------------------------------------------------------
  43. % check that a matrix is correctly filled with random numbers
  44. function randomFill(testcase)
  45. sz = [7 11];
  46. mat = zeros(sz);
  47. mat = cv.randn(mat, 0, 1);
  48. testcase.verifyEqual(size(mat), sz, 'Matrix should not change size');
  49. testcase.verifyNotEqual(mat, zeros(sz), 'Matrix should be nonzero');
  50. end
  51. function transpose(testcase)
  52. m = randn(19, 81);
  53. mt1 = transpose(m);
  54. mt2 = cv.transpose(m);
  55. testcase.verifyEqual(size(mt1), size(mt2), 'Matrix transposed to incorrect dimensionality');
  56. testcase.verifyLessThan(norm(mt1 - mt2), 1e-8, 'Too much precision lost in tranposition');
  57. end
  58. % multiple return
  59. function multipleReturn(testcase)
  60. A = randn(10);
  61. A = A'*A;
  62. [V1, D1] = eig(A); D1 = diag(D1);
  63. [~, D2, V2] = cv.eigen(A);
  64. testcase.verifyLessThan(norm(V1 - V2), 1e-6, 'Too much precision lost in eigenvectors');
  65. testcase.verifyLessThan(norm(D1 - D2), 1e-6, 'Too much precision lost in eigenvalues');
  66. end
  67. % complex output from SVD
  68. function complexOutputSVD(testcase)
  69. A = randn(10);
  70. [V1, D1] = eig(A);
  71. [~, D2, V2] = cv.eigen(A);
  72. testcase.verifyTrue(~isreal(V2) && size(V2,3) == 1, 'Output should be complex');
  73. testcase.verifyLessThan(norm(V1 - V2), 1e-6, 'Too much precision lost in eigenvectors');
  74. end
  75. % complex output from Fourier Transform
  76. function complexOutputFFT(testcase)
  77. A = randn(10);
  78. F1 = fft2(A);
  79. F2 = cv.dft(A, cv.DFT_COMPLEX_OUTPUT);
  80. testcase.verifyTrue(~isreal(F2) && size(F2,3) == 1, 'Output should be complex');
  81. testcase.verifyLessThan(norm(F1 - F2), 1e-6, 'Too much precision lost in eigenvectors');
  82. end
  83. % -------------------------------------------------------------------------
  84. % TYPE CASTS
  85. % Check that types are correctly cast
  86. % -------------------------------------------------------------------------
  87. % -------------------------------------------------------------------------
  88. % PRECISION
  89. % Check that basic operations are performed with sufficient precision
  90. % -------------------------------------------------------------------------
  91. % check that summing elements is within reasonable precision
  92. function sumElements(testcase)
  93. a = randn(5000);
  94. b = sum(a(:));
  95. c = cv.sum(a);
  96. testcase.verifyLessThan(norm(b - c), 1e-8, 'Matrix reduction with insufficient precision');
  97. end
  98. % check that adding two matrices is within reasonable precision
  99. function addPrecision(testcase)
  100. a = randn(50);
  101. b = randn(50);
  102. c = a+b;
  103. d = cv.add(a, b);
  104. testcase.verifyLessThan(norm(c - d), 1e-8, 'Matrices are added with insufficient precision');
  105. end
  106. % check that performing gemm is within reasonable precision
  107. function gemmPrecision(testcase)
  108. a = randn(10, 50);
  109. b = randn(50, 10);
  110. c = randn(10, 10);
  111. alpha = 2.71828;
  112. gamma = 1.61803;
  113. d = alpha*a*b + gamma*c;
  114. e = cv.gemm(a, b, alpha, c, gamma);
  115. testcase.verifyLessThan(norm(d - e), 1e-8, 'Matrices are multiplied with insufficient precision');
  116. end
  117. % -------------------------------------------------------------------------
  118. % MISCELLANEOUS
  119. % Miscellaneous tests
  120. % -------------------------------------------------------------------------
  121. % check that cv::waitKey waits for at least specified time
  122. function waitKey(testcase)
  123. tic();
  124. cv.waitKey(500);
  125. elapsed = toc();
  126. testcase.verifyGreaterThan(elapsed, 0.5, 'Elapsed time should be at least 0.5 seconds');
  127. end
  128. % check that highgui window can be created and destroyed
  129. function createAndDestroyWindow(testcase)
  130. try
  131. cv.namedWindow('test window');
  132. catch
  133. testcase.verifyFail('could not create window');
  134. end
  135. try
  136. cv.destroyWindow('test window');
  137. catch
  138. testcase.verifyFail('could not destroy window');
  139. end
  140. testcase.verifyTrue(true);
  141. end
  142. end
  143. end