-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (134 loc) · 3.81 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
import cv2
import matplotlib.pyplot as plt
import numpy as np
import time
import math
if __name__=='__main__':
start=time.perf_counter()
file_name='D:\FFOutput\\1600357086421.jpg'
img_cv= cv2.imread(file_name)
#把图像数组的正确读取(cv2本来是BGR)
img_rgb=cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
# plt.imshow(img_cv)
# plt.show()
plt.imshow(img_rgb)
plt.show()
#输出参数,看看
print('The type of the image:', type(img_cv))
print('shape{}'.format(img_cv.shape))
print('height: {}'.format(img_cv.shape[0]))
print('the de of img_rgb:{}'.format(img_cv.shape[2]))
print('max of img_rgb:{}'.format(img_cv.max()))
#maxar=max(img_cv.all())
#print('max of img_R:{}'.format(maxar))
#要求每个分量里的最大值呢?
#splite the RGB channel
numtx1=np.zeros_like(img_cv)
numtx2=np.zeros_like(img_cv)
numtx3 = np.zeros_like(img_cv)
numtx1[:,:,0]=img_rgb[:,:,0]
numtx2[:,:,1]=img_rgb[:,:,1]
numtx3[:,:,2] = img_rgb[:,:,2]
img_gray=cv2.cvtColor(img_cv,cv2.COLOR_BGR2GRAY)
#分离后的图显示
# plt.imshow(img_gray)
# plt.show()
# plt.imshow(numtx1)
# plt.show()
# plt.imshow( numtx2)
# plt.show()
# plt.imshow( numtx3)
# plt.show()
#图片上加入文字
# cv2.putText(img_cv, "yaodaoji", (200, 100), \
# cv2.FONT_HERSHEY_COMPLEX, 2.0, (100, 200, 200), 5)
# plt.imshow(img_cv)
# plt.show()
#绘制灰度直方图。简单函数试试
# x=np.linspace(-3,3,40)
# y=2*x
# y2=1/x
#
# plt.figure()
# plt.plot(x,y)
# plt.show()
#
# plt.figure()
# plt.bar(x, y2)
# plt.show()
color = ("blue", "green", "red")
for i, color in enumerate(color):
#老师教的办法。用起来
hist = cv2.calcHist([img_rgb], [i], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.show()
cv2.waitKey()
# #分别自己bar实现三通道直方图
# plt.figure()
# #把矩阵变成一维数组
# mtx1r=numtx1.ravel()
# #去掉零
# b = np.argwhere(mtx1r==0)
# mtx1r = np.delete(mtx1r, b)
# hist =np.zeros(256)
# #实现x,y(数组)
# xaxis=np.linspace(0,255,256)
# for i in range(255):
# mt1_tf=(mtx1r==i)
# hist[i]=np.sum(mt1_tf)
# plt.title("red bar")
# plt.bar(xaxis, hist)
# plt.show()
#
# #第二个通道就照写,改一下数字
# plt.figure()
# mtx2r=numtx2.ravel()
# b = np.argwhere(mtx2r==0)
# mtx2r = np.delete(mtx2r, b)
# hist =np.zeros(256)
# xaxis=np.linspace(0,255,256)
# for i in range(255):
# mt1_tf=(mtx2r==i)
# hist[i]=np.sum(mt1_tf)
# plt.title("green bar")
# plt.bar(xaxis, hist)
# plt.show()
#
# plt.figure()
# mtx3r=numtx3.ravel()
# b = np.argwhere(mtx3r==0)
# mtx3r = np.delete(mtx3r, b)
# hist =np.zeros(256)
# xaxis=np.linspace(0,255,256)
# for i in range(255):
# mt1_tf=(mtx3r==i)
# hist[i]=np.sum(mt1_tf)
# plt.title("blue bar")
# plt.bar(xaxis, hist)
# plt.show()
#下面 本质还是 利用hist函数实现的。不算自己写的
#比较运算速度那个我也还没做。
plt.figure()
mtx1r=numtx1.ravel()
b = np.argwhere(mtx1r==0)
mtx1r = np.delete(mtx1r, b)
plt.hist(mtx1r, 256, [0, 256])
plt.show()
cv2.waitKey()
plt.figure()
mtx2r=numtx2.ravel()
b = np.argwhere(mtx2r==0)
mtx2r = np.delete(mtx2r, b)
plt.hist(mtx2r, 256, [0, 256])
plt.show()
cv2.waitKey()
plt.figure()
mtx3r = numtx3.ravel()
b = np.argwhere(mtx3r == 0)
mtx3r = np.delete(mtx3r, b)
plt.hist(mtx3r, 256, [0, 256])
plt.show()
cv2.waitKey()
end=time.perf_counter()
print("{}".format(end-start))