Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Spyder Editor
- This is a temporary script file.
- """
- import numpy as np
- import matplotlib.pyplot as plt
- import math
- obraz = plt.imread("lab33.jpg");
- obraz = obraz[:,:,2]
- m = obraz.shape[0]
- n = obraz.shape[1]
- #maska = np.array([[1,1,1], [1,4,1], [1,1,1]])
- def filtr(obr, mas):
- obraz2 = np.ones((m+2, n+2))
- #powiekszanie obrazu o dwie kolumny i dwa wiersze
- #przepisanie wartosci obrazu
- for x in range(m):
- for y in range (n):
- obraz2[x+1,y+1] = obr[x,y]
- NormaMaski = np.sum(mas)
- if NormaMaski ==0:
- NormaMaski = 1
- splot = np.zeros((m,n))
- #dla kazdego elementu obrazu
- for x in range(m):
- for y in range(n):
- #dla kazdego elementu maski
- for a in range(mas.shape[0]):
- for b in range(mas.shape[1]):
- splot[x,y] = splot[x,y] + (obraz2[x+a, y+b] * mas[a,b])
- #suma przez norme maski
- splot[x,y]= splot[x,y]/NormaMaski
- plt.subplot(121)
- plt.imshow(splot)
- plt.subplot(122)
- plt.imshow(obr)
- plt.show()
- return splot
- #funkcje krawΔdziowes1 = filtr(obraz, maska)
- #roberts
- Roberts1 = np.array([[-1,0], [1,0]])
- Roberts2 = np.array([[-1,1], [0,0]])
- Roberts3 = np.array([[0,1], [-1,0]])
- Roberts4 = np.array([[1,0], [0,-1]])
- print("Roberts")
- r1=filtr(obraz, Roberts1)
- r2=filtr(obraz, Roberts2)
- r3=filtr(obraz, Roberts3)
- r4=filtr(obraz, Roberts4)
- sumaR = (abs(r1)+abs(r2) +abs(r3) + abs(r4))/4
- plt.imshow(sumaR)
- plt.show()
- #Laplace
- Laplace = np.array([[0,1,0],[1,-4,1],[0,1,0]])
- Laplace2 = np.array([[1,1,1],[1,-8,1],[1,1,1]])
- print("Laplace 1")
- l1 = filtr(obraz, Laplace)
- plt.imshow(l1)
- plt.show()
- print("Laplace 2")
- l2 = filtr(obraz, Laplace2)
- plt.imshow(l2)
- plt.show()
- #Prewitta, Sobela, Kirscha to wszystkie maski
- Prewitt1 = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
- Prewitt2 = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
- Prewitt3 = np.array([[0,1,1],[-1,0,1],[-1,-1,0]])
- Prewitt4 = np.array([[-1,-1,0],[-1,0,1],[0,1,1]])
- print("Prewitt")
- p1 = filtr(obraz, Prewitt1)
- p2 = filtr(obraz, Prewitt2)
- p3 = filtr(obraz, Prewitt3)
- p4 = filtr(obraz, Prewitt4)
- sumaP = (abs(p1)+abs(p2)+abs(p3)+abs(p4))/4
- plt.imshow(sumaP)
- plt.show()
- #Sobela
- Sobel1 = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
- Sobel2 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
- Sobel3 = np.array([[0,1,2],[-1,0,1],[-2,-1,0]])
- Sobel4 = np.array([[-2,-1,0],[-1,0,1],[0,1,2]])
- print("Sobel")
- s1 = filtr(obraz, Sobel1)
- s2 = filtr(obraz, Sobel2)
- s3 = filtr(obraz, Sobel3)
- s4 = filtr(obraz, Sobel4)
- sumaS = (abs(s1)+abs(s2)+abs(s3)+abs(s4))/4
- plt.imshow(sumaS)
- plt.show()
- #Kirscha
- Kirsch1 = np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])
- Kirsch2 = np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]])
- Kirsch3 = np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])
- Kirsch4 = np.array([[5,5,-3],[5,0,-3],[-3,-3,-3]])
- Kirsch5 = np.array([[5,-3,-3],[5,0,-3],[5,-3,-3]])
- Kirsch6 = np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])
- Kirsch7 = np.array([[-3,-3,-3],[-3,0,-3],[5,5,5]])
- Kirsch8 = np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])
- print("Kirsch")
- k1 = filtr(obraz, Kirsch1)
- k2 = filtr(obraz, Kirsch2)
- k3 = filtr(obraz, Kirsch3)
- k4 = filtr(obraz, Kirsch4)
- k5 = filtr(obraz, Kirsch5)
- k6 = filtr(obraz, Kirsch6)
- k7 = filtr(obraz, Kirsch7)
- k8 = filtr(obraz, Kirsch8)
- sumaK = (abs(k1)+abs(k2)+abs(k3)+abs(k4)+abs(k5)+abs(k6)+abs(k7)+abs(k8))/8
- plt.imshow(sumaK)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement