Compare commits

..

No commits in common. "main" and "changes" have entirely different histories.

5 changed files with 17 additions and 105 deletions

View File

@ -1 +0,0 @@
3.11

View File

@ -1,7 +0,0 @@
[project]
name = "digba"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = []

View File

@ -45,21 +45,12 @@ def applyMedianFilter(img: ndarray, kSize: int):
# create a moving average kernel of arbitrary size
def createMovingAverageKernel(kSize):
kernel = np.zeros((kSize, kSize))
f = 1.0/(kSize * kSize)
for x in range(kSize):
for y in range(kSize):
kernel[x][y] = f
return kernel
# Won't use below cuz it would e muuuuch slooooower.
def gaussian(x, y, sigmaX, sigmaY, meanX, meanY):
px = (x-meanX)*(x-meanX)/(sigmaX*sigmaX)
py = (y-meanY)*(y-meanY)/(sigmaY*sigmaY)
return math.exp(-0.5 * ( px + py ))/(2*math.pi*sigmaX*sigmaY)
result = 1
return result
# create a gaussian kernel of arbitrary size
@ -73,12 +64,14 @@ def createGaussianKernel(kSize, sigma=None):
sum = 0.0
for x in range(kSize):
xm = x - math.floor(kSize/2)
xm = x - kSize/2
xsum = xm * xm / stdev2
for y in range(kSize):
ym = y - math.floor(kSize/2)
ym = y - kSize/2
ysum = ym * ym / stdev2
kernel[x][y] = math.exp((xm*xm + ym*ym) / stdev2 * -0.5) * factor
kernel[x][y] = math.exp((xsum + ysum) * -0.5) * factor
sum += kernel[x][y]
# Normalize gaussian kernel in order not minimize power loss:
@ -92,19 +85,14 @@ def createGaussianKernel(kSize, sigma=None):
# create a sobel kernel in x direction of size 3x3
def createSobelXKernel():
return np.array([
[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
kernel = np.zeros((3, 3))
return kernel
# create a sobel kernel in y direction of size 3x3
def createSobelYKernel():
return np.array([
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]])
kernel = np.zeros((3, 3))
return kernel
def applyKernelInSpatialDomain(img, kernel):
@ -123,82 +111,34 @@ def applyKernelInSpatialDomain(img, kernel):
for v in range(0, height):
t = y + v - int(height/2)
s = max(min(s, img.shape[0] - 1), 0)
t = max(min(t, img.shape[1] - 1), 0)
color = img[s][t]
color = np.zeros([3])
if t >= 0 and t < img.shape[1] and s >= 0 and s < img.shape[0]:
color = img[s][t]
filtered_img[x][y][0] += kernel[u][v] * color[0]
filtered_img[x][y][1] += kernel[u][v] * color[1]
filtered_img[x][y][2] += kernel[u][v] * color[2]
# Only for sobel, implemented in controller.py as workaround.
# filtered_img[x][y][0] = filtered_img[x][y][0] + 127
# filtered_img[x][y][1] = filtered_img[x][y][1] + 127
# filtered_img[x][y][2] = filtered_img[x][y][2] + 127
return filtered_img
# Extra: create an integral image of the given image
def createIntegralImage(img):
integral_image = img.copy()
for x in range(0, img.shape[0]):
for y in range(0, img.shape[1]):
for c in range(3):
integral_image[x][y][c] = img[x][y][c] - img[x-1][y-1][c] + img[x+1][y][c] + img[x][y+1][c]
return integral_image
# Extra: apply the moving average filter by using an integral image
def applyMovingAverageFilterWithIntegralImage(img, kSize):
filtered_img = img.copy()
integral_img = createIntegralImage(img)
return filtered_img
# Extra:
def applyMovingAverageFilterWithSeperatedKernels(img, kSize):
filtered_img = img.copy()
f = 1.0/(kSize)
kernel = np.zeros((kSize, 1))
for i in range(kSize):
kernel[i] = f
for x in range(img.shape[0]):
for y in range(img.shape[1]):
for u in range(0, kSize):
s = x + u - int(kSize/2)
s = max(min(s, img.shape[0] - 1), 0)
color = img[s][y]
filtered_img[x][y][0] += kernel[u] * color[0]
filtered_img[x][y][1] += kernel[u] * color[1]
filtered_img[x][y][2] += kernel[u] * color[2]
for x in range(img.shape[0]):
for y in range(img.shape[1]):
for v in range(0, kSize):
t = y + v - int(kSize/2)
t = max(min(t, img.shape[1] - 1), 0)
color = img[t][y]
filtered_img[x][y][0] += kernel[v] * color[0]
filtered_img[x][y][1] += kernel[v] * color[1]
filtered_img[x][y][2] += kernel[v] * color[2]
return filtered_img
def run_runtime_evaluation(img):
pass

View File

@ -134,27 +134,15 @@ class MainController:
img = IF.applyMedianFilter(self._model.input_image, kernel_size)
self._model.image = img
def apply_filter_sobelX(self):
kernel = IF.createSobelXKernel()
img = IF.applyKernelInSpatialDomain(self._model.input_image, kernel)
self._model.image = normalizeSobel(img)
self._model.image = img
def apply_filter_sobelY(self):
kernel = IF.createSobelYKernel()
img = IF.applyKernelInSpatialDomain(self._model.input_image, kernel)
self._model.image = normalizeSobel(img)
self._model.image = img
def run_runtime_evaluation(self):
IF.run_runtime_evaluation(self._model.input_image)
def normalizeSobel(img):
filtered_img = img.copy()
for x in range(0, img.shape[0]):
for y in range(0, img.shape[1]):
filtered_img[x][y][0] = img[x][y][0] + 127
filtered_img[x][y][1] = img[x][y][1] + 127
filtered_img[x][y][2] = img[x][y][2] + 127
return filtered_img

View File

@ -1,8 +0,0 @@
version = 1
revision = 1
requires-python = ">=3.11"
[[package]]
name = "digba"
version = "0.1.0"
source = { virtual = "." }