|
|
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): |
|
|
"""形态学操作去除条纹""" |
|
|
|
|
|
if len(image.shape) > 2: |
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
|
else: |
|
|
gray = image.copy() |
|
|
|
|
|
|
|
|
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1)) |
|
|
|
|
|
|
|
|
detected_lines = cv2.morphologyEx(gray, cv2.MORPH_OPEN, horizontal_kernel) |
|
|
|
|
|
|
|
|
result = cv2.subtract(gray, detected_lines) |
|
|
|
|
|
|
|
|
result = cv2.normalize(result, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8) |
|
|
|
|
|
|
|
|
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 = 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) |
|
|
|
|
|
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) |