Contrast stretching in Python/ OpenCV

OpenCV doesn't have any function for contrast stretching and google yields the same result because histogram equalization does stretch the histogram horizontally but its just the difference of the transformation function. (Both methods increase the contrast of the images.Transformation function transfers the pixel intensity levels from the given range to required range.)

Histogram equalization derives the transformation function(TF) automatically from probability density function (PDF) of the given image where as in contrast stretching you specify your own TF based on the applications' requirement.

One simple TF through which you can do contrast stretching is min-max contrast stretching -

((pixel – min) / (max – min))*255.

You do this for each pixel value. min and max being the minimum and maximum intensities.


You can also use cv2.LUT for contrast stretching by creating a custom table using np.interp. Links to their documentation are this and this respectively. Below an example is shown.

import cv2
import numpy as np

img = cv2.imread('messi.jpg')
original = img.copy()
xp = [0, 64, 128, 192, 255]
fp = [0, 16, 128, 240, 255]
x = np.arange(256)
table = np.interp(x, xp, fp).astype('uint8')
img = cv2.LUT(img, table)
cv2.imshow("original", original)
cv2.imshow("Output", img)
cv2.waitKey(0)
cv2.destroyAllWindows() 

The table created

[  0   0   0   0   1   1   1   1   2   2   2   2   3   3   3   3   4   4
   4   4   5   5   5   5   6   6   6   6   7   7   7   7   8   8   8   8
   9   9   9   9  10  10  10  10  11  11  11  11  12  12  12  12  13  13
  13  13  14  14  14  14  15  15  15  15  16  17  19  21  23  24  26  28
  30  31  33  35  37  38  40  42  44  45  47  49  51  52  54  56  58  59
  61  63  65  66  68  70  72  73  75  77  79  80  82  84  86  87  89  91
  93  94  96  98 100 101 103 105 107 108 110 112 114 115 117 119 121 122
 124 126 128 129 131 133 135 136 138 140 142 143 145 147 149 150 152 154
 156 157 159 161 163 164 166 168 170 171 173 175 177 178 180 182 184 185
 187 189 191 192 194 196 198 199 201 203 205 206 208 210 212 213 215 217
 219 220 222 224 226 227 229 231 233 234 236 238 240 240 240 240 240 241
 241 241 241 242 242 242 242 243 243 243 243 244 244 244 244 245 245 245
 245 245 246 246 246 246 247 247 247 247 248 248 248 248 249 249 249 249
 250 250 250 250 250 251 251 251 251 252 252 252 252 253 253 253 253 254
 254 254 254 255]

Now cv2.LUT will replace the values of the original image with the values in the table. For example, all the pixels having values 1 will be replaced by 0 and all pixels having values 4 will be replaced by 1.

Original Image

Original

Contrast Stretched Image

enter image description here

The values of xp and fp can be varied to create custom tables as required and it will stretch the contrast even if min and max pixels are 0 and 255 unlike the answer provided by hashcode55.


Python/OpenCV can do contrast stretching via the cv2.normalize() method using min_max normalization. For example:

Input:

enter image description here

#!/bin/python3.7

import cv2
import numpy as np

# read image
img = cv2.imread("zelda3_bm20_cm20.jpg", cv2.IMREAD_COLOR)

# normalize float versions
norm_img1 = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
norm_img2 = cv2.normalize(img, None, alpha=0, beta=1.2, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

# scale to uint8
norm_img1 = (255*norm_img1).astype(np.uint8)
norm_img2 = np.clip(norm_img2, 0, 1)
norm_img2 = (255*norm_img2).astype(np.uint8)

# write normalized output images
cv2.imwrite("zelda1_bm20_cm20_normalize1.jpg",norm_img1)
cv2.imwrite("zelda1_bm20_cm20_normalize2.jpg",norm_img2)

# display input and both output images
cv2.imshow('original',img)
cv2.imshow('normalized1',norm_img1)
cv2.imshow('normalized2',norm_img2)
cv2.waitKey(0)
cv2.destroyAllWindows()


Normalize1:

enter image description here

Normalize2:

enter image description here

You can also do your own stretching by using a simple linear equation with 2 pair of input/ouput values using the form y=A*x+B and solving the two simultaneous equations. See concept for stretching shown in How can I make the gradient appearance of one image equal to the other?