Advertisement
Guest User

PO3

a guest
Apr 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4.  
  5. This is a temporary script file.
  6. """
  7.  
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. import math
  11.  
  12. obraz = plt.imread("lab33.jpg");
  13. obraz = obraz[:,:,2]
  14. m = obraz.shape[0]
  15. n = obraz.shape[1]
  16. #maska = np.array([[1,1,1], [1,4,1], [1,1,1]])
  17.  
  18.  
  19. def filtr(obr, mas):
  20.     obraz2 = np.ones((m+2, n+2))
  21.    
  22.     #powiekszanie obrazu o dwie kolumny i dwa wiersze
  23.     #przepisanie wartosci obrazu
  24.     for x in range(m):
  25.        for y in range (n):
  26.            obraz2[x+1,y+1] = obr[x,y]
  27.    
  28.     NormaMaski = np.sum(mas)
  29.     if NormaMaski ==0:
  30.         NormaMaski = 1
  31.    
  32.     splot = np.zeros((m,n))
  33.     #dla kazdego elementu obrazu
  34.     for x in range(m):
  35.         for y in range(n):
  36.         #dla kazdego elementu maski
  37.             for a in range(mas.shape[0]):
  38.                 for b in range(mas.shape[1]):
  39.                     splot[x,y] = splot[x,y] + (obraz2[x+a, y+b] * mas[a,b])
  40.                     #suma przez norme maski
  41.            
  42.             splot[x,y]= splot[x,y]/NormaMaski
  43.            
  44.     plt.subplot(121)
  45.     plt.imshow(splot)
  46.     plt.subplot(122)
  47.     plt.imshow(obr)
  48.     plt.show()
  49.     return splot
  50.  
  51.  
  52.  
  53. #funkcje krawΔ™dziowes1 = filtr(obraz, maska)
  54. #roberts
  55. Roberts1 = np.array([[-1,0], [1,0]])
  56. Roberts2 = np.array([[-1,1], [0,0]])
  57. Roberts3 = np.array([[0,1], [-1,0]])
  58. Roberts4 = np.array([[1,0], [0,-1]])
  59.  
  60. print("Roberts")
  61. r1=filtr(obraz, Roberts1)
  62. r2=filtr(obraz, Roberts2)
  63. r3=filtr(obraz, Roberts3)
  64. r4=filtr(obraz, Roberts4)  
  65. sumaR = (abs(r1)+abs(r2) +abs(r3) + abs(r4))/4
  66.  
  67. plt.imshow(sumaR)
  68. plt.show()
  69.  
  70.  
  71. #Laplace
  72. Laplace = np.array([[0,1,0],[1,-4,1],[0,1,0]])
  73. Laplace2 = np.array([[1,1,1],[1,-8,1],[1,1,1]])
  74. print("Laplace 1")
  75. l1 = filtr(obraz, Laplace)
  76. plt.imshow(l1)
  77. plt.show()
  78. print("Laplace 2")
  79. l2 = filtr(obraz, Laplace2)
  80. plt.imshow(l2)
  81. plt.show()
  82.  
  83. #Prewitta, Sobela, Kirscha to wszystkie maski
  84. Prewitt1 = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
  85. Prewitt2 = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
  86. Prewitt3 = np.array([[0,1,1],[-1,0,1],[-1,-1,0]])
  87. Prewitt4 = np.array([[-1,-1,0],[-1,0,1],[0,1,1]])
  88.  
  89. print("Prewitt")
  90. p1 = filtr(obraz, Prewitt1)
  91. p2 = filtr(obraz, Prewitt2)
  92. p3 = filtr(obraz, Prewitt3)
  93. p4 = filtr(obraz, Prewitt4)
  94.  
  95. sumaP = (abs(p1)+abs(p2)+abs(p3)+abs(p4))/4
  96.  
  97. plt.imshow(sumaP)
  98. plt.show()
  99.  
  100.  
  101. #Sobela
  102. Sobel1 = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
  103. Sobel2 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
  104. Sobel3 = np.array([[0,1,2],[-1,0,1],[-2,-1,0]])
  105. Sobel4 = np.array([[-2,-1,0],[-1,0,1],[0,1,2]])
  106.  
  107. print("Sobel")
  108. s1 = filtr(obraz, Sobel1)
  109. s2 = filtr(obraz, Sobel2)
  110. s3 = filtr(obraz, Sobel3)
  111. s4 = filtr(obraz, Sobel4)
  112.  
  113. sumaS = (abs(s1)+abs(s2)+abs(s3)+abs(s4))/4
  114.  
  115. plt.imshow(sumaS)
  116. plt.show()
  117.  
  118. #Kirscha
  119. Kirsch1 = np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])
  120. Kirsch2 = np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]])
  121. Kirsch3 = np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])
  122. Kirsch4 = np.array([[5,5,-3],[5,0,-3],[-3,-3,-3]])
  123. Kirsch5 = np.array([[5,-3,-3],[5,0,-3],[5,-3,-3]])
  124. Kirsch6 = np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])
  125. Kirsch7 = np.array([[-3,-3,-3],[-3,0,-3],[5,5,5]])
  126. Kirsch8 = np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])
  127.  
  128. print("Kirsch")
  129. k1 = filtr(obraz, Kirsch1)
  130. k2 = filtr(obraz, Kirsch2)
  131. k3 = filtr(obraz, Kirsch3)
  132. k4 = filtr(obraz, Kirsch4)
  133. k5 = filtr(obraz, Kirsch5)
  134. k6 = filtr(obraz, Kirsch6)
  135. k7 = filtr(obraz, Kirsch7)
  136. k8 = filtr(obraz, Kirsch8)
  137.  
  138. sumaK = (abs(k1)+abs(k2)+abs(k3)+abs(k4)+abs(k5)+abs(k6)+abs(k7)+abs(k8))/8
  139.  
  140. plt.imshow(sumaK)
  141. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement