1.GlobalLocalImageCompletion下简易GUI
https://github.com/shinseung428/GlobalLocalImageCompletion_TF/blob/master/test.py
核心代码:
drawing = False # true if mouse is pressed
ix,iy = -1,-1
color = (255,255,255)
size = 10
def erase_img(args, img):
# mouse callback function
def erase_rect(event,x,y,flags,param):
global ix,iy,drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
if drawing == True:
# cv2.circle(img,(x,y),10,(255,255,255),-1)
cv2.rectangle(img,(x-size,y-size),(x+size,y+size),color,-1)
cv2.rectangle(mask,(x-size,y-size),(x+size,y+size),color,-1)
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
# cv2.circle(img,(x,y),10,(255,255,255),-1)
cv2.rectangle(img,(x-size,y-size),(x+size,y+size),color,-1)
cv2.rectangle(mask,(x-size,y-size),(x+size,y+size),color,-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
# cv2.circle(img,(x,y),10,(255,255,255),-1)
cv2.rectangle(img,(x-size,y-size),(x+size,y+size),color,-1)
cv2.rectangle(mask,(x-size,y-size),(x+size,y+size),color,-1)
cv2.namedWindow('image')
cv2.setMouseCallback('image',erase_rect)
#cv2.namedWindow('mask')
cv2.setMouseCallback('mask',erase_rect)
mask = np.zeros(img.shape)
while(1):
img_show = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('image',img_show)
k = cv2.waitKey(1) & 0xFF
if k == 10:
break
cv2.destroyAllWindows()
return ...
2.自己根据参考制作版本
def draw_circle(event, x, y, flags, param):
global ix, iy, drawing, drawing_edge
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix, iy = x, y
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing:
cv2.circle(mask[0, :, :, 0], (x, y), radius, 1, -1)
else:
pass
if event == cv2.EVENT_MBUTTONDOWN:
print("click middle key to draw edge")
drawing_edge = True
ix, iy = x, y
# if event == cv2.EVENT_MBUTTONDOWN:
# print("click middle key to draw edge")
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_MBUTTON:
if drawing_edge:
# cv2.circle(guidance[0, :, :, 0], (x, y), radius_edge, 1, -1)
cv2.line(guidance[0, :, :, 0], (ix, iy), (x, y), color=1, thickness=1)
ix, iy = x, y
else:
pass