-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobel_edge_detection.py
57 lines (46 loc) · 1.98 KB
/
sobel_edge_detection.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
python
# 边缘检测使用Sobel算子
def sobel_edge_detection(image):
edge_sobel = cv2.Sobel(image, cv2.CV_64F, 1, 1, ksize=3)
return edge_sobel
# 对比度增强可以使用直方图均衡化
def contrast_enhancement(image):
img_y_cr_cb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
y, cr, cb = cv2.split(img_y_cr_cb)
# 对亮度通道Y应用直方图均衡化
y_eq = cv2.equalizeHist(y)
img_y_cr_cb_eq = cv2.merge((y_eq, cr, cb))
img_contrast_enhanced = cv2.cvtColor(img_y_cr_cb_eq, cv2.COLOR_YCrCb2BGR)
return img_contrast_enhanced
# 图像锐化可以通过构建一个高通滤波器
def sharpen_image(image):
kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
image_sharpened = cv2.filter2D(image, -1, kernel)
return image_sharpened
# 图像去噪可以使用多种方法,这里示例使用UMS噪声消除
# 由于UMS噪声消除不是一个标准的OpenCV函数,这里用高斯模糊代替
def ums_noise_reduction(image):
return cv2.GaussianBlur(image, (3, 3), 0)
class ImagePreprocessing(nn.Module):
def __init__(self):
super(ImagePreprocessing, self).__init__()
def forward(self, image_path):
# 读取图像
image = cv2.imread(image_path)
# 应用边缘检测
edge_detected_image = sobel_edge_detection(image)
# 应用对比度增强
contrast_enhanced_image = contrast_enhancement(image)
# 应用图像锐化
sharpened_image = sharpen_image(image)
# 应用图像去噪
noise_reduced_image = ums_noise_reduction(image)
# 根据原始流程图,以上步骤可能需要组合或顺序执行
# 这里假设最终预处理图像是各步骤结果的组合
preprocessed_image = np.maximum.reduce([
edge_detected_image,
contrast_enhanced_image,
sharpened_image,
noise_reduced_image
])
return preprocessed_image