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