def embed_specific(self, image, image_file, watermark=None): i_width = image.shape[0] i_height = image.shape[1] wm = watermark wm_width = wm.shape[0] wm_height = wm.shape[1] if not (wm_width == i_width / 2 and wm_height == i_height / 2): raise WmError("Watermark size must be half of image size. For now.") ir, ig, ib = self.split_image(image) img = self.split_image(wm) dcts_wm = self.rgb_to_dct(*img) dwts_i = self.rgb_to_dwt(ir, ig, ib) for color in range(3): dct = dcts_wm[color] hh = TwoDimensionalDCT.forward(dwts_i[color][1][2]) for j in range(len(dct)): for k in range(len(dct[0])): hh[j][k] += dct[j][k] * self.alpha ihh = TwoDimensionalDCT.inverse(hh) hh = dwts_i[color][1][2] for j in range(len(dct)): for k in range(len(dct[0])): hh[j][k] = ihh[j][k] rgb = self.dwt_to_rgb(*dwts_i) img = self.join_image(*rgb) return img
def embed_specific(self, image, image_file, watermark=None): # Compute DCT f_dct = TwoDimensionalDCT.forward(image) # Sort DCT size_dct = int(f_dct.__len__()*self.watermark_size_percentage) sorted_dct_indexes = f_dct.ravel().argsort()[-size_dct:] sorted_dct_indexes = (numpy.unravel_index(indx, f_dct.shape) for indx in sorted_dct_indexes) sorted_unraveled = [(f_dct[indx], indx) for indx in sorted_dct_indexes] nbits = numpy.random.normal(self.mu, self.sigma, size_dct) # Construct the Watermark for i in range(len(nbits)): f_dct[sorted_unraveled[i][1]] += self.alpha * nbits[i] self.export_image(sorted_unraveled, image_file, nbits) inverse = TwoDimensionalDCT.inverse(f_dct) return inverse
def test_dct_solid_gray(self): dct = TwoDimensionalDCT.forward(self.solid_gray) for channel in range(3): self.assertTrue(self.closeEnough(dct[0][0][channel], 800)) for channel in range(3): for i in range(len(dct)): for j in range(len(dct[i])): if i != 0 and j != 0: self.assertTrue(self.closeEnough(dct[i][j][channel], 0))
def extract_specific(self, image, watermark): f_dct = TwoDimensionalDCT.forward(image) w = self.load_watermark(watermark) xi = [] xo = [] for entry in w: xo.append( entry[self.INSERTED_WATERMARK_VALUE_KEY] ) xi.append( (f_dct[tuple(entry[self.INDEX_KEY])] - entry[self.ORIGINAL_VALUE_KEY]) /\ (self.alpha)) return None, Metrics.gamma(xi, xo)
def extract_specific(self, image, watermark): wm = self.open_image(watermark) RED, GREEN, BLUE = (0,1,2) image_rgb = self.split_image(image) wm_rgb = self.split_image(wm) image_dwt = list(self.rgb_to_dwt(*image_rgb)) wm_dwt = list(self.rgb_to_dwt(*wm_rgb)) for color in range(3): dct_image_dwt = TwoDimensionalDCT.forward(image_dwt[color][1][2]) dct_wm_dwt = TwoDimensionalDCT.forward(wm_dwt[color][1][2]) for i in range(len(image_dwt[RED][1][2])): for j in range(len(image_dwt[RED][1][2][0])): dct_image_dwt[i][j] = (dct_wm_dwt[i][j] - dct_image_dwt[i][j])/self.alpha for i in range(len(image_dwt[RED][1][2])): for j in range(len(image_dwt[RED][1][2][0])): image_dwt[color][1][2][i][j] = dct_image_dwt[i][j] return self.join_image(*self.dct_to_rgb(image_dwt[RED][1][2], image_dwt[GREEN][1][2], image_dwt[BLUE][1][2])), 0
def dct_to_rgb(self, cr, cg, cb): return TwoDimensionalDCT.inverse(cr), TwoDimensionalDCT.inverse(cg), TwoDimensionalDCT.inverse(cb)
def rgb_to_dct(self, r, g, b): return TwoDimensionalDCT.forward(r), TwoDimensionalDCT.forward(g), TwoDimensionalDCT.forward(b)
def test_idct_close_values(self): img = TwoDimensionalDCT.inverse(TwoDimensionalDCT.forward(self.close_values)) self._test_inverse(img, self.close_values)
def test_idct_solid_gray(self): img = TwoDimensionalDCT.inverse(TwoDimensionalDCT.forward(self.solid_gray)) self._test_inverse(img, self.solid_gray)
def test_dct_close_values(self): dct = TwoDimensionalDCT.forward(self.close_values) self.assertTrue(self.closeEnough(dct[0][0][0], 407))