I am trying write a contrast adjustment for images in gray scale colors but couldn't find the right way to do it so far. This is what I came up with:
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy import misc
def fix_contrast(image):
minimumColor = np.amin(image)
maximumColor = np.amax(image)
#avg = (minimumColor - maximumColor)/2 first attempt
avg = np.mean(image) #second attempt
colorDownMatrix = image < avg # also tried
colorUpMatrix = image > avg
#also tried: colorUpMatrix = image > avg * 1.2
# and : colorDownMatrix = image < avg* 0.3
image = image - minimumColor*colorDownMatrix
image = image + maximumColor*colorUpMatrix
lessThen0 = image<0
moreThen255 = image>255
image[lessThen0] = 0
image[moreThen255] = 255
return image
My general attempt was to decrease elements towards 0 the pixels that their are "closer" to 0 and to increase elements towards 255 the elements that are "closer" to 255. I tried to measure closeness by mean function but before that by the arithmetic average, but all my attempts didn't get me to any good result.
Am I anywhere close to the solution? Any hints/ tips will be great