Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from collections import namedtuple
  2. from collections import deque
  3.  
  4. import numpy as np
  5. import cv2
  6.  
  7. Point = namedtuple('Point', ['x', 'y'])
  8.  
  9. def grassfire(start, image, labels, label):
  10.     if labels[start.y, start.x] != 0:
  11.         # Burned already, just return.
  12.         return False
  13.     burnlist = deque([start])
  14.  
  15.     while len(burnlist) > 0:
  16.         p = burnlist.popleft()
  17.         labels[p.y, p.x] = label
  18.         # Look right
  19.         if image[p.y, p.x+1] > 0 and labels[p.y, p.x+1] == 0:
  20.             burnlist.append(Point(p.x+1, p.y))
  21.         # Look down
  22.         if image[p.y+1, p.x] > 0 and labels[p.y+1, p.x] == 0:
  23.             burnlist.append(Point(p.x, p.y+1))
  24.         # Look left
  25.         if image[p.y, p.x-1] > 0 and labels[p.y, p.x-1] == 0:
  26.             burnlist.append(Point(p.x-1, p.y))
  27.         # Look up
  28.         if image[p.y-1, p.x] > 0 and labels[p.y-1, p.x] == 0:
  29.             burnlist.append(Point(p.x, p.y-1))
  30.     return True
  31.  
  32.  
  33. img = cv2.imread("grassfire.png", 0)
  34. labels = np.zeros(img.shape, np.uint8)
  35.  
  36. print(img)
  37.  
  38. nextlabel = 1
  39. for y, row in enumerate(img):
  40.     for x, pixel in enumerate(row):
  41.         if pixel > 0:
  42.             print("Witch! Burn %d,%d!" % (x,y))
  43.             if(grassfire(Point(x,y), img, labels, nextlabel)):
  44.                 nextlabel += 1
  45.  
  46. print(labels)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement