import os
import cv2
import pyautogui
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input_dir', type=str, default='C:/Users/AbaKir/Downloads/images/images/')
parser.add_argument('--output_dir', type=str, default='C:/Users/AbaKir/Downloads/images/masks_h/')
parser.add_argument('--wait_time', type=int, default=100)
args = parser.parse_args()
def drawxxx(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
# print('你单机了鼠标左键,鼠标坐标为:%s %s' % (x, y))
mask = np.zeros([img.shape[0] + 2, img.shape[1] + 2, 1], np.uint8)
cv2.floodFill(img, mask, (x, y), (0, 0, 255), (20, 20, 20), (20, 20, 20), cv2.FLOODFILL_FIXED_RANGE)
cv2.imshow('img', img)
cv2.waitKey(args.wait_time)
mask = np.zeros_like(img)
mask[(img[:, :, 0] == 0) & (img[:, :, 1] == 0) & (img[:, :, 2] == 255)] = 255
cv2.imshow('img', mask)
if args.output_dir != '':
cv2.imwrite(args.output_dir + img_name, mask)
cv2.waitKey(args.wait_time)
pyautogui.press('enter')
if __name__ == '__main__':
img_ls = os.listdir(args.input_dir)
for img_name in img_ls:
img = cv2.imread(args.input_dir+img_name)
cv2.imshow('img', img)
cv2.setMouseCallback("img", drawxxx, (img, img_name)) # 注册鼠标监听事件(窗口,回调函数)
while cv2.waitKey() != 13:
pass