I want to reduce the contrast of my whole dataset as an experiment. The dataset is in a NumPy array of np.array(X).reshape(-1, 28, 28, 1)
I tried to use the library Albumentations, which works really well for motion blur and Gaussian noise, but I didn't find a way to reduce the contrast with that library. How can I do this?
Asked
Active
Viewed 91 times
-2

Cris Luengo
- 55,762
- 10
- 62
- 120

MRSinal
- 25
- 1
- 5
1 Answers
0
You would need to rescale the difference to the mean value, here is an example, using this image as source. Extra code using PIL and imshow
for visuals:
import numpy as np
import PIL.Image
import matplotlib.pyplot as plt
im = PIL.Image.open('a-600-600-image-of-a-building.jpg')
np_im = np.array(im)
np_mean = np.mean(np.array(im))
for factor in [1.0, 0.9, 0.5, 0.1]:
plt.figure()
plt.title("{}".format(factor))
reduced_contrast=(np_im-np_mean)*factor + np_mean
new_im = PIL.Image.fromarray(reduced_contrast)
plt.imshow(np.array(list(new_im.convert('RGBA').getdata())).reshape(new_im.height, new_im.width, 4))
plt.savefig("{}.png".format(factor))
Output:
The relevant line is reduced_contrast=(np_im-np_mean)*factor + np_mean

FlyingTeller
- 17,638
- 3
- 38
- 53
-
Thank you very much for the clear explanation! – MRSinal Aug 17 '22 at 08:21
-
I tried this method in my code, but for some reason, it didn't change anything about the picture. I feel like there is a problem with the np.mean as I am trying to make all the dataset have a reduce contrast – MRSinal Aug 17 '22 at 15:06
-
The problem is not np.mean itself but the problem is that I am not converting my image from an array put I am printing the array itself. How could I do this without me converting it into a PIL.Image? – MRSinal Aug 17 '22 at 15:20
-
Include a MWE in your original question of your code, it's difficult to debug from descriptions – FlyingTeller Aug 17 '22 at 21:27