test_mat.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import numpy as np
  4. import cv2 as cv
  5. import os
  6. import sys
  7. import unittest
  8. from tests_common import NewOpenCVTests
  9. try:
  10. if sys.version_info[:2] < (3, 0):
  11. raise unittest.SkipTest('Python 2.x is not supported')
  12. class MatTest(NewOpenCVTests):
  13. def test_mat_construct(self):
  14. data = np.random.random([10, 10, 3])
  15. #print(np.ndarray.__dictoffset__) # 0
  16. #print(cv.Mat.__dictoffset__) # 88 (> 0)
  17. #print(cv.Mat) # <class cv2.Mat>
  18. #print(cv.Mat.__base__) # <class 'numpy.ndarray'>
  19. mat_data0 = cv.Mat(data)
  20. assert isinstance(mat_data0, cv.Mat)
  21. assert isinstance(mat_data0, np.ndarray)
  22. self.assertEqual(mat_data0.wrap_channels, False)
  23. res0 = cv.utils.dumpInputArray(mat_data0)
  24. self.assertEqual(res0, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=300 dims(-1)=3 size(-1)=[10 10 3] type(-1)=CV_64FC1")
  25. mat_data1 = cv.Mat(data, wrap_channels=True)
  26. assert isinstance(mat_data1, cv.Mat)
  27. assert isinstance(mat_data1, np.ndarray)
  28. self.assertEqual(mat_data1.wrap_channels, True)
  29. res1 = cv.utils.dumpInputArray(mat_data1)
  30. self.assertEqual(res1, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=100 dims(-1)=2 size(-1)=10x10 type(-1)=CV_64FC3")
  31. mat_data2 = cv.Mat(mat_data1)
  32. assert isinstance(mat_data2, cv.Mat)
  33. assert isinstance(mat_data2, np.ndarray)
  34. self.assertEqual(mat_data2.wrap_channels, True) # fail if __array_finalize__ doesn't work
  35. res2 = cv.utils.dumpInputArray(mat_data2)
  36. self.assertEqual(res2, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=100 dims(-1)=2 size(-1)=10x10 type(-1)=CV_64FC3")
  37. def test_mat_construct_4d(self):
  38. data = np.random.random([5, 10, 10, 3])
  39. mat_data0 = cv.Mat(data)
  40. assert isinstance(mat_data0, cv.Mat)
  41. assert isinstance(mat_data0, np.ndarray)
  42. self.assertEqual(mat_data0.wrap_channels, False)
  43. res0 = cv.utils.dumpInputArray(mat_data0)
  44. self.assertEqual(res0, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=1500 dims(-1)=4 size(-1)=[5 10 10 3] type(-1)=CV_64FC1")
  45. mat_data1 = cv.Mat(data, wrap_channels=True)
  46. assert isinstance(mat_data1, cv.Mat)
  47. assert isinstance(mat_data1, np.ndarray)
  48. self.assertEqual(mat_data1.wrap_channels, True)
  49. res1 = cv.utils.dumpInputArray(mat_data1)
  50. self.assertEqual(res1, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=500 dims(-1)=3 size(-1)=[5 10 10] type(-1)=CV_64FC3")
  51. mat_data2 = cv.Mat(mat_data1)
  52. assert isinstance(mat_data2, cv.Mat)
  53. assert isinstance(mat_data2, np.ndarray)
  54. self.assertEqual(mat_data2.wrap_channels, True) # __array_finalize__ doesn't work
  55. res2 = cv.utils.dumpInputArray(mat_data2)
  56. self.assertEqual(res2, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=500 dims(-1)=3 size(-1)=[5 10 10] type(-1)=CV_64FC3")
  57. def test_mat_wrap_channels_fail(self):
  58. data = np.random.random([2, 3, 4, 520])
  59. mat_data0 = cv.Mat(data)
  60. assert isinstance(mat_data0, cv.Mat)
  61. assert isinstance(mat_data0, np.ndarray)
  62. self.assertEqual(mat_data0.wrap_channels, False)
  63. res0 = cv.utils.dumpInputArray(mat_data0)
  64. self.assertEqual(res0, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=12480 dims(-1)=4 size(-1)=[2 3 4 520] type(-1)=CV_64FC1")
  65. with self.assertRaises(cv.error):
  66. mat_data1 = cv.Mat(data, wrap_channels=True) # argument unable to wrap channels, too high (520 > CV_CN_MAX=512)
  67. res1 = cv.utils.dumpInputArray(mat_data1)
  68. print(mat_data1.__dict__)
  69. print(res1)
  70. def test_ufuncs(self):
  71. data = np.arange(10)
  72. mat_data = cv.Mat(data)
  73. mat_data2 = 2 * mat_data
  74. self.assertEqual(type(mat_data2), cv.Mat)
  75. np.testing.assert_equal(2 * data, 2 * mat_data)
  76. def test_comparison(self):
  77. # Undefined behavior, do NOT use that.
  78. # Behavior may be changed in the future
  79. data = np.ones((10, 10, 3))
  80. mat_wrapped = cv.Mat(data, wrap_channels=True)
  81. mat_simple = cv.Mat(data)
  82. np.testing.assert_equal(mat_wrapped, mat_simple) # ???: wrap_channels is not checked for now
  83. np.testing.assert_equal(data, mat_simple)
  84. np.testing.assert_equal(data, mat_wrapped)
  85. #self.assertEqual(mat_wrapped, mat_simple) # ???
  86. #self.assertTrue(mat_wrapped == mat_simple) # ???
  87. #self.assertTrue((mat_wrapped == mat_simple).all())
  88. except unittest.SkipTest as e:
  89. message = str(e)
  90. class TestSkip(unittest.TestCase):
  91. def setUp(self):
  92. self.skipTest('Skip tests: ' + message)
  93. def test_skip():
  94. pass
  95. pass
  96. if __name__ == '__main__':
  97. NewOpenCVTests.bootstrap()