-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConvlusion-Of-Mask-And-Image(FilterationPurpose)-Without-Build-in.py
58 lines (53 loc) · 1.84 KB
/
Convlusion-Of-Mask-And-Image(FilterationPurpose)-Without-Build-in.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import cv2 as cv
import numpy as np
def mask(size):
mask = np.zeros((size, size), dtype=np.float)
for i in range(size):
for j in range(size):
mask[i, j] = int(input('enter values: '))
return mask
def padding(originalImg, padSize):
padImg = np.zeros((rows+2*padSize, columns+2*padSize), dtype=np.uint8)
# Using Slicing
padImg[padSize:rows+padSize, padSize:columns+padSize] = originalImg
return padImg
def convolution(padImg, mask):
convImg = np.zeros((rows, columns), dtype=np.uint32)
for i in range(0, rows):
for j in range(0, columns):
# using Slicing
prod = np.multiply(padImg[i:i+size, j:j+size], mask)
# Taking sum of the matrix and then taking absolute of that
sum1 = np.absolute(np.sum(prod))
convImg[i][j] = sum1
return convImg
def normalization(convImg):
minimum = np.min(convImg)
maximum = np.max(convImg)
normImg = np.zeros((rows, columns), dtype=np.uint8)
for i in range(0, rows):
for j in range(0, columns):
# Using Normalization Formula
normImg[i][j] = (255*(convImg[i][j]-minimum))//(maximum-minimum)
return normImg
# mask size input
size = int(input('enter mask size: '))
# mask function call
mask = mask(size)
# padding size
p_size = size//2
# image reading
orginalImg = cv.imread('lab5Fig1.tif', 0)
# getting size of image
# orginalImg = cv.GaussianBlur(orginalImg, (3, 3), cv.BORDER_DEFAULT)
rows = orginalImg.shape[0]
columns = orginalImg.shape[1]
# padding function call
padImg = padding(orginalImg, p_size)
# convolution function call
convImg = convolution(padImg, mask)
# normalization function call
normImg = normalization(convImg)
cv.imshow('Filtered Image', normImg)
cv.waitKey(0)
cv.imwrite('LAB05Task05.jpg', normImg)