codeformer,是如何对数据进行降级处理的?是如何模糊人脸图像的?
答案:https://github.com/sczhou/CodeFormer/blob/8392d0334956108ab53d9439c4b9fc9c4af0d66d/basicsr/data/ffhq_blind_dataset.py#L90
运动模糊:
# motion blur
if self.use_motion_kernel and random.random() < self.motion_kernel_prob:
m_i = random.randint(0,31)
k = self.motion_kernels[f'{m_i:02d}']
img_in = cv2.filter2D(img_in,-1,k)
核大小为41的高斯模糊:
# gaussian blur
kernel = gaussian_kernels.random_mixed_kernels(
self.kernel_list,
self.kernel_prob,
self.blur_kernel_size,
self.blur_sigma,
self.blur_sigma,
[-math.pi, math.pi],
noise_range=None)
img_in = cv2.filter2D(img_in, -1, kernel)
downsample
# downsample
scale = np.random.uniform(self.downsample_range[0], self.downsample_range[1])
img_in = cv2.resize(img_in, (int(self.gt_size // scale), int(self.gt_size // scale)), interpolation=cv2.INTER_LINEAR)
噪声:
# noise
if self.noise_range is not None:
noise_sigma = np.random.uniform(self.noise_range[0] / 255., self.noise_range[1] / 255.)
noise = np.float32(np.random.randn(*(img_in.shape))) * noise_sigma
img_in = img_in + noise
img_in = np.clip(img_in, 0, 1)
压缩:
# jpeg
if self.jpeg_range is not None:
jpeg_p = np.random.uniform(self.jpeg_range[0], self.jpeg_range[1])
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_p]
_, encimg = cv2.imencode('.jpg', img_in * 255., encode_param)
img_in = np.float32(cv2.imdecode(encimg, 1)) / 255.
大概是:
import cv2
# 读取图像
img_path = 'demo.jpg'
img = cv2.imread(img_path)
w, h = img.shape[:2]
img = cv2.resize(img, (w // 2, h // 2))
# 模糊
img = cv2.GaussianBlur(img, (5, 5), 0)
# jpeg压缩
cv2.imwrite('demo2.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 10])
# 读取图像
img = cv2.imread('demo2.jpg')
img = cv2.resize(img, (w, h))
cv2.imwrite('demo2.jpg', img)