inpaint.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 as cv
  17. from common import Sketcher
  18. def main():
  19. import sys
  20. try:
  21. fn = sys.argv[1]
  22. except:
  23. fn = 'fruits.jpg'
  24. img = cv.imread(cv.samples.findFile(fn))
  25. if img is None:
  26. print('Failed to load image file:', fn)
  27. sys.exit(1)
  28. img_mark = img.copy()
  29. mark = np.zeros(img.shape[:2], np.uint8)
  30. sketch = Sketcher('img', [img_mark, mark], lambda : ((255, 255, 255), 255))
  31. while True:
  32. ch = cv.waitKey()
  33. if ch == 27:
  34. break
  35. if ch == ord(' '):
  36. res = cv.inpaint(img_mark, mark, 3, cv.INPAINT_TELEA)
  37. cv.imshow('inpaint', res)
  38. if ch == ord('r'):
  39. img_mark[:] = img
  40. mark[:] = 0
  41. sketch.show()
  42. print('Done')
  43. if __name__ == '__main__':
  44. print(__doc__)
  45. main()
  46. cv.destroyAllWindows()