Skip to content

Commit

Permalink
Implement Low pass (same as mean but alternative kernel pattern)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbsfchnr committed Oct 27, 2020
1 parent a85280e commit 19f20c8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
56 changes: 51 additions & 5 deletions TF00-initial-exploration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"metadata": {},
"outputs": [],
"source": [
"imgFiltered = meanie.convolve2D(img)"
"imgFiltered = meanie.convolve(img)"
]
},
{
Expand Down Expand Up @@ -116,7 +116,7 @@
"metadata": {},
"outputs": [],
"source": [
"imgFiltered = gaus.convolve2D(img)"
"imgFiltered = gaus.convolve(img)"
]
},
{
Expand Down Expand Up @@ -162,7 +162,7 @@
"metadata": {},
"outputs": [],
"source": [
"imgFiltered = med.convolve2D(img)"
"imgFiltered = med.convolve(img)"
]
},
{
Expand All @@ -187,7 +187,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create and Apply Highpass Filter"
"## Create and Apply High Pass Filter"
]
},
{
Expand All @@ -206,7 +206,7 @@
"metadata": {},
"outputs": [],
"source": [
"imgFiltered = highP.convolve2D(img)"
"imgFiltered = highP.convolve(img)"
]
},
{
Expand All @@ -229,6 +229,52 @@
"handler.saveAll(imgFiltered, highP)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create and Apply Low Pass Filter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lowP = filters.LowPass(maskSize=11)\n",
"print(lowP)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imgFiltered = lowP.convolve(img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"handler.plotFigs([img, imgFiltered])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"handler.saveAll(imgFiltered, lowP)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
25 changes: 21 additions & 4 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def assertTypes(maskSize, kernel):
def computePixel(self, sub):
pass

def convolve2D(self, img, padding=True):
def convolve(self, img, padding=True):
"""
This function which takes an image and a kernel and returns the convolution of them.
:param padding: bool defines if padding is used
Expand Down Expand Up @@ -93,6 +93,9 @@ def computePixel(self, sub):
return statistics.median(sub.flatten())

class Mean(Filter):
"""
Effectively a low pass filter. Alternative kernel implemented in class LowPass(Filter).
"""
def __init__(self, maskSize):
kernel = np.ones((maskSize,maskSize))/(maskSize**2)
super().__init__(maskSize, kernel, name='mean', linearity='linear')
Expand Down Expand Up @@ -135,12 +138,12 @@ def computePixel(self, sub):
class HighPass(Filter):
def __init__(self, maskSize):

# TODO: Make ratio of intensity reduction vs. increase configurable
# TODO: Make ratio of intensity reduction vs. increase configurable for both high and low pass
kernel = np.full((maskSize, maskSize), -1/(maskSize**2))
middle = int((maskSize-1)/2)
kernel[middle, middle] = 1 - 1/(maskSize**2)

super().__init__(maskSize, kernel, name='high-pass', linearity='linear')
#TODO: Check for high and low pass filter if they are non-linear or linear
super().__init__(maskSize, kernel, name='high-pass', linearity='non-linear')

def computePixel(self, sub):
try:
Expand All @@ -149,3 +152,17 @@ def computePixel(self, sub):
raise Exception("Sum of high pass filter weights should be effectively zero.")

return (self.kernel * sub).sum()

class LowPass(Filter):
def __init__(self, maskSize):

kernel = np.zeros((maskSize, maskSize))
middle = int((maskSize-1)/2)
kernel[middle, :] = 1/8
kernel[:, middle] = 1/8
kernel[middle, middle] = 1/2

super().__init__(maskSize, kernel, name='low-pass', linearity='non-linear')

def computePixel(self, sub):
return (self.kernel * sub).sum()/ self.kernel.sum()

0 comments on commit 19f20c8

Please sign in to comment.