def main(X, ri): global _b_vector, _A_matrix, _image_dims, _ri_vector # read image in grayscale, then downscale it ny, nx = X.shape # take random samples of image, store them in a vector b b = X.T.flat[ri].astype(float) # important: cast to 64 bit # This method evaluates the objective function sum((Ax-b).^2) and its # gradient without ever actually generating A (which can be massive). # Our ability to do this stems from our knowledge that Ax is just the # sampled idct2 of the spectral image (x in matrix form). # save image dims, sampling vector, and b vector and to global vars _image_dims = (ny, nx) _ri_vector = ri _b_vector = np.expand_dims(b, axis=1) # perform the L1 minimization in memory Xat2 = owlqn(nx * ny, evaluate, progress, ORTHANTWISE_C) # transform the output back into the spatial domain Xat = Xat2.reshape(nx, ny).T # stack columns Xa = idct2(Xat) # create images of mask (for visualization) mask = np.zeros(X.shape) mask.T.flat[ri] = 255 Xm = 255 * np.ones(X.shape) Xm.T.flat[ri] = X.T.flat[ri] # display the result return Xm, Xa
def mask_and_recreate_image(s, img, special_mask: np.s_ = None): nx, ny = img.shape # create random sampling index vector k = round(nx * ny * s) if special_mask is None: ri = np.random.choice(nx * ny, k, replace=False) # random sample of indices else: ri = np.arange(nx * ny).reshape((nx, ny)) ri[special_mask] = -1 ri.flatten() ri = ri[ri != -1] # create images of mask (for visualization) Xm = 255 * np.ones(img.shape) Xm.T.flat[ri] = img.T.flat[ri] mask = Xm # take random samples of image, store them in a vector b b = img.T.flat[ri].astype(float) # perform the L1 minimization in memory evaluator = Evaluator(nx, ny, ri, b) evaluator = Evaluator(nx, ny, ri, b) Xat2 = owlqn(nx * ny, evaluator.evaluate, None, 5) # transform the output back into the spatial domain Xat = Xat2.reshape(nx, ny).T # stack columns Xa = idct2(Xat) recovered_image = Xa.astype('uint8') diff = np.square(np.subtract(recovered_image, img)).mean() return recovered_image, mask, diff
def recover_1_channel(Xm, b, ri): # Recover the image on a single channel # Xm: nx*ny matrix, sampled matrix in a single color channel # b: a vector that contains that sampled part of X # ri: a vector that indicates the sampled locations global _b_vector, _ri_vector _b_vector = b _ri_vector = ri ny, nx = Xm.shape Z = np.zeros(Xm.shape, dtype='uint8') # The recovered image # perform the L1 minimization in memory coef = 8 # coefficient before the L1 norm Xat2 = owlqn(nx * ny, evaluate, progress, coef) # transform the output back into the spatial domain Xat = Xat2.reshape(nx, ny).T # stack columns Xa = idct2(Xat) Z = Xa.astype('uint8') return Z
def owl_qn_cs( image, n_x, n_y, indexes, measurements ): global nx, ny, ri, b nx = n_x ny = n_y ri = indexes b = measurements # Get the dimensions of the image. ny, nx = image.shape # Take random samples of the image b = image.T.flat[ ri ].astype( float ) # Perform the L1 minimization in memory. # Result is in the frequency domain. Xat2 = owlqn( nx * ny, evaluate, progress, 5 ) print( '' ) # Transform the output back into the spatial domain. Xat = Xat2.reshape( nx, ny ).T # Stack columns. Xa = idct2( Xat ).astype( 'uint8' ) # Return the reconstructed signal. return Xa
ri = np.random.choice(nx * ny, k, replace=False) # random sample of indices # for each color channel for j in range(nchan): # extract channel X = Xorig[:, :, j].squeeze() # create images of mask (for visualization) Xm = 255 * np.ones(X.shape) Xm.T.flat[ri] = X.T.flat[ri] masks[i][:, :, j] = Xm # take random samples of image, store them in a vector b b = X.T.flat[ri].astype(float) # perform the L1 minimization in memory Xat2 = owlqn(nx * ny, evaluate, None, 5) # transform the output back into the spatial domain Xat = Xat2.reshape(nx, ny).T # stack columns Xa = idct2(Xat) Z[i][:, :, j] = Xa.astype('uint8') plt.imshow(Xorig) plt.show() plt.imshow(Xa, cmap='gray') plt.show() plt.imshow(Xm, cmap='gray') plt.show()
def reconstruction(self, samples, image_size, method='', mask=''): m = Masks() self.image_size = image_size self.samples = samples if mask == 'hadamard': masks = m.generate_hadamard(len(samples), image_size) else: random_masks = m.generate_random(len(samples), image_size) masks = [] for i in random_masks: masks.append(i.T.flatten()) if method == 'direct_inverse': matrix_vector = [] np.random.seed(1) for _ in range(0, len(samples)): random_matrix = (np.random.rand( self.image_size, self.image_size) < 0.5) * np.float32(1) matrix_vector.append(random_matrix.flatten()) Tmatrix_vector = np.linalg.pinv(np.matrix(matrix_vector)) res = np.reshape(np.matmul(Tmatrix_vector, np.matrix(samples).T), (64, 64)) if method == 'hadamard': if mask == '' or mask == 'hadamard': masks = m.generate_hadamard(len(samples), image_size) else: masks = random_masks res = np.zeros([image_size, image_size]) for i in range(0, len(samples)): res += masks[i] * samples[i] if method == 'fourier': random_masks = m.generate_random(len(samples), image_size) if mask == 'hadamard': random_masks = m.generate_hadamard(len(samples), image_size) random_masks = (np.asarray(random_masks) > 0) * 1.0 random_masks_formated = [] for i in random_masks: random_masks_formated.append(i.T.flatten()) A = np.kron( spfft.idct(np.identity(image_size[0]), norm='ortho', axis=0), spfft.idct(np.identity(image_size[1]), norm='ortho', axis=0)) A = np.dot(random_masks_formated, A) vx = cvx.Variable(image_size[0] * image_size[1]) objective = cvx.Minimize(cvx.norm(vx, 1)) constraints = [A * vx == samples] prob = cvx.Problem(objective, constraints) result = prob.solve(verbose=False) Xat2 = np.array(vx.value).squeeze() Xat = Xat2.reshape(image_size, image_size).T res = self.__idct2(Xat) if method == 'fourier_optim': random_masks = m.generate_random(len(samples), image_size) if mask == 'hadamard': random_masks = m.generate_hadamard(len(samples), image_size) random_masks = (np.asarray(random_masks) > 0) * 1.0 random_masks_formated = [] for i in random_masks: random_masks_formated.append(i.T.flatten()) self.random_masks = random_masks_formated Xat2 = owlqn(image_size * image_size, self.__evaluate, self.__progress, 500) Xat = Xat2.reshape(image_size, image_size).T # stack columns res = self.__idct2(Xat) return res
masked = mask * Xorig intensity = np.sum(masked) mask_vec.append(mask.T.flatten()) intensity_vec.append(intensity) #mask_vec = np.expand_dims(mask_vec, axis=1) mask_vec = np.asarray(mask_vec) intensity_vec = np.asarray(intensity_vec, dtype="float64") # perform the L1 minimization in memory tic = time.clock() plt.gray() plt.ion() fig = plt.figure() im = plt.imshow(Xorig) plt.show() Xat2 = owlqn(nx * ny, evaluate, progress, 10000) tac = time.clock() print(tac - tic) # transform the output back into the spatial domain Xat = Xat2.reshape(nx, ny).T # stack columns Xa = idct2(Xat) Z = Xa.astype('uint8') fig = plt.figure() fig.add_subplot(1, 2, 1) plt.gray() plt.imshow(Xorig) fig.add_subplot(1, 2, 2) plt.imshow(Z)