-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49 Imgage Histogram - Find, Plot and Analyze.py
138 lines (102 loc) · 3.17 KB
/
49 Imgage Histogram - Find, Plot and Analyze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
@author: Osama Shakeel
---Imgage Histogram - Find, Plot and Analyze---
It which gives you an overall idea about the intensity distribution of an image.
It distribute data along x and y axis.
x - axis contain range of color vlaues.
y - axis contain numbers of pixels in an image.
With histogram to extrct information about contast, brigthness and intensity etc.
plot histomgram using matplotlib
"""
"""
#Histogram using Null image
import numpy as np
import cv2
from matplotlib import pyplot as plt
#plotting with calhist method
img = np.zeros((200,200), np.uint8)
cv2.rectangle(img, (0, 100), (200, 200), (255), -1)
cv2.rectangle(img, (0, 50), (50, 100), (127), -1)
#It accept parameters like ([img],[channel],mask,[histsize],range[0-255]).
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.plot(hist)
#display
plt.show()
cv2.imshow("res",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
"""
#Histogram using Color Image
import numpy as np
import cv2
from matplotlib import pyplot as plt
#with color image--------------------
img = cv2.imread("D:\\music\\thor.jpg")
img = cv2.resize(img,(500,650))
b, g, r = cv2.split(img)
cv2.imshow("img", img)
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
#Plotting different channel with hist
plt.hist(b.ravel(), 256, [0, 256])
plt.hist(g.ravel(), 256, [0, 256])
plt.hist(r.ravel(), 256, [0, 256])
plt.title("ColorFull Image")
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
"""
#Histogram for Gray scale image
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:\\music\\thor.jpg")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([img_gray], [0], None, [256], [0, 256])
plt.plot(hist)
plt.title("Gray Image")
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
"""
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:\\music\\thor.jpg")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Histogram equalization is good when of the image is confined to a particular region.
#It accept gray scale image
equ = cv2.equalizeHist(img_gray)
res = np.hstack((img_gray,equ)) #stacking images side-by-side
cv2.imshow("equ",res)
hist1 = cv2.calcHist([equ], [0], None, [256], [0, 256])
plt.plot(hist1)
plt.title("Equalization")
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
#CLAHE (Contrast Limited Adaptive Histogram Equalization)
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:\\music\\thor.jpg")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.resize(img_gray, (400,400))
#CLAHE (Contrast Limited Adaptive Histogram Equalization)
# create a CLAHE object (Arguments are optional).
#It is used to enchance image and also handle noise froom image region
#gray scale imge is required
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(img_gray)
cv2.imshow('clahe',cl1)
hist2 = cv2.calcHist([cl1], [0], None, [256], [0, 256])
plt.plot(hist2)
plt.title("CLAHE")
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()