Calculate the contrast of a grayscale image. Given a 2D array of pixel values, compute the contrast as the difference between the maximum and minimum pixel intensities (range), or using RMS contrast.
import numpy as np
def image_contrast(image):
image = np.array(image, dtype=float)
# Michelson contrast or simple range contrast
min_val = np.min(image)
max_val = np.max(image)
# RMS contrast
mean_val = np.mean(image)
rms_contrast = np.sqrt(np.mean((image - mean_val) ** 2))
return round(float(rms_contrast), 4)