2 from opencv.cv import *
3 from opencv.highgui import *
10 prev_pt = cvPoint(-1,-1)
12 def on_mouse( event, x, y, flags, param ):
17 if event == CV_EVENT_LBUTTONUP or not (flags & CV_EVENT_FLAG_LBUTTON):
18 prev_pt = cvPoint(-1,-1)
19 elif event == CV_EVENT_LBUTTONDOWN:
20 prev_pt = cvPoint(x,y)
21 elif event == CV_EVENT_MOUSEMOVE and (flags & CV_EVENT_FLAG_LBUTTON) :
25 cvLine( inpaint_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 )
26 cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 )
28 cvShowImage( "image", img )
30 if __name__=="__main__":
31 filename = "../c/fruits.jpg"
32 if len(sys.argv) >= 2:
33 filename = sys.argv[1]
35 img0 = cvLoadImage(filename,-1)
37 print "Can't open image '%s'" % filename
41 print "\tESC - quit the program"
42 print "\tr - restore the original image"
43 print "\ti or ENTER - run inpainting algorithm"
44 print "\t\t(before running it, paint something on the image)"
46 cvNamedWindow( "image", 1 )
48 img = cvCloneImage( img0 )
49 inpainted = cvCloneImage( img0 )
50 inpaint_mask = cvCreateImage( cvGetSize(img), 8, 1 )
52 cvZero( inpaint_mask )
54 cvShowImage( "image", img )
55 cvShowImage( "watershed transform", inpainted )
56 cvSetMouseCallback( "image", on_mouse, None )
61 if c == '\x1b' or c == 'q':
65 cvZero( inpaint_mask )
67 cvShowImage( "image", img )
69 if c == 'i' or c == '\012':
70 cvNamedWindow( "inpainted image", 1 )
71 cvInpaint( img, inpaint_mask, inpainted, 3, CV_INPAINT_TELEA )
72 cvShowImage( "inpainted image", inpainted )