def embed_bit(block, bit): patch = block.copy() coefs = dct(dct(patch, axis=0, norm='ortho'), axis=1, norm='ortho') while not valid_coeffs(coefs, bit, P) or (bit != retrieve_bit(patch)): coefs = change_coeffs(coefs, bit) patch = double_to_byte( idct(idct(coefs, axis=0, norm='ortho'), axis=1, norm='ortho')) return patch
def __init__(self, n_filters, filter_size): ''' n_filters (int): Number of learnable filters in a group filter_size (int): Size of each filter in the group ''' super(WienerFilter, self).__init__() self.alpha = torch.FloatTensor([0.0]) self.alpha = self.alpha.unsqueeze(-1) self.alpha = nn.Parameter(self.alpha) self.g_ker = torch.Tensor(n_filters, 1, filter_size, filter_size) self.g_ker = nn.Parameter(self.g_ker) utils.dct(self.g_ker)
def extract_watermark(host, pxl_perm_mat): host = utils.pad(host) host = cv.cvtColor(host, cv.COLOR_BGR2YCrCb) luma = host[:, :, 0] luma = luma.astype(np.float) luma = utils.dct(luma) watermark = extract_freq_domain(luma) pxl_perm_mat = utils.inv_perm_mat(pxl_perm_mat) watermark = utils.permute_pxl(watermark, pxl_perm_mat) return watermark
def embed_watermark(host, watermark, robustness): old_shape = host.shape[0: 2] host = utils.pad(host) height, width = host.shape[0: 2] host = cv.cvtColor(host, cv.COLOR_BGR2YCrCb) luma = host[:, :, 0] luma = luma.astype(np.float) watermark = cv.resize(watermark, (width >> 1, height >> 2)) watermark = cv.adaptiveThreshold(watermark, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2) watermark, pxl_perm_mat = utils.pseudo_random_permute(watermark) luma = utils.dct(luma) luma = embed_freq_domain(luma, watermark, robustness) luma = utils.idct(luma) host[:, :, 0] = np.clip(luma, 0, 255) host = cv.cvtColor(host, cv.COLOR_YCrCb2BGR) host = utils.cut(host, old_shape) return host, pxl_perm_mat
def bm3d(im, sigma, N1_ht=8, N2_ht=16, Nstep_ht=6, N_S_ht=12, lambda2d=0, lambda3d=2.7, tau_match_ht=2500, N1_wie=8, N2_wie=16, Nstep_wie=5, N_S_wie=12, tau_match_wie=400): blocks_noisy = block_dct(im, bsize=N1_ht) bh, bw, _, _ = blocks_noisy.shape thr_im_dct = utils.dct(im) thr_im_dct = utils.hard_thr(thr_im_dct, lambda2d * sigma) thr_im = utils.dct(thr_im_dct, inverse=True) blocks_thr = block_dct(thr_im, bsize=N1_ht) step1_agg = np.zeros_like(im) step1_weights = np.zeros_like(im) _ys = set() #debug for y in range(0, bh, Nstep_ht): for x in range(0, bw, Nstep_ht): ref = blocks_thr[y, x] Sy_min = max(0, y - N_S_ht) Sy_max = min(bh, y + N_S_ht + 1) Sx_min = max(0, x - N_S_ht) Sx_max = min(bw, x + N_S_ht + 1) neighborhood = blocks_thr[Sy_min:Sy_max, Sx_min:Sx_max] idx_x, idx_y = group(ref, neighborhood, tau_match_ht, N2_ht) grp = blocks_noisy[Sy_min:Sy_max, Sx_min:Sx_max][idx_y, idx_x] grp = utils.dct(grp, axes=0) grp = utils.hard_thr(grp, lambda3d * sigma) weights = np.square(sigma) * np.sum(grp > 0) if weights == 0: weights = 1. weights = 1. / weights grp = utils.dct(grp, inverse=True) grp *= weights[np.newaxis, ...] idx_x = idx_x + Sx_min idx_y = idx_y + Sy_min for i in range(len(grp)): _ys.add((idx_y[i], idx_x[i])) #debug step1_agg[idx_y[i]:idx_y[i] + N1_ht, idx_x[i]:idx_x[i] + N1_ht] += grp[i] step1_weights[idx_y[i]:idx_y[i] + N1_ht, idx_x[i]:idx_x[i] + N1_ht] += weights print(len(_ys)) #debug basic_im = step1_agg / (step1_weights + 10e-8) blocks_noisy = block_dct(im, bsize=N1_wie) bh, bw, _, _ = blocks_noisy.shape blocks_basic = block_dct(basic_im, bsize=N1_wie) step2_agg = np.zeros_like(im) step2_weights = np.zeros_like(im) for y in range(0, bh, Nstep_wie): for x in range(0, bw, Nstep_wie): ref = blocks_basic[y, x] Sy_min = max(0, y - N_S_wie) Sy_max = min(bh, y + N_S_wie + 1) Sx_min = max(0, x - N_S_wie) Sx_max = min(bw, x + N_S_wie + 1) neighborhood = blocks_basic[Sy_min:Sy_max, Sx_min:Sx_max] idx_x, idx_y = group(ref, neighborhood, tau_match_wie, N2_wie) mask = np.zeros(neighborhood.shape[:2], dtype=np.bool) mask[idx_y, idx_x] = True grp_basic = neighborhood[mask] n_matched = len(grp_basic) grp_basic = grp_basic[:min(n_matched, N2_wie)] grp_basic = utils.dct(grp_basic, axes=0) W_wie = np.square(grp_basic) / (np.square(grp_basic) + np.square(sigma)) grp_noisy = blocks_noisy[Sy_min:Sy_max, Sx_min:Sx_max][mask] grp_noisy = grp_noisy[:min(n_matched, N2_wie)] grp_noisy = utils.dct(grp_noisy, axes=0) grp_noisy *= W_wie weights = 1. / (np.square(sigma) * np.sum(np.square(W_wie)) + 10e-8) grp_noisy = utils.dct(grp_noisy, inverse=True) grp_noisy *= weights xv = np.arange(Sx_min, Sx_max) yv = np.arange(Sy_min, Sy_max) xv, yv = np.meshgrid(xv, yv) xv = xv[mask] xv = xv[:min(n_matched, N2_wie)] yv = yv[mask] xv = xv[:min(n_matched, N2_wie)] for i in range(len(grp_noisy)): step2_agg[yv[i]:yv[i] + N1_wie, xv[i]:xv[i] + N1_wie] += grp_noisy[i] step2_weights[yv[i]:yv[i] + N1_wie, xv[i]:xv[i] + N1_wie] += weights final_im = step2_agg / (step2_weights + 10e-8) return basic_im, final_im
def block_dct(im, bsize, inverse=False): # Compute dct for all overlapping blocks in an image blocks = all_blocks(im, bsize) blocks = utils.dct(blocks, axes=[2, 3], inverse=inverse) return blocks
inSequence = getSequence() print("\nThe input sequence is: {}".format(inSequence)) if args.transform == 'dft': task = 'Discrete Fourier Transform' outSequence = np.round(dft(inSequence), args.roundDigit) elif args.transform == 'ditfft': task = 'Fast Fourier Transform (Decimation in Time)' outSequence = np.round(ditfft(inSequence), args.roundDigit) elif args.transform == 'diffft': task = 'Fast Fourier Transform (Decimation in Frequency)' outSequence = np.round(reArrange(diffft(inSequence)), args.roundDigit) elif args.transform == 'dct': task = 'Discrete Cosine Transform' outSequence = np.round(dct(inSequence), args.roundDigit) elif args.transform == 'dst': task = 'Discrete Sine Transform' outSequence = np.round(dst(inSequence), args.roundDigit) elif args.transform == 'wht': task = 'Walsh-Hadamard Transform' outSequence = np.round(wht(inSequence), args.roundDigit) elif args.transform == 'slant': task = 'Slant Transform' outSequence = slant(inSequence) elif args.transform == 'haar': task = 'Haar Transform' outSequence = haar(inSequence) else: task = 'Karhunen-Loeve Transform' outSequence = klt(inSequence)