File size: 4,459 Bytes
5cf4e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np

class StripeRemover:
    def __init__(self):
        pass
    
    def fourier_method(self, image):
        """傅里叶变换去除条纹"""
        # 转换为灰度图像
        if len(image.shape) > 2:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        else:
            gray = image.copy()
            
        # 傅里叶变换
        dft = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT)
        dft_shift = np.fft.fftshift(dft)
        
        # 创建掩模
        rows, cols = gray.shape
        crow, ccol = rows // 2, cols // 2
        mask = np.ones((rows, cols, 2), np.uint8)
        mask[crow-5:crow+5, :] = 0  # 调整带宽以匹配条纹频率
        
        # 应用掩模和逆变换
        fshift = dft_shift * mask
        f_ishift = np.fft.ifftshift(fshift)
        img_back = cv2.idft(f_ishift)
        img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])
        
        # 归一化处理
        img_back = cv2.normalize(img_back, None, 0, 255, cv2.NORM_MINMAX)
        return np.uint8(img_back)
    
    def morphological_method(self, image):
        """形态学操作去除条纹"""
        # Convert to grayscale if needed
        if len(image.shape) > 2:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        else:
            gray = image.copy()
            
        # Create horizontal kernel (adjust size if needed)
        horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1))
        
        # Detect lines using morphological operations
        detected_lines = cv2.morphologyEx(gray, cv2.MORPH_OPEN, horizontal_kernel)
        
        # Remove detected lines and normalize
        result = cv2.subtract(gray, detected_lines)
        
        # Normalize and enhance contrast
        result = cv2.normalize(result, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
        
        # Convert back to BGR for display
        return cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
    
    def adaptive_threshold_method(self, image):
        """自适应阈值处理"""
        if len(image.shape) > 2:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        else:
            gray = image.copy()
            
        # 自适应阈值处理
        thresh = cv2.adaptiveThreshold(gray, 255, 
                                     cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                     cv2.THRESH_BINARY, 11, 2)
        
        # 中值滤波去噪
        result = cv2.medianBlur(thresh, 3)
        return result
    
    def enhance_image(self, image):
        """图像增强处理"""
        # CLAHE对比度增强
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
        enhanced = clahe.apply(image)
        
        # 锐化处理
        kernel = np.array([[-1,-1,-1],
                         [-1, 9,-1],
                         [-1,-1,-1]])
        sharpened = cv2.filter2D(enhanced, -1, kernel)
        
        return sharpened
    
    def adaptive_enhance(self, image):
        """自适应阈值 + 图像增强"""
        thresh_result = self.adaptive_threshold_method(image)
        return self.enhance_image(thresh_result)
        
    def fourier_enhance(self, image):
        """傅里叶变换 + 图像增强"""
        fourier_result = self.fourier_method(image)
        return self.enhance_image(fourier_result)
        
    def morphological_enhance(self, image):
        """形态学操作 + 图像增强"""
        morph_result = self.morphological_method(image)
        # Convert BGR to gray for enhance_image
        gray = cv2.cvtColor(morph_result, cv2.COLOR_BGR2GRAY)
        enhanced = self.enhance_image(gray)
        return cv2.cvtColor(enhanced, cv2.COLOR_GRAY2BGR)
        
    def adaptive_fourier(self, image):
        """自适应阈值 + 傅里叶变换"""
        thresh_result = self.adaptive_threshold_method(image)
        return self.fourier_method(thresh_result)
        
    def morphological_adaptive(self, image):
        """形态学操作 + 自适应阈值"""
        morph_result = self.morphological_method(image)
        gray = cv2.cvtColor(morph_result, cv2.COLOR_BGR2GRAY)
        return self.adaptive_threshold_method(gray)
        
    def fourier_morphological(self, image):
        """傅里叶变换 + 形态学操作"""
        fourier_result = self.fourier_method(image)
        return self.morphological_method(fourier_result)