When I run the following code the output result is blurred but the image gets darker as I increase the value of sigma.
Imports
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
import math
import copy
Gaussian filter code
e = 2.71828182845904
pi = 3.1415926535897
def gaus(mat_l,sigma):
Matrix = [[0.0 for x in range(mat_l)] for y in range(mat_l)]
for x in range(-(int(mat_l/2)),(int(mat_l/2))+1):
for y in range(-(int(mat_l/2)),(int(mat_l/2))+1):
ee = pow(e,(-(((pow(x,2)+pow(y,2))/(2*(pow(sigma,2)))))))
aa = (1/(2*pi*pow(sigma,2)))
result = ee*aa
Matrix[x+int(mat_l/2)][y+int(mat_l/2)] = round(result,6)
return Matrix
Convolution code
def matrix_convolve(image,kernel,a,b,mul=1):
print(kernel)
img_x, img_y = image.shape
kernl_x, kernl_y = kernel.shape
result = copy.deepcopy(image)
for i in range(img_x-int(len(kernel)/2)):
for j in range(img_y-int(len(kernel)/2)):
result[i][j] = 0
summ = 0
for s in range(-a,a+1):
for t in range(-b,b+1):
summ += kernel[s,t] * image[i+s,j+t]
result[i][j] = math.ceil(mul*summ)
return result
Driver
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernal = gaus(5,5)
kernal = np.array([kernal[0],kernal[1],kernal[2],kernal[3],kernal[4]])
result3 = matrix_convolve(image,kernal,2,2)
plt.imshow(result3.astype(np.uint8),cmap='gray')
plt.show()
When sigma is 5, the output image is
when sigma is 2, the output image is
result3=cv2.GaussianBlur(image,(5,5),5)
plt.imshow(result3.astype(np.uint8),cmap='gray')
plt.show()
When blurred using OpenCV the following is the output for sigma 5
aa
so that the center of the kernel will be unity. If for some reason it is not unity, then normalize by dividing by the center value. – fmw42@Cris Luengo
Yes, you are correct. My error. – fmw42