-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvUntil.py
320 lines (271 loc) · 9.74 KB
/
cvUntil.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import cv2
import numpy as np
from MPath import MPath
"""
CV 的封装
"""
def listMethod(nd, r_name='', show_help=False):
print("current object type {}".format(type(nd)))
for i in dir(nd):
if r_name:
if r_name.lower() in i.lower():
print(i)
if show_help:
help(getattr(nd, i))
continue
print(i)
if show_help:
help(getattr(nd, i))
class ImageNode():
def __init__(self, file):
if type(file) == str:
## 作为路径的输入
self.file_path = MPath(file)
self.image_data = cv2.imread(file_path)
self.file_type = "FILE"
elif type(file) == np.ndarray:
self.image_data = file
self.file_type = "ARRAY"
self.file_path = None
self.shape = self.image_data.shape
self._height = self.shape[0]
self._width = self.shape[1]
self._center = ((self._width - 1) / 2, (self._height - 1) / 2)
self.source_node = self.image_data.copy()
def changing_color_space(self, convert_space_mode):
"""
转换当前 颜色得色彩数据模式
:param convert_space_mode: 参考cv 文档中 cv2.COLOR_xxxx 定义得模式
:return:
"""
return ImageNode(cv2.cvtColor(self.image_data, convert_space_mode))
def blur(self, size, blur_type=2, radius=2, border_type=3):
"""
对当前图像进行模糊操作
:param blur_type: 0 blur
1 medianBlur
2 GaussianBlur
:param size: 模糊大小以及强度
:param radius: 高斯模糊得时候需要设置 高斯核得大小
:param border_type: 边缘扩展类型 默认类型给 3
0 BORDER_REPLICATE
1 BORDER_REFLECT
2 BORDER_WRAP
3 BORDER_REFLECT_101
4 BORDER_TRANSPARENT
:return:
"""
border_list = [cv2.BORDER_REPLICATE,
cv2.BORDER_REFLECT,
cv2.BORDER_WRAP,
cv2.BORDER_REFLECT101,
cv2.BORDER_TRANSPARENT]
if blur_type == 0:
res = ImageNode(cv2.blur(self.image_data, size, borderType=border_list[border_type]))
elif blur_type == 1:
res = ImageNode(cv2.medianBlur(self.image_data, size, borderType=border_list[border_type]))
else:
res = ImageNode(cv2.GaussianBlur(self.image_data, size, radius, borderType=border_list[border_type]))
return res
def binary_image(self):
self.image_gray = cv2.cvtColor(self.image_data, cv2.COLOR_BGR2GRAY)
return ImageNode(cv2.threshold(self.image_gray,127,255,0)[1])
def threashold(self, thresh=127, maxval=255, threashold_type=0):
"""
对该图像进行阈值操作
:param thresh: 阈值分界点
:param maxval: 最大阈值
:param threashold_type: 阈值类型
cv.THRESH_BINARY
cv.THRESH_BINARY_INV
cv.THRESH_TRUNC
cv.THRESH_TOZERO
cv.THRESH_TOZERO_INV
:return: <ImageData>
"""
return ImageNode(cv2.threshold(self.image_data, thresh, maxval, threashold_type)[1])
def erode(self, radius):
"""
形态学操作 腐蚀操作
:return:
"""
kernel = np.ones((radius, radius), np.uint8)
return ImageNode(cv2.erode(self.image_data, kernel, anchor=(0, 0), iterations=3))
def dilate(self, radius):
kernel = np.ones((radius, radius), np.uint8)
return ImageNode(cv2.dilate(self.image_data, kernel, anchor=(-1, -1), iterations=3))
def morphology(self, operation, radius):
"""
形态学操作
:param operation: 操作方式 int 类型
:param radius: 卷积核大小
:return:
"""
operation_method = [
cv2.MORPH_OPEN, # 0
cv2.MORPH_CLOSE, # 1
cv2.MORPH_GRADIENT, # 2
cv2.MORPH_CROSS, # 3
cv2.MORPH_BLACKHAT, # 4
cv2.MORPH_TOPHAT, # 5
cv2.MORPH_RECT, # 6
cv2.MORPH_HITMISS # 7
]
kernel = np.ones((radius, radius), np.uint8)
return ImageNode(cv2.morphologyEx(self.image_data, operation_method[operation], kernel))
def sobel(self,ksize=3):
"""
索贝算子
:param ksize: 1 3 5 7 9
:return:
"""
x = cv2.Sobel(self.image_data,-1,1,0,ksize=ksize)
y = cv2.Sobel(self.image_data,-1,0,1,ksize=ksize)
return ImageNode(cv2.addWeighted(cv2.convertScaleAbs(x),0.5,cv2.convertScaleAbs(y),0.5,0))
def Scharr(self):
"""
scharr 算子
:param ksize: 1 3 5 7 9
:return:
"""
x = cv2.Scharr(self.image_data,-1,1,0)
y = cv2.Scharr(self.image_data,-1,0,1)
return ImageNode(cv2.addWeighted(cv2.convertScaleAbs(x),0.5,cv2.convertScaleAbs(y),0.5,0))
def laplacian(self):
"""
拉普拉斯算子
:return:
"""
return ImageNode(cv2.convertScaleAbs(cv2.Laplacian(self.image_data,-1)))
def bilateral_filter(self, distance=10, sigmaColor=50, sigmaSpace=50):
"""
双边滤波函数
:return:
"""
return ImageNode(cv2.bilateralFilter(self.image_data, distance, sigmaColor, sigmaSpace))
def canny(self,threshold1,threshold2):
"""
canny 边缘查找
:return:
"""
return cv2.Canny(self.image_data,threshold1,threshold2)
def mipmap(self):
"""
对图像进行
:return:
"""
pass
def contour_detection(self,index=-1,color=(0,255,0),thinkness=1):
"""
:return:
"""
contours,hierarchy = cv2.findContours(self.binary_image().image_data,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE
)
for cnt in contours:
if cv2.arcLength(cnt,True)< 20:
continue
# approx = cv2.approxPolyDP(cnt,epsilon,True)
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(self.image_data,(x,y),(x+w,y+h),color,1)
return self
# return ImageNode(cv2.drawContours(self.image_data,contours,index,color,thinkness))
def corner_detection(self,blockSize=3,ksize=3,k=0.001):
return ImageNode(cv2.cornerHarris(np.float32(self.binary_image().image_data),blockSize,ksize,k)).dilate(10)
@property
def height(self):
return self._height
@property
def width(self):
return self._width
@property
def center(self):
return self._center
def pixel_color(self, x, y):
'''
获取像素得颜色
:return:
'''
return self.image_data[x, y]
def set_pixel_color(self, x, y, color):
"""
设置像素颜色
:param x:
:param y:
:param color: <list> BGR 数组
:return:
"""
self.image_data[x, y] = color
def combin_image_data(self,image_nodes,direction=True):
"""
合并图片数据
:param image_dataes: <tuple> ImageData
:param direction: 默认为 横向, direction 给False 指定纵向
:return:
"""
images = tuple([data.image_data for data in image_nodes])
if direction:
res = np.hstack(images)
else:
res = np.vstack(images)
return ImageNode(res)
def show(self, time=2000, name='img'):
"""
显示当前得 image
:return:
"""
cv2.imshow(name, self.image_data)
cv2.waitKey(time)
cv2.destroyWindow(name)
def save(self, path):
"""
保存当前图像到指定地址
:param path:
:return:
"""
cv2.imwrite(path, self.image_data)
def resize(self, height, width):
"""
对图像进行缩放操作,将图像缩放到指定大小
:param height:
:param width:
"""
self.image_data = cv2.resize(self.image_data, (width, height), interpolation=cv2.INTER_CUBIC)
def translation(self, translationX, translationY):
"""
对图像进行平移操作
:param translationX:
:param translationY:
"""
self.image_data = cv2.warpAffine(self.image_data,
np.float32([[1, 0, translationX], [0, 1, translationY]]),
(self.height, self.width)
)
def rotation(self, angle, scale=1):
"""
对图像进行旋转操作
:param angle: 旋转角度
:param scale: 缩放值
:return:
"""
self.image_data = cv2.getRotationMatrix2D(self.center, angle, scale)
if __name__ == '__main__':
# file_path = r'C:\Users\Administrator\Desktop\preview2.jpg'
# file_path = r'A:\Users\Administrator\Desktop\bg2017121301.jpg'
file_path = r'A:\Users\Administrator\Desktop\65cd01d83da55cfdabca7969c4ccb79b.jpg'
# file_path = r'A:\Users\Administrator\Desktop\624629610785903385.jpg'
img = ImageNode(file_path)
# img.threashold()
img.corner_detection().show(0)
# img.show(0,'start')
# img.resize(540,960)
# edge = img.bilateral_filter(50,50,250).canny(240,255)
# img.sobel()
# nd.show(0)
# cnd = nd.canny(240,255)
# cv2.imshow('tt',edge)
# cv2.waitKey(0)
# cv2.destroyWindow('tt')