23 lines
811 B
Python
23 lines
811 B
Python
|
|
import cv2
|
||
|
|
import numpy as np
|
||
|
|
import os
|
||
|
|
|
||
|
|
NOISE_THRESHOLD = int(os.getenv("NOISE_THRESHOLD", "10")) # 示例阈值,单位为噪声的标准差
|
||
|
|
|
||
|
|
def pre_process(image):
|
||
|
|
if len(image.shape) == 3: # 如果是彩色图像 (具有三个通道)
|
||
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||
|
|
else:
|
||
|
|
gray = image
|
||
|
|
|
||
|
|
# 噪声水平估计(简单地使用标准差作为代理)
|
||
|
|
noise_level = np.std(gray) # 在灰度图像上评估噪声水平
|
||
|
|
if noise_level > NOISE_THRESHOLD:
|
||
|
|
if len(image.shape) == 3: # 如果是彩色图像
|
||
|
|
processed_img = cv2.fastNlMeansDenoisingColored(image)
|
||
|
|
else: # 如果是灰度图像
|
||
|
|
processed_img = cv2.fastNlMeansDenoising(image)
|
||
|
|
else:
|
||
|
|
processed_img = image
|
||
|
|
|
||
|
|
return processed_img
|