MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / gen_kernel

Function gen_kernel

ldm/modules/image_degradation/bsrgan.py:145–184  ·  view source on GitHub ↗

# modified version of https://github.com/assafshocher/BlindSR_dataset_generator # Kai Zhang # min_var = 0.175 * sf # variance of the gaussian kernel will be sampled between min_var and max_var # max_var = 2.5 * sf

(k_size=np.array([15, 15]), scale_factor=np.array([4, 4]), min_var=0.6, max_var=10., noise_level=0)

Source from the content-addressed store, hash-verified

143
144
145def gen_kernel(k_size=np.array([15, 15]), scale_factor=np.array([4, 4]), min_var=0.6, max_var=10., noise_level=0):
146 """"
147 # modified version of https://github.com/assafshocher/BlindSR_dataset_generator
148 # Kai Zhang
149 # min_var = 0.175 * sf # variance of the gaussian kernel will be sampled between min_var and max_var
150 # max_var = 2.5 * sf
151 """
152 # Set random eigen-vals (lambdas) and angle (theta) for COV matrix
153 lambda_1 = min_var + np.random.rand() * (max_var - min_var)
154 lambda_2 = min_var + np.random.rand() * (max_var - min_var)
155 theta = np.random.rand() * np.pi # random theta
156 noise = -noise_level + np.random.rand(*k_size) * noise_level * 2
157
158 # Set COV matrix using Lambdas and Theta
159 LAMBDA = np.diag([lambda_1, lambda_2])
160 Q = np.array([[np.cos(theta), -np.sin(theta)],
161 [np.sin(theta), np.cos(theta)]])
162 SIGMA = Q @ LAMBDA @ Q.T
163 INV_SIGMA = np.linalg.inv(SIGMA)[None, None, :, :]
164
165 # Set expectation position (shifting kernel for aligned image)
166 MU = k_size // 2 - 0.5 * (scale_factor - 1) # - 0.5 * (scale_factor - k_size % 2)
167 MU = MU[None, None, :, None]
168
169 # Create meshgrid for Gaussian
170 [X, Y] = np.meshgrid(range(k_size[0]), range(k_size[1]))
171 Z = np.stack([X, Y], 2)[:, :, :, None]
172
173 # Calcualte Gaussian for every pixel of the kernel
174 ZZ = Z - MU
175 ZZ_t = ZZ.transpose(0, 1, 3, 2)
176 raw_kernel = np.exp(-0.5 * np.squeeze(ZZ_t @ INV_SIGMA @ ZZ)) * (1 + noise)
177
178 # shift the kernel so it will be centered
179 # raw_kernel_centered = kernel_shift(raw_kernel, scale_factor)
180
181 # Normalize the kernel and return
182 # kernel = raw_kernel_centered / np.sum(raw_kernel_centered)
183 kernel = raw_kernel / np.sum(raw_kernel)
184 return kernel
185
186
187def fspecial_gaussian(hsize, sigma):

Callers

nothing calls this directly

Calls 1

meshgridMethod · 0.80

Tested by

no test coverage detected