daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest May 16th, 2018 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed May 16 13:44:06 2018
  4.  
  5. @author: Olejnik
  6. """
  7. #import os
  8. #import numpy as np
  9. #import matplotlib.image as img
  10. #import matplotlib.pyplot as plt
  11. #from scipy import misc as mc
  12. #
  13. #a = np.array([[1,1,1],[2,2,2],[2,3,1]])
  14. #b = np.array([[1,1,1],[2,2,2],[2,3,2]])
  15. #
  16. #c = np.multiply(a,b)
  17. #print(c)
  18.  
  19. # -*- coding: utf-8 -*-
  20. """
  21. Created on Sun May 13 21:08:02 2018
  22.  
  23. @author: Olejnik
  24. """
  25. import os
  26. import numpy as np
  27. import matplotlib.image as img
  28. import matplotlib.pyplot as plt
  29. from scipy import misc as mc
  30.  
  31.  
  32. os.chdir('C:\Users\olejn\Desktop\WI\Semestr4\Przetwarzanie obrazu\laboratorium\lab3')
  33. obr=img.imread('lab3.png')
  34.  
  35.  
  36. def fmask(obr, mask):
  37.     #rozmiar maski shape[0] - wiersz, shape[1] - kolumna
  38.     w_mask = mask.shape[0]
  39.     h_mask = mask.shape[1]
  40.     w_obr = obr.shape[0]
  41.     h_obr = obr.shape[1]
  42.     obr2 = obr.copy()
  43.     newobr=np.zeros((h_obr,w_obr))
  44.     norma = np.sum(mask)
  45.     if norma == 0:
  46.         norma = 1
  47.     for x in range (0, w_obr-3):
  48.         for y in range (0, h_obr-3):
  49.             wymnozone = mask * obr2[y:y+h_mask,x:x+w_mask]
  50.             dodane = np.sum(wymnozone)
  51.             piksel = dodane/norma
  52.             newobr[y,x] = piksel
  53.     return newobr
  54.  
  55.    
  56. def ffilter(obr, mask):
  57.     ileMasek = mask.shape[0]
  58.     out = np.zeros(obr.shape)
  59.     for i in range(0, ileMasek):
  60.         out = out + np.abs(fmask(obr.copy(), mask[i, :, :]))
  61.     out = np.abs(out /ileMasek)
  62.     return out
  63.  
  64. def wyswietl(obrOrg, obr, title):
  65.     plt.subplot(1,2,1)
  66.     plt.imshow(obrOrg, cmap=plt.cm.gray, vmin=0, vmax=255, aspect='auto')
  67.     plt.title('Oryginal')
  68.     plt.subplot(1,2,2)
  69.     plt.imshow(obr, cmap=plt.cm.gray, vmin=0, vmax=255, aspect='auto')
  70.     plt.title(title)
  71.     plt.show()
  72.  
  73. #filtry krawędziowe
  74. def fRobertsa(obr):
  75.     #mask=np.array([[1,1,1],[1,4,1],[1,1,1]])
  76.     mask=np.zeros((4,2,2))
  77.     mask[0,:,:]=np.array([[-1,0],[1,0]])
  78.     mask[1,:,:]=np.array([[-1,1],[0,0]])
  79.     mask[2,:,:]=np.array([[0,1],[-1,0]])
  80.     mask[3,:,:]=np.array([[1,0],[0,-1]])
  81.     obr2 = ffilter(obr,mask)
  82.     return obr2
  83.  
  84. def fLaplacea(obr):
  85.     mask=np.zeros((1,3,3))
  86.     mask[0,:,:]=np.array([[0,1,0],[1,-4,1],[0,1,0]])
  87.     #mask[1,:,:]=np.array([[1,1,1],[1,-8,1],[1,1,1]])
  88.     obr2 = ffilter(obr,mask)
  89.     return obr2
  90.  
  91. def fPrewitta(obr):
  92.     mask=np.zeros((4,3,3))
  93.     mask[0,:,:]=np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
  94.     mask[1,:,:]=np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
  95.     mask[2,:,:]=np.array([[0,1,1],[-1,0,1],[-1,-1,0]])
  96.     mask[3,:,:]=np.array([[-1,-1,0],[-1,0,1],[0,1,1]])
  97.     obr2 = ffilter(obr,mask)
  98.     return obr2
  99.  
  100. def fSobela(obr):
  101.     mask=np.zeros((4,3,3))
  102.     mask[0,:,:]=np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
  103.     mask[1,:,:]=np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
  104.     mask[2,:,:]=np.array([[0,1,2],[-1,0,1],[-2,-1,0]])
  105.     mask[3,:,:]=np.array([[-2,-1,0],[-1,0,1],[0,1,2]])
  106.     obr2 = ffilter(obr,mask)
  107.     return obr2
  108. #dokonczyc
  109. def fKirscha(obr):
  110.     mask=np.zeros((8,3,3))
  111.     mask[0,:,:]=np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])#
  112.     mask[1,:,:]=np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]])#
  113.     mask[2,:,:]=np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])#
  114.     mask[3,:,:]=np.array([[5,5,-3],[5,0,-3],[-3,-3,-3]])#
  115.     mask[4,:,:]=np.array([[5,-3,-3],[5,0,-3],[5,-3,-3]])#
  116.     mask[5,:,:]=np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])#
  117.     mask[6,:,:]=np.array([[-3,-3,-3],[-3,0,-3],[5,5,5]])#
  118.     mask[7,:,:]=np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])
  119.     obr2 = ffilter(obr,mask)
  120.     return obr2
  121. #
  122. obr = mc.imread('lab3.png')[:,:,0]
  123. plt.title('Oryginal')
  124. obr2=obr.copy()
  125. obr2 = fRobertsa(obr)
  126. wyswietl(obr, obr2, 'Roberts')
  127. obr2 = fLaplacea(obr)
  128. wyswietl(obr, obr2, 'Laplace')
  129. obr2 = fPrewitta(obr)
  130. wyswietl(obr, obr2, 'Prewitt')
  131. obr2 = fSobela(obr)
  132. wyswietl(obr, obr2, 'Soel')
  133. obr2 = fKirscha(obr)
  134. wyswietl(obr, obr2, 'Kirsch')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top