Skip to content

Commit fbcedaa

Browse files
authored
Add files via upload
1 parent 6664604 commit fbcedaa

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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/XbCK46n7U80
6+
7+
"""
8+
This code demonstrates potential applications in image processing by manipulating DCT.
9+
In this example, we perform averaging of images in real and DCT space.
10+
Since DCT is linear, an average of images in spatial domain would be identical to performing
11+
DCT on each image and averaging the DCT and then inverse DCT to bring back the original image.
12+
13+
DCT is similar to DFT. DFT uses a set of complex exponential functions,
14+
while the DCT uses only (real-valued) cosine functions.
15+
16+
DCT is used in data compression applications such as JPEG.This is becasue DCT representation
17+
has more of the information concentrated in small number of coefficients. This is useful for
18+
compression algorithms because you can approximate original information in a relatively small set
19+
of DCT coefficients.
20+
"""
21+
22+
import matplotlib.pyplot as plt
23+
import cv2
24+
import numpy as np
25+
from scipy.fftpack import dct, idct
26+
27+
from os import listdir
28+
29+
image_dir = "C:/PfM/Python_files/New_stuff/noisy_images/25_sigma/" # Path to image directory
30+
31+
# Load images
32+
filenames = listdir(image_dir)
33+
filenames.sort()
34+
35+
imgs = []
36+
for f in filenames:
37+
imgs.append((cv2.imread(image_dir + f, 0)).astype(np.float32))
38+
39+
40+
height, width = imgs[0].shape
41+
42+
# Apply the weighted average to images and corresponding DCT images, respectively.
43+
avg_img = np.zeros([height, width], np.float32)
44+
dct_avg_img = np.zeros([height, width], np.float32)
45+
46+
for i in range(len(imgs)):
47+
avg_img = cv2.addWeighted(avg_img, i/(i+1.0), imgs[i], 1/(i+1.0), 0) #Original image average
48+
dct_avg_img = cv2.addWeighted(dct_avg_img, i/(i+1.0), dct(imgs[i]), 1/(i+1.0), 0) #DCT average
49+
50+
51+
reverse_img = idct(dct_avg_img) #Convert averaged DCT back to real space.
52+
53+
54+
plt.imsave("noisy_images/00-dct_averaged_img.jpg", reverse_img, cmap="gray")
55+
plt.imsave("noisy_images/00-averaged_img.jpg", avg_img, cmap="gray")
56+
57+
58+
fig = plt.figure(figsize=(10, 10))
59+
ax1 = fig.add_subplot(2,2,1)
60+
ax1.imshow(imgs[0], cmap='gray')
61+
ax1.title.set_text('Input Image 1')
62+
ax2 = fig.add_subplot(2,2,2)
63+
ax2.imshow(imgs[1], cmap='gray')
64+
ax2.title.set_text('Input Image 2')
65+
66+
ax3 = fig.add_subplot(2,2,3)
67+
ax3.imshow(avg_img, cmap='gray')
68+
ax3.title.set_text('Average of Images')
69+
ax4 = fig.add_subplot(2,2,4)
70+
ax4.imshow(reverse_img, cmap='gray')
71+
ax4.title.set_text('Image from DCT average')
72+
plt.show()

113-what_is_histogram_equalization.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)