You are here

Přidat komentář

https://jenda.hrach.eu/f/bugemos-589.png


import cv2
import numpy as np
import queue

im = cv2.imread("Stresova-Zlomalovanka.png")

def darkcolor():
__a = np.random.uniform(0, 127, size=3)
__return a

def cond(p):
__return (p>127).all()

def dfsfill(im,x,y,c):
__if not cond(im[x,y]):
____return
__q = queue.Queue()
__q.put((x,y))
__while not q.empty():
____x,y = q.get()
____if x>=1 and cond(im[x-1, y]):
______im[x-1, y] = c
______q.put((x-1, y))
____if y>=1 and cond(im[x,y-1]):
______im[x, y-1] = c
______q.put((x, y-1))
____if x<im.shape[0]-1 and cond(im[x+1,y]):
______im[x+1, y] = c
______q.put((x+1, y))
____if y<im.shape[1]-1 and cond(im[x,y+1]):
______im[x, y+1] = c
______q.put((x, y+1))

for x in range(im.shape[0]):
__for y in range(im.shape[1]):
____dfsfill(im, x, y, darkcolor())
__print("fill %i"%x)

cv2.imwrite("filled.png", im)

  • nahraďte _ za mezery, místní CMS umí jenom tag <code> a ne <pre> tak jsem odsazení nedokázal zadat
  • nepovedlo se mi uspokojivě použít cv2.floodFill, ten threshold nefunguje podle mých představ a masku se mi zkoumat nechtělo
  • tupé rekurzivní DFS narazilo na limit stacku v Pythonu
  • celkově je to šíleně pomalé, příště to napíšu v nějakém vhodnějším jazyce