test_copytomask.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. '''
  3. Test for copyto with mask
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import numpy as np
  8. import cv2 as cv
  9. import sys
  10. from tests_common import NewOpenCVTests
  11. class copytomask_test(NewOpenCVTests):
  12. def test_copytomask(self):
  13. img = self.get_sample('python/images/baboon.png', cv.IMREAD_COLOR)
  14. eps = 0.
  15. #Create mask using inRange
  16. valeurBGRinf = np.array([0,0,100])
  17. valeurBGRSup = np.array([70, 70,255])
  18. maskRed = cv.inRange(img, valeurBGRinf, valeurBGRSup)
  19. #New binding
  20. dstcv = np.ndarray(np.array((2, 2, 1))*img.shape, dtype=img.dtype)
  21. dstcv.fill(255)
  22. cv.copyTo(img, maskRed, dstcv[:img.shape[0],:img.shape[1],:])
  23. #using numpy
  24. dstnp = np.ndarray(np.array((2, 2, 1))*img.shape, dtype=img.dtype)
  25. dstnp.fill(255)
  26. mask2=maskRed.astype(bool)
  27. _, mask_b = np.broadcast_arrays(img, mask2[..., None])
  28. np.copyto(dstnp[:img.shape[0],:img.shape[1],:], img, where=mask_b)
  29. self.assertEqual(cv.norm(dstnp ,dstcv), eps)
  30. if __name__ == '__main__':
  31. NewOpenCVTests.bootstrap()