def K0_transpose(x): # transpose gradient op x1 = un_flatten_by_channel(x[:, 0], super_shape) x2 = un_flatten_by_channel(x[:, 1], super_shape) GradX = np.array([[-1, 1, 0]]).T GradY = GradX.T dX = cv2.filter2D(x1, -1, GradX, borderType=cv2.BORDER_REPLICATE) dY = cv2.filter2D(x2, -1, GradY, borderType=cv2.BORDER_REPLICATE) return flatten_by_channel(dX + dY)
def grad_transpose(self, v): #return v[:,0] + v[:,1] GradX = np.array([[-1, 1, 0]]) GradY = GradX.T vx = un_flatten_by_channel(v[:, 0], self.shape) vy = un_flatten_by_channel(v[:, 1], self.shape) dX = cv2.filter2D(vx, -1, GradX.T, borderType=cv2.BORDER_REPLICATE) dY = cv2.filter2D(vy, -1, GradY.T, borderType=cv2.BORDER_REPLICATE) return flatten_by_channel(dX) + flatten_by_channel(dY)
def NLMProx(v): # tonemap v_min = v.min() v_max = x.max() if ((v_min - v_max) == 0) and v_max == 0: return v * 0 v = (v - v_min) / (v_max - v_min) v = v * 255 v = v.astype(np.uint8) v = un_flatten_by_channel(v, super_shape) #cv2.imshow("Before NLM", v) v = cv2.fastNlMeansDenoisingColored(v) #cv2.imshow("After NLM", v) #cv2.waitKey(0) v = flatten_by_channel(v).astype(float) / 255.0 v = v * (v_max - v_min) v = v + v_min return v
def K0(x): #gradient x = un_flatten_by_channel(x, super_shape) GradX = np.array([[-1, 1, 0]]) GradY = GradX.T dX = cv2.filter2D(x, -1, GradX, borderType=cv2.BORDER_REPLICATE) dY = cv2.filter2D(x, -1, GradY, borderType=cv2.BORDER_REPLICATE) dX = flatten_by_channel(dX)[:, np.newaxis] dY = flatten_by_channel(dY)[:, np.newaxis] return np.concatenate((dX, dY), axis=1)
def totalVariation(self, x, y): x = un_flatten_by_channel(x, self.shape) dX, dY = self.grad(x) dX = flatten_by_channel(dX) dY = flatten_by_channel(dY) D = np.concatenate((dX, dY)) v = y + (self.gamma*flatten_by_channel(D)) v_orig = v v = v/self.gamma v = self.L1_norm_prox(v, 1/self.gamma) return v_orig - self.gamma*v
def NLM(self, x, y): v = y + (self.gamma * x) v_orig = v.copy() v = v / self.gamma v = un_flatten_by_channel(v, self.shape) v_max = v.max() v_min = v.min() v = (v - v_min) * 255 / (v_max - v_min) prox = flatten_by_channel( cv2.fastNlMeansDenoisingColored(v.astype( np.uint8))).astype(float) / 255 prox = prox * (v_max - v_min) prox = prox + v_min return v_orig - (self.gamma * prox)
def forward(self, z, init_guess, iters): self.shape = z.shape x_bar = x = init_guess # For debugging answer = flatten_by_channel(z) z = self.A @ flatten_by_channel(z) y = 0 print("Starting iterations...") for _ in range(iters): y = self.penalty(x_bar, y) x_new, _ = self.data_fidelity(x, y, z) x_bar = self.extrapolation(x, x_new) x = x_new out = (x - x.min()) * 255 / (x.max() - x.min()) cv2.imshow("intermediate", un_flatten_by_channel(out.astype(np.uint8), self.shape)) cv2.waitKey(100) print((((out / 255.0)**2 + (answer**2))**0.5).sum()) return x
def extrapolation(x_new, x_old): return x_new + (theta * (x_new - x_old)) initial = None for _ in range(iters): y = penalty_op() x_old = x x, _ = data_fidelity_op() x_bar = extrapolation(x, x_old) print(x.min()) print(x.max()) print() reconstructed_im = un_flatten_by_channel(x, super_shape) reconstructed_im = (reconstructed_im - reconstructed_im.min()) / ( reconstructed_im.max() - reconstructed_im.min()) reconstructed_im = reconstructed_im * 255 reconstructed_im[reconstructed_im < 0] = 0 reconstructed_im[reconstructed_im > 255] = 254 reconstructed_im = reconstructed_im.astype(np.uint8) if (initial is None): initial = reconstructed_im.copy() cv2.imshow("x", reconstructed_im) cv2.waitKey(10) print("Done") cv2.imshow("x0", initial)
def extrapolation(x_new, x_old): return x_new + (theta * (x_new - x_old)) initial = None for _ in range(iters): y = penalty_op() x_old = x x, _ = data_fidelity_op() x_bar = extrapolation(x, x_old) print(x.min()) print(x.max()) print() reconstructed_im = un_flatten_by_channel(x, im.shape) reconstructed_im = (reconstructed_im - reconstructed_im.min()) / ( reconstructed_im.max() - reconstructed_im.min()) reconstructed_im = reconstructed_im * 255 reconstructed_im[reconstructed_im < 0] = 0 reconstructed_im[reconstructed_im > 255] = 254 reconstructed_im = reconstructed_im.astype(np.uint8) if (initial is None): initial = reconstructed_im.copy() cv2.imshow("x", reconstructed_im) cv2.waitKey(10) print("Done") cv2.imshow("x0", initial)