Created
May 10, 2022 19:48
-
-
Save ali-john/977886fdeb52b3c1a6b3a149e5ca1ad2 to your computer and use it in GitHub Desktop.
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 maxFilter(img, size): | |
out = np.zeros([img.shape[0], img.shape[1]], dtype="int") | |
for i in range(img.shape[0] - size): | |
for j in range(img.shape[1] - size): | |
mat = img[i: i + size, j: j + size] | |
mat = np.ravel(mat) # convert to 1D array | |
mat = np.sort(mat) | |
out[i, j] = np.max(mat) | |
return out | |
def ContrastStretching(image, lp, hp): | |
lower_percentile = np.percentile(image, lp) # used as min value | |
higher_percentile = np.percentile(image, hp) # used for max value | |
output = np.zeros([image.shape[0], image.shape[1]], dtype=np.uint8) | |
# pixel value less than lp percentile -- set to 0 | |
# pixel value > hp percentile -- set to 255 | |
# lp percentile < pixel value < hp percentile -- stretch according to formula | |
for i in range(image.shape[0]): | |
for j in range(image.shape[1]): | |
val = image[i, j] | |
if val < lower_percentile: | |
output[i, j] = 0 | |
elif val > higher_percentile: | |
output[i, j] = 255 | |
else: | |
output[i, j] = ((image[i, j] - lower_percentile) / (higher_percentile - lower_percentile)) * 255 | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment