Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- from collections import deque
- import numpy as np
- import cv2
- Point = namedtuple('Point', ['x', 'y'])
- def grassfire(start, image, labels, label):
- if labels[start.y, start.x] != 0:
- # Burned already, just return.
- return False
- burnlist = deque([start])
- while len(burnlist) > 0:
- p = burnlist.popleft()
- labels[p.y, p.x] = label
- # Look right
- if image[p.y, p.x+1] > 0 and labels[p.y, p.x+1] == 0:
- burnlist.append(Point(p.x+1, p.y))
- # Look down
- if image[p.y+1, p.x] > 0 and labels[p.y+1, p.x] == 0:
- burnlist.append(Point(p.x, p.y+1))
- # Look left
- if image[p.y, p.x-1] > 0 and labels[p.y, p.x-1] == 0:
- burnlist.append(Point(p.x-1, p.y))
- # Look up
- if image[p.y-1, p.x] > 0 and labels[p.y-1, p.x] == 0:
- burnlist.append(Point(p.x, p.y-1))
- return True
- img = cv2.imread("grassfire.png", 0)
- labels = np.zeros(img.shape, np.uint8)
- print(img)
- nextlabel = 1
- for y, row in enumerate(img):
- for x, pixel in enumerate(row):
- if pixel > 0:
- print("Witch! Burn %d,%d!" % (x,y))
- if(grassfire(Point(x,y), img, labels, nextlabel)):
- nextlabel += 1
- print(labels)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement