inpaint.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. '''
  3. Inpainting sample.
  4. Inpainting repairs damage to images by floodfilling
  5. the damage with surrounding image areas.
  6. Usage:
  7. inpaint.py [<image>]
  8. Keys:
  9. SPACE - inpaint
  10. r - reset the inpainting mask
  11. ESC - exit
  12. '''
  13. # Python 2/3 compatibility
  14. from __future__ import print_function
  15. import numpy as np
  16. import cv2
  17. from common import Sketcher
  18. if __name__ == '__main__':
  19. import sys
  20. try:
  21. fn = sys.argv[1]
  22. except:
  23. fn = '../data/fruits.jpg'
  24. print(__doc__)
  25. img = cv2.imread(fn)
  26. if img is None:
  27. print('Failed to load image file:', fn)
  28. sys.exit(1)
  29. img_mark = img.copy()
  30. mark = np.zeros(img.shape[:2], np.uint8)
  31. sketch = Sketcher('img', [img_mark, mark], lambda : ((255, 255, 255), 255))
  32. while True:
  33. ch = 0xFF & cv2.waitKey()
  34. if ch == 27:
  35. break
  36. if ch == ord(' '):
  37. res = cv2.inpaint(img_mark, mark, 3, cv2.INPAINT_TELEA)
  38. cv2.imshow('inpaint', res)
  39. if ch == ord('r'):
  40. img_mark[:] = img
  41. mark[:] = 0
  42. sketch.show()
  43. cv2.destroyAllWindows()