1
+ #!/usr/bin/env python
2
+ __author__ = "Sreenivas Bhattiprolu"
3
+ __license__ = "Feel free to copy, I appreciate if you acknowledge Python for Microscopists"
4
+
5
+ # https://youtu.be/jWShMEhMZI4
6
+
7
+ """
8
+ Histogram equalization:
9
+ Stretches histogram to include all ranges if the original histogram is confined
10
+ only to a small region - low contrast images.
11
+ But, this type of stretching may not result in ideal results and gives
12
+ too bright and too dark regions in the image. This can be very bad for images
13
+ with large intensity variations.
14
+
15
+ CLAHE: COntrast limited adaptive histogram equalization
16
+ Regular histogram equalization uses global contrast of the image. This results in
17
+ too bright and too dark regions as the histogram stretches and is not confined
18
+ to specific region.
19
+
20
+ Adaptive histogram equalization divides the image into small tiles and within
21
+ each tile the histogram is equalized. Tile size is typically 8x8.
22
+ If theimage contains noise, it gets amplified during this process. Therefore,
23
+ contrast limiting is applied to limit the contrast below a specific limit.
24
+ Bilinear interpolation is performed between tile borders.
25
+
26
+ Below, let us perform both histogram equalization and CLAHE and compare the results.
27
+ The best way to work with color images is by converting them to luminance space,
28
+ e.g. LAB, and enhancing lumincnace channel only and eventually combining all channels.
29
+
30
+ """
31
+
32
+ import cv2
33
+ from skimage import io
34
+ from matplotlib import pyplot as plt
35
+
36
+ img = cv2 .imread ("images/bio_low_contrast.JPG" , 1 )
37
+ #img = cv2.imread('images/retina.jpg', 1)
38
+
39
+ #Converting image to LAB Color so CLAHE can be applied to the luminance channel
40
+ lab_img = cv2 .cvtColor (img , cv2 .COLOR_BGR2LAB )
41
+
42
+ #Splitting the LAB image to L, A and B channels, respectively
43
+ l , a , b = cv2 .split (lab_img )
44
+
45
+ #plt.hist(l.flat, bins=100, range=(0,255))
46
+ ###########Histogram Equlization#############
47
+ #Apply histogram equalization to the L channel
48
+ equ = cv2 .equalizeHist (l )
49
+
50
+ #plt.hist(equ.flat, bins=100, range=(0,255))
51
+ #Combine the Hist. equalized L-channel back with A and B channels
52
+ updated_lab_img1 = cv2 .merge ((equ ,a ,b ))
53
+
54
+ #Convert LAB image back to color (RGB)
55
+ hist_eq_img = cv2 .cvtColor (updated_lab_img1 , cv2 .COLOR_LAB2BGR )
56
+
57
+ ###########CLAHE#########################
58
+ #Apply CLAHE to L channel
59
+ clahe = cv2 .createCLAHE (clipLimit = 3.0 , tileGridSize = (8 ,8 ))
60
+ clahe_img = clahe .apply (l )
61
+ #plt.hist(clahe_img.flat, bins=100, range=(0,255))
62
+
63
+ #Combine the CLAHE enhanced L-channel back with A and B channels
64
+ updated_lab_img2 = cv2 .merge ((clahe_img ,a ,b ))
65
+
66
+ #Convert LAB image back to color (RGB)
67
+ CLAHE_img = cv2 .cvtColor (updated_lab_img2 , cv2 .COLOR_LAB2BGR )
68
+
69
+
70
+ cv2 .imshow ("Original image" , img )
71
+ cv2 .imshow ("Equalized image" , hist_eq_img )
72
+ cv2 .imshow ('CLAHE Image' , CLAHE_img )
73
+ cv2 .waitKey (0 )
74
+ cv2 .destroyAllWindows ()
0 commit comments