From b92bee11394d09e1af1e6fd9f1db745edabfd444 Mon Sep 17 00:00:00 2001 From: rainfly Date: Sun, 3 Dec 2017 10:36:27 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E8=8A=B1=E5=9E=8B=E5=A1=AB=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\261\345\236\213\345\241\253\350\211\262" | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 "\350\212\261\345\236\213\345\241\253\350\211\262" diff --git "a/\350\212\261\345\236\213\345\241\253\350\211\262" "b/\350\212\261\345\236\213\345\241\253\350\211\262" new file mode 100644 index 00000000..832d0a54 --- /dev/null +++ "b/\350\212\261\345\236\213\345\241\253\350\211\262" @@ -0,0 +1,90 @@ +#!/usr/bin/env python + +''' +Floodfill sample. +Usage: + floodfill.py [] + Click on the image to set seed point +Keys: + f - toggle floating range + c - toggle 4/8 connectivity + ESC - exit +''' + +# Python 2/3 compatibility +from __future__ import print_function + +import numpy as np +import cv2 +import time + +if __name__ == '__main__': + import sys + try: + fn = sys.argv[1] + except: + fn = 'flower.jpg' + print(__doc__) + + img = cv2.imread(fn, True) + if img is None: + print('Failed to load image file:', fn) + sys.exit(1) + + h, w = img.shape[:2] + mask = np.zeros((h+2, w+2), np.uint8) + seed_pt = None + fixed_range = True + connectivity = 8 + + + def update(dummy=None): + if seed_pt is None: + cv2.imshow('floodfill', img) + return + flooded = img.copy() + mask[:] = 0 + lo = cv2.getTrackbarPos('lo', 'floodfill') + hi = cv2.getTrackbarPos('hi', 'floodfill') + flags = connectivity + if fixed_range: + flags |= cv2.FLOODFILL_FIXED_RANGE + cv2.floodFill(flooded, mask, seed_pt, (255, 0, 0), (lo,)*3, (hi,)*3, flags) + cv2.circle(flooded, seed_pt, 2, (0, 0, 255), -1) + cv2.imshow('floodfill', flooded) + + def onmouse(event, x, y, flags, param): + global seed_pt + if flags & cv2.EVENT_FLAG_LBUTTON: + seed_pt = x, y + update() + + update() + + cv2.setMouseCallback('floodfill', onmouse) + cv2.createTrackbar('lo', 'floodfill', 20, 255, update) + cv2.createTrackbar('hi', 'floodfill', 40, 255, update) + gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) + g=cv2.GaussianBlur(gray,(3,3),0); + canny = cv2.Canny(g,30,100); + contours, hierarchy = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) + cv2.drawContours(img,contours,-1,(0,0,0),1) + #cv2.imshow('floodfill', img) + + while True: + ch = cv2.waitKey() + if ch == 27: + break + if ch == ord('s'): + temp = img.copy() + cv2.drawContours(temp,contours,-1,(0,0,255),3) + cv2.imshow('flofill', temp) + if ch == ord('f'): + fixed_range = not fixed_range + print('using %s range' % ('floating', 'fixed')[fixed_range]) + update() + if ch == ord('c'): + connectivity = 12-connectivity + print('connectivity =', connectivity) + update() +cv2.destroyAllWindows()