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