11-5Repair_photo.py

000: import cv2
001: import numpy as np
002:
003: def __main():
004: global mask, img
005: cv2.namedWindow(“Repair”)
006: cv2.setMouseCallback(“Repair”, onMouse, None)
007:
008: while True:
009: img = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
010: cv2.imshow(“Repair”, img)
011: if cv2.waitKey(10) > -1:
012: break
013:
014: def onMouse(event, x, y, flags, param):
015: global drawing # 割込みはここでグローバル宣言する
016:
017: if event == cv2.EVENT_MOUSEMOVE:
018: print(“x = “, x)
019: print(“y = “, y)
020: # 左マウスボタンを押しながらマウス移動したら描画する
021: if drawing:
022: cv2.circle(mask, (x, y), 3, 255, -1)
023: # 左マウスボタンを押した場合
024: elif event == cv2.EVENT_LBUTTONDOWN:
025: drawing = True
026: # 右マウスボタンを押した場合
027: elif event == cv2.EVENT_LBUTTONUP:
028: drawing = False
029:
030: if __name__ == ‘__main__’:
031: print(cv2.__version__)
032:
033: img = cv2.imread(‘../IMG_0262s.JPG’)
034: h, w, c = img.shape
035: mask = np.zeros((h, w, 1), dtype=np.uint8)
036: drawing = False
037:
038: __main()