Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save firatkizilirmakk/aa95c34905e877949d2753f09e3d3224 to your computer and use it in GitHub Desktop.
Save firatkizilirmakk/aa95c34905e877949d2753f09e3d3224 to your computer and use it in GitHub Desktop.
Scalar Median Filtering Strategy for Salt & Pepper Noise
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