assembly-opencv 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,323 @@
1
+ import cv2
2
+ import numpy as np
3
+ from shapely import Polygon
4
+
5
+ from assembly.description.Decoration import func_description, class_description
6
+ from assembly.GeneralUtil import show_image
7
+
8
+ '''
9
+ 轮廓运算
10
+
11
+ 输入:轮廓矩阵
12
+ 输出:轮廓矩阵
13
+ '''
14
+
15
+
16
+ @class_description(
17
+ title="轮廓运算",
18
+ description="轮廓运算主要是指对轮廓进行各种数学运算以实现轮廓的增强、变换和分析等功能。",
19
+ level=10
20
+ )
21
+ class PolygonOperation(object):
22
+
23
+ @func_description(
24
+ title="轮廓平移",
25
+ description="对轮廓进行平移操作"
26
+ )
27
+ @staticmethod
28
+ def polygon_translate(contour, tx, ty):
29
+ """
30
+ :name 轮廓平移
31
+ :param contour: 轮廓矩阵
32
+ :param tx: x方向平移距离
33
+ :param ty: y方向平移距离
34
+ :return contour: 平移后的轮廓矩阵
35
+ """
36
+ M = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32)
37
+ contour_translated = cv2.transform(contour, M)
38
+ return contour_translated
39
+
40
+ @func_description(
41
+ title="轮廓旋转",
42
+ description="对轮廓进行旋转操作"
43
+ )
44
+ @staticmethod
45
+ def polygon_rotate(contour, angle, center=None):
46
+ """
47
+ :name 轮廓旋转
48
+ :param contour: 轮廓矩阵
49
+ :param angle: 旋转角度,正值表示逆时针旋转,负值表示顺时针旋转
50
+ :param center: 旋转中心点,默认None表示以轮廓的质心为中心
51
+ :return contour: 旋转后的轮廓矩阵
52
+ """
53
+ if center is None:
54
+ M = cv2.moments(contour)
55
+ if M["m00"] != 0:
56
+ center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
57
+ else:
58
+ center = (0, 0)
59
+
60
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
61
+ contour_rotated = cv2.transform(contour, M)
62
+ return contour_rotated
63
+
64
+ @func_description(
65
+ title="轮廓缩放",
66
+ description="对轮廓进行缩放操作"
67
+ )
68
+ @staticmethod
69
+ def polygon_scale(contour, scale_x, scale_y, center=None):
70
+ """
71
+ :name 轮廓缩放
72
+ :param contour: 轮廓矩阵
73
+ :param scale_x: x方向缩放因子
74
+ :param scale_y: y方向缩放因子
75
+ :param center: 缩放中心点,默认None表示以轮廓的质心为中心
76
+ :return contour: 缩放后的轮廓矩阵
77
+ """
78
+ if center is None:
79
+ M = cv2.moments(contour)
80
+ if M["m00"] != 0:
81
+ center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
82
+ else:
83
+ center = (0, 0)
84
+
85
+ M = np.array([[scale_x, 0, (1 - scale_x) * center[0]],
86
+ [0, scale_y, (1 - scale_y) * center[1]]], dtype=np.float32)
87
+ contour_scaled = cv2.transform(contour, M)
88
+ return contour_scaled
89
+
90
+ @func_description(
91
+ title="轮廓取交集",
92
+ description="计算两个轮廓的交集区域"
93
+ )
94
+ @staticmethod
95
+ def contours_intersect(contour1, contour2, buffer=0):
96
+ """
97
+ :name 轮廓取交集
98
+ :param contour1: 轮廓矩阵1
99
+ :param contour2: 轮廓矩阵2
100
+ :param buffer: 扩展轮廓 >0外扩 <0内缩
101
+ :return intersect: 布尔值,表示是否相交
102
+ """
103
+ poly1 = Polygon(contour1)
104
+ if buffer != 0:
105
+ poly1 = poly1.buffer(buffer)
106
+
107
+ poly2 = Polygon(contour2)
108
+ if buffer != 0:
109
+ poly2 = poly2.buffer(buffer)
110
+
111
+ if not poly1.is_valid:
112
+ poly1 = poly1.buffer(0)
113
+ if not poly2.is_valid:
114
+ poly2 = poly2.buffer(0)
115
+
116
+ if poly1.intersects(poly2):
117
+ intersect_poly = poly1.intersection(poly2)
118
+ intersect = self.shapely_poly_to_cv(intersect_poly)
119
+ else:
120
+ intersect = []
121
+
122
+ return intersect
123
+
124
+ @func_description(
125
+ title="轮廓取差集",
126
+ description="计算两个轮廓的差集区域"
127
+ )
128
+ @staticmethod
129
+ def contours_difference(contour1, contour2, buffer=0):
130
+ """
131
+ :name 轮廓取差集
132
+ :param contour1: 轮廓矩阵1
133
+ :param contour2: 轮廓矩阵2
134
+ :param buffer: 扩展轮廓 >0外扩 <0内缩
135
+ :return difference_contour: 差集轮廓矩阵
136
+ """
137
+ poly1 = Polygon(contour1)
138
+ if buffer != 0:
139
+ poly1 = poly1.buffer(buffer)
140
+
141
+ poly2 = Polygon(contour2)
142
+ if buffer != 0:
143
+ poly2 = poly2.buffer(buffer)
144
+
145
+ if not poly1.is_valid:
146
+ poly1 = poly1.buffer(0)
147
+ if not poly2.is_valid:
148
+ poly2 = poly2.buffer(0)
149
+
150
+ if poly1.intersects(poly2):
151
+ difference_poly = poly1.difference(poly2)
152
+ difference = self.shapely_poly_to_cv(difference_poly)
153
+ else:
154
+ difference = []
155
+
156
+ return difference
157
+
158
+ @func_description(
159
+ title="轮廓取并集",
160
+ description="计算两个轮廓的并集区域"
161
+ )
162
+ @staticmethod
163
+ def contours_union(contour1, contour2, buffer=0):
164
+ """
165
+ :name 轮廓取并集
166
+ :param contour1: 轮廓矩阵1
167
+ :param contour2: 轮廓矩阵2
168
+ :param buffer: 扩展轮廓 >0外扩 <0内缩
169
+ :return union_contour: 并集轮廓矩阵
170
+ """
171
+ poly1 = Polygon(contour1)
172
+ if buffer != 0:
173
+ poly1 = poly1.buffer(buffer)
174
+
175
+ poly2 = Polygon(contour2)
176
+ if buffer != 0:
177
+ poly2 = poly2.buffer(buffer)
178
+
179
+ if not poly1.is_valid:
180
+ poly1 = poly1.buffer(0)
181
+ if not poly2.is_valid:
182
+ poly2 = poly2.buffer(0)
183
+
184
+ if poly1.intersects(poly2):
185
+ union_poly = poly1.union(poly2)
186
+ union = self.shapely_poly_to_cv(union_poly)
187
+ else:
188
+ union = []
189
+
190
+ return union
191
+
192
+ @func_description(
193
+ title="轮廓交并比",
194
+ description="计算两个轮廓的交并比"
195
+ )
196
+ @staticmethod
197
+ def contours_iou(contour1, contour2, buffer=0):
198
+ """
199
+ :name 轮廓交并比
200
+ :param contour1: 轮廓矩阵1
201
+ :param contour2: 轮廓矩阵2
202
+ :param buffer: 扩展轮廓 >0外扩 <0内缩
203
+ :return iou: 交并比
204
+ """
205
+ poly1 = Polygon(contour1)
206
+ if buffer != 0:
207
+ poly1 = poly1.buffer(buffer)
208
+
209
+ poly2 = Polygon(contour2)
210
+ if buffer != 0:
211
+ poly2 = poly2.buffer(buffer)
212
+
213
+ if not poly1.is_valid:
214
+ poly1 = poly1.buffer(0)
215
+ if not poly2.is_valid:
216
+ poly2 = poly2.buffer(0)
217
+
218
+ if poly1.intersects(poly2):
219
+ intersection = poly1.intersection(poly2)
220
+ intersection_area = intersection.area
221
+
222
+ union = poly1.union(poly2)
223
+
224
+ return intersection_area / union.area
225
+ else:
226
+ return 0
227
+
228
+ @func_description(
229
+ title="轮廓绘制",
230
+ description="在图像上绘制轮廓"
231
+ )
232
+ @staticmethod
233
+ def draw_contours(img, contours, color=(0, 255, 0), thickness=2):
234
+ """
235
+ :name 轮廓绘制
236
+ :param img: 原始图像
237
+ :param contours: 轮廓矩阵或轮廓矩阵列表
238
+ :param color: 轮廓颜色,默认为绿色
239
+ :param thickness: 轮廓线条粗细,默认为2
240
+ :return img_with_contours: 绘制轮廓后的图像
241
+ """
242
+
243
+ if img is not None:
244
+ img_with_contours = img.copy()
245
+ else:
246
+ contour_width = contours[:, :, 0].max() - contours[:, :, 0].min() + 10
247
+ contour_height = contours[:, :, 1].max() - contours[:, :, 1].min() + 10
248
+ img_with_contours = np.zeros((contour_height, contour_width, 3), dtype=np.uint8)
249
+ if isinstance(contours, list):
250
+ cv2.drawContours(img_with_contours, contours, -1, color, thickness)
251
+ else:
252
+ cv2.drawContours(img_with_contours, [contours], -1, color, thickness)
253
+ return img_with_contours
254
+
255
+ # OpenCV多边形 -> Shapely多边形
256
+ @staticmethod
257
+ def cv_poly_to_shapely(cv_poly):
258
+ # 将点从OpenCV格式(float32)转换为整数格式
259
+ points = np.round(cv_poly.reshape(-1, 1, 2)).astype(np.int)
260
+ return Polygon(points)
261
+
262
+ # Shapely多边形 -> OpenCV多边形
263
+ @staticmethod
264
+ def shapely_poly_to_cv(shapely_poly):
265
+ # 提取Shapely多边形的顶点
266
+ vertices = np.array(shapely_poly.exterior.coords)
267
+ # 转换为float32类型
268
+ return vertices.astype(int)
269
+
270
+
271
+ # 暴露方法
272
+ def polygon_translate(contour, tx, ty):
273
+ return PolygonOperation.polygon_translate(contour, tx, ty)
274
+
275
+ def polygon_rotate(contour, angle, center=None):
276
+ return PolygonOperation.polygon_rotate(contour, angle, center)
277
+
278
+ def polygon_scale(contour, scale_factor, center=None):
279
+ return PolygonOperation.polygon_scale(contour, scale_factor, center)
280
+
281
+ def contours_intersect(contour1, contour2, buffer=0):
282
+ return PolygonOperation.contours_intersect(contour1, contour2, buffer)
283
+
284
+ def contours_union(contour1, contour2, buffer=0):
285
+ return PolygonOperation.contours_union(contour1, contour2, buffer)
286
+
287
+ def contours_difference(contour1, contour2, buffer=0):
288
+ return PolygonOperation.contours_difference(contour1, contour2, buffer)
289
+
290
+ def contours_iou(contour1, contour2, buffer=0):
291
+ return PolygonOperation.contours_iou(contour1, contour2, buffer)
292
+
293
+ def draw_contours(img, contours, color=(0, 255, 0), thickness=2):
294
+ return PolygonOperation.draw_contours(img, contours, color, thickness)
295
+
296
+
297
+
298
+ if __name__ == '__main__':
299
+ poly = PolygonOperation()
300
+ polygon1 = [[100, 100], [200, 100], [200, 200], [100, 200]]
301
+ polygon2 = [[150, 150], [300, 150], [300, 300], [150, 300]]
302
+
303
+ img = np.zeros((400, 400, 3), dtype=np.uint8)
304
+ img1 = poly.draw_contours(img, np.array(polygon1))
305
+ img2 = poly.draw_contours(img, np.array(polygon2), color=(255, 0, 0))
306
+
307
+ show_image("polygon1", img1)
308
+ show_image("polygon2", img2)
309
+
310
+ contours_intersect = poly.contours_intersect(np.array(polygon1), np.array(polygon2))
311
+ img_contour = poly.draw_contours(img, np.array(contours_intersect))
312
+ show_image("contours_intersect", img_contour)
313
+
314
+ contours_union = poly.contours_union(np.array(polygon1), np.array(polygon2))
315
+ img_contour = poly.draw_contours(img, np.array(contours_union))
316
+ show_image("contours_union", img_contour)
317
+
318
+ contours_difference = poly.contours_difference(np.array(polygon1), np.array(polygon2))
319
+ img_contour = poly.draw_contours(img, np.array(contours_difference))
320
+ show_image("contours_difference", img_contour)
321
+
322
+ iou = poly.contours_iou(np.array(polygon1), np.array(polygon2))
323
+ print("交并比:", iou)
assembly/__init__.py ADDED
@@ -0,0 +1,165 @@
1
+ # assembly包初始化文件
2
+
3
+ # 导出所有模块的函数
4
+ from .CornerDetection import *
5
+ from .DrawImage import *
6
+ from .EdgeDetection import *
7
+ from .GeneralUtil import *
8
+ from .GeometricChanges import *
9
+ from .ImageEnhance import *
10
+ from .ImageOperation import *
11
+ from .ImageSegmentation import *
12
+ from .MorphologicalChange import *
13
+ from .PolygonOperation import *
14
+
15
+ __all__ = [
16
+ # CornerDetection
17
+ 'CornerDetection',
18
+ 'harris_corner_detection',
19
+ 'shi_tomasi_corner_detection',
20
+ 'fast_corner_detection',
21
+
22
+ # DrawImage
23
+ 'DrawImage',
24
+ 'draw_circle',
25
+ 'draw_rectangle',
26
+ 'draw_line',
27
+ 'draw_text',
28
+ 'generate_blank_image',
29
+
30
+ # EdgeDetection
31
+ 'EdgeDetection',
32
+ 'canny_edge_detection',
33
+ 'sobel_edge_detection',
34
+ 'laplacian_edge_detection',
35
+
36
+ # GeneralUtil
37
+ 'GeneralUtil',
38
+ 'color_transform_gray',
39
+ 'color_transform_hsv',
40
+ 'color_transform_lab',
41
+ 'color_transform_yuv',
42
+ 'color_transform_rgb',
43
+ 'image_blend',
44
+ 'normalize_image',
45
+ 'resize_image',
46
+ 'rotate_image',
47
+ 'flip_image',
48
+ 'crop_image',
49
+ 'threshold_image',
50
+ 'adaptive_threshold_image',
51
+ 'histogram_equalization',
52
+ 'clahe_equalization',
53
+ 'gaussian_blur',
54
+ 'median_blur',
55
+ 'bilateral_filter',
56
+ 'sharpen_image',
57
+ 'denoise_image',
58
+ 'calculate_histogram',
59
+ 'calculate_mean',
60
+ 'calculate_std',
61
+ 'calculate_min_max',
62
+ 'calculate_entropy',
63
+ 'calculate_gradient',
64
+ 'calculate_laplacian',
65
+ 'calculate_sobel',
66
+ 'calculate_canny',
67
+ 'calculate_harris',
68
+ 'calculate_shi_tomasi',
69
+ 'calculate_fast',
70
+ 'calculate_orb',
71
+ 'calculate_sift',
72
+ 'calculate_surf',
73
+ 'calculate_akaze',
74
+ 'calculate_brisk',
75
+ 'calculate_kaze',
76
+ 'calculate_mser',
77
+ 'calculate_good_features_to_track',
78
+ 'calculate_corner_sub_pix',
79
+ 'calculate_find_homography',
80
+ 'calculate_perspective_transform',
81
+ 'calculate_warp_perspective',
82
+ 'calculate_affine_transform',
83
+ 'calculate_warp_affine',
84
+ 'calculate_remap',
85
+ 'calculate_resize',
86
+ 'calculate_rotate',
87
+ 'calculate_flip',
88
+ 'calculate_crop',
89
+ 'calculate_threshold',
90
+ 'calculate_adaptive_threshold',
91
+ 'calculate_histogram_equalization',
92
+ 'calculate_clahe_equalization',
93
+ 'calculate_gaussian_blur',
94
+ 'calculate_median_blur',
95
+ 'calculate_bilateral_filter',
96
+ 'calculate_sharpen_image',
97
+ 'calculate_denoise_image',
98
+
99
+ # GeometricChanges
100
+ 'GeometricChanges',
101
+ 'geometric_resize',
102
+ 'geometric_rotate',
103
+ 'geometric_flip',
104
+ 'geometric_crop',
105
+ 'geometric_translate',
106
+ 'geometric_scale',
107
+ 'geometric_affine',
108
+ 'geometric_perspective',
109
+
110
+ # ImageEnhance
111
+ 'ImageEnhance',
112
+ 'enhance_brightness',
113
+ 'enhance_contrast',
114
+ 'enhance_saturation',
115
+ 'enhance_sharpness',
116
+ 'enhance_gamma',
117
+ 'enhance_histogram',
118
+ 'enhance_clahe',
119
+
120
+ # ImageOperation
121
+ 'ImageOperation',
122
+ 'image_add',
123
+ 'image_subtract',
124
+ 'image_multiply',
125
+ 'image_divide',
126
+ 'image_bitwise_and',
127
+ 'image_bitwise_or',
128
+ 'image_bitwise_xor',
129
+ 'image_bitwise_not',
130
+ 'image_mask',
131
+
132
+ # ImageSegmentation
133
+ 'ImageSegmentation',
134
+ 'segmentation_threshold',
135
+ 'segmentation_adaptive_threshold',
136
+ 'segmentation_watershed',
137
+ 'segmentation_grabcut',
138
+ 'segmentation_mean_shift',
139
+ 'segmentation_kmeans',
140
+ 'segmentation_spectral',
141
+
142
+ # MorphologicalChange
143
+ 'MorphologicalChange',
144
+ 'mlc_erode',
145
+ 'mlc_dilate',
146
+ 'mlc_open',
147
+ 'mlc_close',
148
+ 'mlc_gradient',
149
+ 'mlc_tophat',
150
+ 'mlc_blackhat',
151
+
152
+ # PolygonOperation
153
+ 'PolygonOperation',
154
+ 'polygon_contour_detection',
155
+ 'polygon_contour_area',
156
+ 'polygon_contour_perimeter',
157
+ 'polygon_convex_hull',
158
+ 'polygon_bounding_rect',
159
+ 'polygon_min_area_rect',
160
+ 'polygon_min_enclosing_circle',
161
+ 'polygon_approx_poly_dp',
162
+ 'polygon_circle_detection',
163
+ 'polygon_line_detection',
164
+ 'polygon_shape_detection',
165
+ ]
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.1
2
+ Name: assembly-opencv
3
+ Version: 1.0.0
4
+ Summary: Image processing utilities for Python with OpenCV
5
+ Home-page:
6
+ Author: Zven
7
+ Author-email: zven@zven.xyz
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Requires-Dist: numpy
13
+ Requires-Dist: opencv-python
14
+ Requires-Dist: Pillow
15
+
16
+ A collection of image processing functions for computer vision tasks with OpenCV
@@ -0,0 +1,15 @@
1
+ assembly/CornerDetection.py,sha256=3aa6zilFuU8neTyeJgqTMuVobQRmJKbYyrNgD8O5pYI,4195
2
+ assembly/DrawImage.py,sha256=g9t3OBeX_TcdrLK_nNv0rKpsk-NrTs6ng_sYj5t92Xo,10630
3
+ assembly/EdgeDetection.py,sha256=MTMqf-N1eMMctsyGQnOoGY7qnon5O1O5eRm1OYXd_Z4,7743
4
+ assembly/GeneralUtil.py,sha256=yjLNyeXSkJ3VN09kVOYj__ZxbG2uhzCMqqVK__7SRw4,2746
5
+ assembly/GeometricChanges.py,sha256=v-cq0mW4lWoexTvN1q5fJgsMxB9qNzDOwWoDNtMERqM,6007
6
+ assembly/ImageEnhance.py,sha256=_H_-OsYnrARH8b-8C8mrj4KiOQ6xAgH5wTVBgZibGtA,9263
7
+ assembly/ImageOperation.py,sha256=y68NzACSyIPI-5Bm4_2dPvkAs70fDFcuLVh8_spWKz8,5501
8
+ assembly/ImageSegmentation.py,sha256=8pw-gyEO6FeMqUMbSsB7JL86-_0j9lSLAXMTi7i2HCg,7939
9
+ assembly/MorphologicalChange.py,sha256=XbZyVdkJXKmsWok9JOa8toH1GS-pmXrwdm8R757r_fU,9919
10
+ assembly/PolygonOperation.py,sha256=DLEvWEUFY_Rxeiqy1WxKw3qNOkR8eWbiUfuZy9VVyVI,10915
11
+ assembly/__init__.py,sha256=DYzr16XXFUEKvmyqqsnB5BQispL1MRaGGUxyv62dnik,3951
12
+ assembly_opencv-1.0.0.dist-info/METADATA,sha256=h5kxDVVj8Vb1bDdxSq3MGrIl0UVuuNJOoR5-uvnaPtk,510
13
+ assembly_opencv-1.0.0.dist-info/WHEEL,sha256=hPN0AlP2dZM_3ZJZWP4WooepkmU9wzjGgCLCeFjkHLA,92
14
+ assembly_opencv-1.0.0.dist-info/top_level.txt,sha256=Lz_5VS2mRAR7AduMlKl3vVyAOekcs_JNGew8waVVLas,9
15
+ assembly_opencv-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.46.3)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ assembly