Created
May 25, 2020 21:38
-
-
Save firatkizilirmakk/aa95c34905e877949d2753f09e3d3224 to your computer and use it in GitHub Desktop.
Scalar Median Filtering Strategy for Salt & Pepper Noise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def medianFilterLexicalOrdering(img, filterSize = 3): | |
imgHeight, imWidth, channels = img.shape | |
outputImg = np.zeros((imgHeight, imWidth, channels), dtype=np.uint8) | |
filterHeight = filterWidth = filterSize | |
filterEdge = filterWidth // 2 | |
for i in range(filterEdge, imgHeight - filterEdge): | |
for j in range(filterEdge, imWidth - filterEdge): | |
# retrieve a frame around the pixel i,j to check | |
imgFilter = img[i - filterEdge : i + filterEdge + 1, j - filterEdge : j + filterEdge + 1].reshape(filterHeight * filterWidth, channels) | |
# rotate the frame, lexically sort and get the indices of the order | |
lexSortedIndexes = np.lexsort(np.rot90(imgFilter)) | |
# get the median index of the lexically sorted values, put its intensity | |
outputImg[i][j] = imgFilter[lexSortedIndexes[(filterWidth * filterHeight // 2)]] | |
return outputImg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment