Let say I have a 2D array
let img = [[0, 1, 2, 1, 3, 0],
[1, 1, 1, 1, 1, 1],
[1, 2, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1],
[0, 1, 4, 1, 5, 0],
]
let shape = [[0,1,0],
[1,1,1],
[0,1,0]]
let diamon_shape = [[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0]]
I place center of the shape(diamond) onto each column then each row to get max number inside the shape (=1) then replace center of shape by the max number. This likes dilation and erosion in image morphology
Here is my implementation in Swift:
class func maximum_filter(image:[[Int]], shape:[[Int]]) -> [[Int]]{
let wShape = shape[0].count
let hShape = shape.count
let wImage = image[0].count
let hImage = image.count
var final = Array(repeating: Array(repeating: 0.0, count: image[0].count), count: image.count)
for i in 0..<hImage {
for ii in 0..<wImage {
var startOfWZ = 0
var startOfHZ = 0
var wStart = ii - wShape/2
if wStart < 0 {
wStart = 0
startOfWZ = 1
}
var wEnd = ii + wShape/2
if wEnd >= wImage {
wEnd = wImage - 1
}
var hStart = i - hShape/2
if hStart < 0 {
hStart = 0
startOfHZ = 1
}
var hEnd = i + hShape/2
if hEnd >= hImage {
hEnd = hImage - 1
}
var hz = startOfHZ
var maxNumber = 0.0
for x in hStart...hEnd {
var wz = startOfWZ
for xx in wStart...wEnd {
if shape[hz][wz] == 1 {
let currentNumber = image[x][xx]
if currentNumber > maxNumber {
maxNumber = currentNumber
}
}
wz += 1
}
hz += 1
}
final[i][ii] = maxNumber
}
}
return final
}
First 2 loops I iterate each element of matrix to place center of the shape onto. Then next 2 loops I get all elements of image map with elements (=1) of shape then compare them to get maximum number. Nothing complicated. The result is :
1 2 2 3 3 3
1 2 2 1 3 1
2 2 2 1 1 1
1 2 4 1 5 1
1 4 4 5 5 5
But when I try with real image 4096x4096(The input in Double not in Int in sample) and the diamond shape is 41x41. The performance is super slow (10 seconds) compared with python(1 second). Here the code i use in python result = maximum_filter(img, footprint=shape)
. I couldn't see the source code of maximum_filter
to follow, so I implement it by my self. I got same result but the performance is much slower than their's.