Facebook
From Burly Anoa, 5 Days ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 33
  1. from pylab import *
  2. from Image import *
  3. from numpy import *
  4. from scipy import ndimage
  5.  
  6. img = asarray(open("zima.jpg").convert("L"))
  7. binary = img > 128
  8.  
  9. a = zeros((3,3))
  10. structs = [
  11.     ndimage.generate_binary_structure(2, 1),
  12.     ndimage.generate_binary_structure(2, 2)
  13.     ]
  14.  
  15. i = 1
  16.  
  17. for struct in structs:
  18.     for j in range(1, 10, 3):
  19.         dilation = ndimage.binary_dilation(binary, iterations=j,
  20. structure=struct).astype(binary.dtype)
  21.         erosion = ndimage.binary_erosion(binary, iterations=j,
  22. structure=struct).astype(binary.dtype)
  23.         opening = ndimage.binary_opening(binary, iterations=j,
  24. structure=struct).astype(binary.dtype)
  25.         closing = ndimage.binary_closing(binary, iterations=j,
  26. structure=struct).astype(binary.dtype)
  27.         openingclosing = ndimage.binary_closing(opening, iterations=j,
  28. structure=struct).astype(binary.dtype)
  29.  
  30.         figure(i)
  31.  
  32.         subplot(3,2,1)
  33.         imshow(binary, cmap="gray", vmin=0, vmax=1)
  34.         title("BASE")
  35.  
  36.         subplot(3,2,2)
  37.         imshow(dilation, cmap="gray", vmin=0, vmax=1)
  38.         title("DYLATACJA")
  39.  
  40.         subplot(3,2,3)
  41.         imshow(erosion, cmap="gray", vmin=0, vmax=1)
  42.         title("EROZJA")
  43.  
  44.         subplot(3,2,4)
  45.         imshow(opening, cmap="gray", vmin=0, vmax=1)
  46.         title("OTWARCIE")
  47.  
  48.         subplot(3,2,5)
  49.         imshow(closing, cmap="gray", vmin=0, vmax=1)
  50.         title("ZAMKNIECIE")
  51.  
  52.         subplot(3,2,6)
  53.         imshow(openingclosing, cmap="gray", vmin=0, vmax=1)
  54.         title("OTWARCIE + ZAMKNIECIE")
  55.  
  56.         i+=1
  57.         suptitle("ITERATIONS = " + str(j))
  58.         tight_layout()
  59.         subplots_adjust(top=0.88)
  60.         show()