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

Function gen_kernel

ldm/modules/image_degradation/bsrgan_light.py:144–183  ·  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

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

Callers

nothing calls this directly

Calls 1

meshgridMethod · 0.80

Tested by

no test coverage detected