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,171 @@
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image, ImageDraw, ImageFont
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
+ @class_description(
16
+ title="几何变化",
17
+ description="在图像上绘制轮廓,包括点,线,矩形,圆等基本图形",
18
+ level=3
19
+ )
20
+ class GeometricChanges(object):
21
+
22
+ @func_description(
23
+ title="图像翻转",
24
+ description="对图像进行水平或垂直翻转"
25
+ )
26
+ @staticmethod
27
+ def flip_image(img, mode='horizontal'):
28
+ """
29
+ :name 图像翻转
30
+ :param img: 原始图
31
+ :param mode: 翻转模式,'horizontal'表示水平翻转,'vertical'表示垂直翻转
32
+ :return img: 翻转后的图像
33
+ """
34
+ if mode == 'horizontal':
35
+ img_flipped = cv2.flip(img, 1)
36
+ elif mode == 'vertical':
37
+ img_flipped = cv2.flip(img, 0)
38
+ else:
39
+ raise ValueError("Invalid mode. Use 'horizontal' or 'vertical'.")
40
+
41
+ return img_flipped
42
+
43
+ @func_description(
44
+ title="图像旋转",
45
+ description="对图像进行旋转"
46
+ )
47
+ @staticmethod
48
+ def rotate_image(img, angle):
49
+ """
50
+ :name 图像旋转
51
+ :param img: 原始图
52
+ :param angle: 旋转角度,正值表示逆时针旋转,负值表示顺时针旋转
53
+ :return img: 旋转后的图像
54
+ """
55
+ (h, w) = img.shape[:2]
56
+ center = (w // 2, h // 2)
57
+
58
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
59
+ img_rotated = cv2.warpAffine(img, M, (w, h))
60
+
61
+ return img_rotated
62
+
63
+ @func_description(
64
+ title="图像缩放",
65
+ description="对图像进行缩放"
66
+ )
67
+ @staticmethod
68
+ def scale_image(img, scale_x, scale_y):
69
+ """
70
+ :name 图像缩放
71
+ :param img: 原始图
72
+ :param scale_x: 水平缩放因子
73
+ :param scale_y: 垂直缩放因子
74
+ :return img: 缩放后的图像
75
+ """
76
+ img_scaled = cv2.resize(img, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR)
77
+ return img_scaled
78
+
79
+ @func_description(
80
+ title="图像裁剪",
81
+ description="对图像进行裁剪"
82
+ )
83
+ @staticmethod
84
+ def crop_image(img, x, y, width, height):
85
+ """
86
+ :name 图像裁剪
87
+ :param img: 原始图
88
+ :param x: 裁剪区域左上角x坐标
89
+ :param y: 裁剪区域左上角y坐标
90
+ :param width: 裁剪区域宽度
91
+ :param height: 裁剪区域高度
92
+ :return img: 裁剪后的图像
93
+ """
94
+ img_cropped = img[y:y+height, x:x+width]
95
+ return img_cropped
96
+
97
+ @func_description(
98
+ title="图像透视变换",
99
+ description="将图像从一个视平面投影到另一个视平面的几何变换,用于解决图像的透视畸变问题(如近大远小的视觉效果),或实现视角转换(如从倾斜图像恢复正视图)"
100
+ )
101
+ @staticmethod
102
+ def perspective_transform(img, src_points, dst_points):
103
+ """
104
+ :name 图像透视变换
105
+ :param img: 原始图
106
+ :param src_points: 源点坐标,形如[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
107
+ :param dst_points: 目标点坐标,形如[[x1', y1'], [x2', y2'], [x3', y3'], [x4', y4']]
108
+ :return img: 透视变换后的图像
109
+ """
110
+ src_pts = np.array(src_points, dtype=np.float32)
111
+ dst_pts = np.array(dst_points, dtype=np.float32)
112
+
113
+ M = cv2.getPerspectiveTransform(src_pts, dst_pts)
114
+ (h, w) = img.shape[:2]
115
+ img_transformed = cv2.warpPerspective(img, M, (w, h))
116
+
117
+ return img_transformed
118
+
119
+ @func_description(
120
+ title="图像仿射变换",
121
+ description="仿射变换也称仿射投影,是指几何中,对一个向量空间进行线性变换并接上一个平移,变换为另一个向量空间"
122
+ )
123
+ @staticmethod
124
+ def wrap_affine(img, src_points, dst_points):
125
+ """
126
+ :name 图像仿射变换
127
+ :param img: 原始图
128
+ :param src_points: 源点坐标,形如[[x1, y1], [x2, y2], [x3, y3]]
129
+ :param dst_points: 目标点坐标,形如[[x1', y1'], [x2', y2'], [x3', y3']]
130
+ :return img: 仿射变换后的图像
131
+ """
132
+ src_pts = np.array(src_points, dtype=np.float32)
133
+ dst_pts = np.array(dst_points, dtype=np.float32)
134
+
135
+ M = cv2.getAffineTransform(src_pts, dst_pts)
136
+ (h, w) = img.shape[:2]
137
+ img_transformed = cv2.warpAffine(img, M, (w, h))
138
+
139
+ return img_transformed
140
+
141
+ # 暴露方法
142
+ def flip_image(img, mode='horizontal'):
143
+ return GeometricChanges.flip_image(img, mode)
144
+
145
+ def rotate_image(img, angle):
146
+ return GeometricChanges.rotate_image(img, angle)
147
+
148
+ def scale_image(img, scale_x, scale_y):
149
+ return GeometricChanges.scale_image(img, scale_x, scale_y)
150
+
151
+ def crop_image(img, x, y, width, height):
152
+ return GeometricChanges.crop_image(img, x, y, width, height)
153
+
154
+ def perspective_transform(img, src_points, dst_points):
155
+ return GeometricChanges.perspective_transform(img, src_points, dst_points)
156
+
157
+ def wrap_affine(img, src_points, dst_points):
158
+ return GeometricChanges.wrap_affine(img, src_points, dst_points)
159
+
160
+
161
+
162
+ if __name__ == '__main__':
163
+ geo = GeometricChanges()
164
+ img = cv2.imdecode(np.fromfile(r"D:\新能源\2025-08\top\20250827165052_186_32_1(1).jpg", dtype=np.uint8),
165
+ cv2.IMREAD_COLOR)
166
+ show_image("img", img)
167
+
168
+ trans_img = geo.wrap_affine(img, [[100,100],[200,100],[100,200]], [[150,150],[250,150],[150,250]])
169
+ show_image("wrap_affine",trans_img)
170
+ trans_img = geo.perspective_transform(img, [[100,100],[200,100],[100,200],[200,200]], [[150,150],[250,150],[150,250],[250,250]])
171
+ show_image("perspective_transform",trans_img)
@@ -0,0 +1,242 @@
1
+ import cv2
2
+ import numpy as np
3
+
4
+ from assembly.description.Decoration import func_description, class_description
5
+ from assembly.GeneralUtil import show_image
6
+
7
+ '''
8
+ 图像增强
9
+
10
+ 输入:任意图像
11
+ 输出:任意图像
12
+ '''
13
+
14
+
15
+ @class_description(
16
+ title="图像增强",
17
+ description="图像增强是指对图像进行各种增强处理,以提升图像的视觉效果。",
18
+ level=5
19
+ )
20
+ class ImageEnhance(object):
21
+
22
+ @func_description(
23
+ title="亮度/对比度调整",
24
+ description="通过调整图像的亮度,使得图像更加鲜艳,适用彩色/灰色图像"
25
+ )
26
+ @staticmethod
27
+ def enhance_brightness(img, alpha=1.0, beta=0.0):
28
+ """
29
+ :name 亮度/对比度调整
30
+ :param img: 多通道图
31
+ :param alpha: 亮度调节因子
32
+ :param beta: 亮度偏移量
33
+ :return img: 多通道图
34
+ """
35
+ return cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
36
+
37
+ @func_description(
38
+ title="伽马校正",
39
+ description="通过对图像进行非线性拉伸,使得图像的对比度更加均匀,适用彩色/灰色图像"
40
+ )
41
+ @staticmethod
42
+ def enhance_gamma_correction(img, gamma=1.0):
43
+ """
44
+ :name 伽马校正
45
+ :param img: 多通道图
46
+ :param gamma: 伽马值
47
+ :return img: 多通道图
48
+ """
49
+ inv_gamma = 1.0 / gamma
50
+ table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
51
+ return cv2.LUT(img, table)
52
+
53
+ @func_description(
54
+ title="均值滤波",
55
+ description="通过减少图像中的噪声,使得图像更加平滑,适用彩色/灰色图像"
56
+ )
57
+ @staticmethod
58
+ def enhance_smoothing(img, kernel_size=3):
59
+ """
60
+ :name 均值滤波
61
+ :param img: 多通道图
62
+ :param kernel_size: 卷积核大小,必须为奇数
63
+ :return img: 多通道图
64
+ """
65
+ if kernel_size % 2 == 0:
66
+ kernel_size += 1
67
+ return cv2.blur(img, (kernel_size, kernel_size))
68
+
69
+ @func_description(
70
+ title="高斯滤波",
71
+ description="通过对图像进行平滑,使得图像更加平滑,适用彩色/灰色图像"
72
+ )
73
+ @staticmethod
74
+ def enhance_gaussian_filtering(img, kernel_size=3, sigma=0):
75
+ """
76
+ :name 高斯滤波
77
+ :param img: 多通道图
78
+ :param kernel_size: 卷积核大小,必须为奇数
79
+ :param sigma: 标准差
80
+ :return img: 多通道图
81
+ """
82
+ if kernel_size % 2 == 0:
83
+ kernel_size += 1
84
+ return cv2.GaussianBlur(img, (kernel_size, kernel_size), sigma)
85
+
86
+ @func_description(
87
+ title="中值滤波",
88
+ description="通过减少图像中的椒盐噪声,使得图像更加平滑,适用彩色/灰色图像"
89
+ )
90
+ @staticmethod
91
+ def enhance_median_filtering(img, kernel_size=3):
92
+ """
93
+ :name 中值滤波
94
+ :param img: 多通道图
95
+ :param kernel_size: 卷积核大小,必须为奇数
96
+ :return img: 多通道图
97
+ """
98
+ if kernel_size % 2 == 0:
99
+ kernel_size += 1
100
+ return cv2.medianBlur(img, kernel_size)
101
+
102
+ @func_description(
103
+ title="双边滤波",
104
+ description="通过在平滑图像的同时保留边缘信息,使得图像更加清晰,适用彩色/灰色图像"
105
+ )
106
+ @staticmethod
107
+ def enhance_bilateral_filtering(img, d=9, sigma_color=75, sigma_space=75):
108
+ """
109
+ :name 双边滤波
110
+ :param img: 多通道图
111
+ :param d: 领域直径
112
+ :param sigma_color: 颜色空间标准差
113
+ :param sigma_space: 坐标空间标准差
114
+ :return img: 多通道图
115
+ """
116
+ return cv2.bilateralFilter(img, d, sigma_color, sigma_space)
117
+
118
+ @func_description(
119
+ title="锐化",
120
+ description="通过增强图像的边缘信息,使得图像更加清晰,适用彩色/灰色图像"
121
+ )
122
+ @staticmethod
123
+ def enhance_sharpening(img, kernel_size=3):
124
+ """
125
+ :name 锐化
126
+ :param img: 多通道图
127
+ :param kernel_size: 卷积核大小,必须为奇数
128
+ :return img: 多通道图
129
+ """
130
+ kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
131
+ if kernel_size % 2 == 0:
132
+ kernel_size += 1
133
+ return cv2.filter2D(img, -1, kernel, borderType=cv2.BORDER_REPLICATE)
134
+
135
+ @func_description(
136
+ title="拉普拉斯增强",
137
+ description="通过对图像进行非线性拉伸,使得图像的对比度更加均匀,适用于灰度图像"
138
+ )
139
+ @staticmethod
140
+ def enhance_laplacian_enhancement(gray, kernel_size=3):
141
+ """
142
+ :name 拉普拉斯增强
143
+ :param gray: 单通道图
144
+ :param kernel_size: 卷积核大小,必须为奇数
145
+ :return gray: 单通道图
146
+ """
147
+ if kernel_size % 2 == 0:
148
+ kernel_size += 1
149
+ return cv2.Laplacian(gray, cv2.CV_64F, ksize=kernel_size)
150
+
151
+ @func_description(
152
+ title="直方图均衡化",
153
+ description="通过调整图像的对比度来增强图像的视觉效果,适用于灰度图像"
154
+ )
155
+ @staticmethod
156
+ def enhance_histogram_equalization(gray):
157
+ """
158
+ :name 直方图均衡化
159
+ :param gray: 单通道图
160
+ :return gray: 单通道图
161
+ """
162
+ equalized = cv2.equalizeHist(gray)
163
+ return equalized
164
+
165
+ @func_description(
166
+ title="CLAHE(自适应直方图均衡化):",
167
+ description="通过对图像进行自适应直方图均衡化,可以增强图像的对比度和亮度,适用于灰度图像"
168
+ )
169
+ @staticmethod
170
+ def enhance_adaptive_histogram_equalization(gray, clip_limit=2.0, tile_size=(8, 8)):
171
+ """
172
+ :name CLAHE(自适应直方图均衡化)
173
+ :param gray: 单通道图
174
+ :param clip_limit: 裁剪限值
175
+ :param tile_size: 块大小
176
+ :return gray: 单通道图
177
+ """
178
+ clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_size)
179
+ return clahe.apply(gray)
180
+
181
+
182
+ # 暴露方法
183
+ def enhance_brightness(img, alpha=1.5, beta=0):
184
+ return ImageEnhance.enhance_brightness(img, alpha, beta)
185
+
186
+ def enhance_gamma_correction(img, gamma=1.5):
187
+ return ImageEnhance.enhance_gamma_correction(img, gamma)
188
+
189
+ def enhance_smoothing(img, kernel_size=3):
190
+ return ImageEnhance.enhance_smoothing(img, kernel_size)
191
+
192
+ def enhance_gaussian_filtering(img, kernel_size=3, sigma=0):
193
+ return ImageEnhance.enhance_gaussian_filtering(img, kernel_size, sigma)
194
+
195
+ def enhance_median_filtering(img, kernel_size=3):
196
+ return ImageEnhance.enhance_median_filtering(img, kernel_size)
197
+
198
+ def enhance_bilateral_filtering(img, d=9, sigma_color=75, sigma_space=75):
199
+ return ImageEnhance.enhance_bilateral_filtering(img, d, sigma_color, sigma_space)
200
+
201
+ def enhance_sharpening(img, kernel_size=3):
202
+ return ImageEnhance.enhance_sharpening(img, kernel_size)
203
+
204
+ def enhance_laplacian_enhancement(img, kernel_size=3):
205
+ return ImageEnhance.enhance_laplacian_enhancement(img, kernel_size)
206
+
207
+ def enhance_histogram_equalization(img):
208
+ return ImageEnhance.enhance_histogram_equalization(img)
209
+
210
+ def enhance_adaptive_histogram_equalization(img, clip_limit=2.0, tile_size=(8, 8)):
211
+ return ImageEnhance.enhance_adaptive_histogram_equalization(img, clip_limit, tile_size)
212
+
213
+
214
+
215
+ if __name__ == '__main__':
216
+ image_enhance = ImageEnhance()
217
+ img = cv2.imdecode(np.fromfile(r"D:\新能源\2025-08\top\20250827165052_186_32_1(1).jpg", dtype=np.uint8),
218
+ cv2.IMREAD_COLOR)
219
+
220
+ show_image("gray_img", img)
221
+ enhanced_img = image_enhance.enhance_brightness(img, alpha=1.5, beta=0)
222
+ show_image("enhance_brightness", enhanced_img)
223
+ enhanced_img = image_enhance.enhance_gamma_correction(img, gamma=1.5)
224
+ show_image("enhance_gamma_correction", enhanced_img)
225
+ enhanced_img = image_enhance.enhance_laplacian_enhancement(img, kernel_size=3)
226
+ show_image("enhance_laplacian_enhancement", enhanced_img)
227
+ enhanced_img = image_enhance.enhance_smoothing(img, kernel_size=3)
228
+ show_image("enhance_smoothing", enhanced_img)
229
+ enhanced_img = image_enhance.enhance_gaussian_filtering(img, kernel_size=3, sigma=0)
230
+ show_image("enhance_gaussian_filtering", enhanced_img)
231
+ enhanced_img = image_enhance.enhance_median_filtering(img, kernel_size=3)
232
+ show_image("enhance_median_filtering", enhanced_img)
233
+ enhanced_img = image_enhance.enhance_bilateral_filtering(gray_img, d=9, sigma_color=75, sigma_space=75)
234
+ show_image("enhance_bilateral_filtering", enhanced_img)
235
+ enhanced_img = image_enhance.enhance_sharpening(img, kernel_size=3)
236
+ show_image("enhance_sharpening", enhanced_img)
237
+
238
+ gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
239
+ enhanced_img = image_enhance.enhance_histogram_equalization(gray_img)
240
+ show_image("enhance_histogram_equalization", enhanced_img)
241
+ enhanced_img = image_enhance.enhance_adaptive_histogram_equalization(gray_img)
242
+ show_image("enhance_adaptive_histogram_equalization", enhanced_img)
@@ -0,0 +1,186 @@
1
+ import cv2
2
+ import numpy as np
3
+
4
+ from assembly.description.Decoration import func_description, class_description
5
+ from assembly.GeneralUtil import show_image
6
+
7
+ '''
8
+ 图像运算
9
+
10
+ 输入:原始图
11
+ 输出:原始图
12
+ '''
13
+
14
+
15
+ @class_description(
16
+ title="图像运算",
17
+ description="图像运算主要是指对图像进行各种数学运算以实现图像的增强、变换和分析等功能。",
18
+ level=6
19
+ )
20
+ class ImageOperation(object):
21
+
22
+ @func_description(
23
+ title="加法运算",
24
+ description="实现饱和加法(结果超过255时截断为255)"
25
+ )
26
+ @staticmethod
27
+ def image_add(img1, img2):
28
+ """
29
+ :name 加法运算
30
+ :param img1: 原始图1
31
+ :param img2: 原始图2
32
+ :return: img: 加法运算后的图像
33
+ """
34
+ img_added = cv2.add(img1, img2)
35
+ return img_added
36
+
37
+ @func_description(
38
+ title="减法运算",
39
+ description="实现饱和减法(结果小于0时截断为0)"
40
+ )
41
+ @staticmethod
42
+ def image_subtract(img1, img2):
43
+ """
44
+ :name 减法运算
45
+ :param img1: 原始图1
46
+ :param img2: 原始图2
47
+ :return: img: 减法运算后的图像
48
+ """
49
+ img_subtracted = cv2.subtract(img1, img2)
50
+ return img_subtracted
51
+
52
+ @func_description(
53
+ title="乘法运算",
54
+ description="对两个图像的每个像素进行逐元素乘法操作,增强对比度"
55
+ )
56
+ @staticmethod
57
+ def image_multiply(img1, img2):
58
+ """
59
+ :name 乘法运算
60
+ :param img1: 原始图1
61
+ :param img2: 原始图2
62
+ :return: img: 乘法运算后的图像
63
+ """
64
+ img_multiplied = cv2.multiply(img1, img2)
65
+ return img_multiplied
66
+
67
+ @func_description(
68
+ title="除法运算",
69
+ description="对两个图像的每个像素进行逐元素除法操作,用于光照归一化"
70
+ )
71
+ @staticmethod
72
+ def image_divide(img1, img2):
73
+ """
74
+ :name 除法运算
75
+ :param img1: 原始图1
76
+ :param img2: 原始图2
77
+ :return: img: 除法运算后的图像
78
+ """
79
+ img_divided = cv2.divide(img1, img2)
80
+ return img_divided
81
+
82
+ @func_description(
83
+ title="按位与运算",
84
+ description="对两个图像的每个像素进行按位与操作"
85
+ )
86
+ @staticmethod
87
+ def image_bitwise_and(img1, img2):
88
+ """
89
+ :name 按位与运算
90
+ :param img1: 原始图1
91
+ :param img2: 原始图2
92
+ :return: img: 按位与运算后的图像
93
+ """
94
+ img_and = cv2.bitwise_and(img1, img2)
95
+ return img_and
96
+
97
+ @func_description(
98
+ title="按位或运算",
99
+ description="对两个图像的每个像素进行按位或操作"
100
+ )
101
+ @staticmethod
102
+ def image_bitwise_or(img1, img2):
103
+ """
104
+ :name 按位或运算
105
+ :param img1: 原始图1
106
+ :param img2: 原始图2
107
+ :return: img: 按位或运算后的图像
108
+ """
109
+ img_or = cv2.bitwise_or(img1, img2)
110
+ return img_or
111
+
112
+ @func_description(
113
+ title="按位异或运算",
114
+ description="对两个图像的每个像素进行按位异或操作"
115
+ )
116
+ @staticmethod
117
+ def image_bitwise_xor(img1, img2):
118
+ """
119
+ :name 按位异或运算
120
+ :param img1: 原始图1
121
+ :param img2: 原始图2
122
+ :return: img: 按位异或运算后的图像
123
+ """
124
+ img_xor = cv2.bitwise_xor(img1, img2)
125
+ return img_xor
126
+
127
+ @func_description(
128
+ title="按位非运算",
129
+ description="对图像的每个像素进行按位非操作"
130
+ )
131
+ @staticmethod
132
+ def image_bitwise_not(img):
133
+ """
134
+ :name 按位非运算
135
+ :param img: 原始图
136
+ :return: img: 按位非运算后的图像
137
+ """
138
+ img_not = cv2.bitwise_not(img)
139
+ return img_not
140
+
141
+ @func_description(
142
+ title="图像加权混合",
143
+ description="通过加权平均的方式将两幅图像混合在一起"
144
+ )
145
+ @staticmethod
146
+ def image_blend(img1, img2, alpha=0.5, beta=0.5, gamma=0):
147
+ """
148
+ :name 图像加权混合
149
+ :param img1: 原始图1
150
+ :param img2: 原始图2
151
+ :param alpha: 图像1的权重
152
+ :param beta: 图像2的权重
153
+ :param gamma: 标量值,添加到每个和中
154
+ :return: img: 混合后的图像
155
+ """
156
+ img_blended = cv2.addWeighted(img1, alpha, img2, beta, gamma)
157
+ return img_blended
158
+
159
+ # 暴露方法
160
+ def image_add(img1, img2):
161
+ return ImageOperation.image_add(img1, img2)
162
+
163
+ def image_subtract(img1, img2):
164
+ return ImageOperation.image_subtract(img1, img2)
165
+
166
+ def image_multiply(img1, img2):
167
+ return ImageOperation.image_multiply(img1, img2)
168
+
169
+ def image_divide(img1, img2):
170
+ return ImageOperation.image_divide(img1, img2)
171
+
172
+ def image_bitwise_and(img1, img2):
173
+ return ImageOperation.image_bitwise_and(img1, img2)
174
+
175
+ def image_bitwise_or(img1, img2):
176
+ return ImageOperation.image_bitwise_or(img1, img2)
177
+
178
+ def image_bitwise_xor(img1, img2):
179
+ return ImageOperation.image_bitwise_xor(img1, img2)
180
+
181
+ def image_bitwise_not(img):
182
+ return ImageOperation.image_bitwise_not(img)
183
+
184
+ def image_blend(img1, img2, alpha=0.5, beta=0.5, gamma=0):
185
+ return ImageOperation.image_blend(img1, img2, alpha, beta, gamma)
186
+