It is a learning memo.
I wrote a Python script to adjust the contrast of an image.
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('image.jpg')
#contrast
contrast = 128
#Contrast adjustment factor
factor = (259 *(contrast + 255)) / (255 *(259 - contrast))
#Convert to float type
newImage = np.array(img, dtype = 'float64')
#Contrast adjustment. (0 or less or 255 or more) is clipping
newImage = np.clip((newImage[:,:,:] - 128) * factor + 128, 0, 255)
#Return to int type
newImage = np.array(newImage, dtype = 'uint8')
#output
cv2.imwrite('out.png', newImage)
I will try it with Lena's image.
Original image
After adjusting with contrast +128
IMAGE PROCESSING ALGORITHMS PART 5: CONTRAST ADJUSTMENT Algorithms for Adjusting Brightness and Contrast of an Image
Recommended Posts