def test_decode_img(img, wm): rs = RSCoder(255, 223) msg_str = img_to_str(wm) msg = msg_to_bin(msg_str) LL, LH, HL, HH = dwt(img) HH = np.ravel(HH) bin_msg = "" for i in range(0, len(msg) / 2): hh_bin = bin(int(round(HH[i]))) hh_bin = hh_bin[hh_bin.index('b') + 1:] bit_pairs = hh_bin[len(hh_bin) - 2:] if len(hh_bin) >= 2 else '0' + hh_bin bin_msg += bit_pairs msg_str = "" for i in range(0, len(bin_msg), 8): digit = bin_msg[i:i + 8] msg_str += unichr(int(digit, 2)) decoded = "" #11475 for i in range(0, len(msg_str), 255): decoded += rs.decode(msg_str[i:i + 255]) dc_list = list(decoded) print(len(dc_list)) for i in range(0, len(dc_list)): dc_list[i] = ord(dc_list[i]) result = dc_list[:wm.size] result = np.reshape(result, wm.shape) return result
def msg_to_bin(msg): rs = RSCoder(255, 223) final_msg = "" for i in range(0, len(msg), 223): ecc_msg = rs.encode(msg[i:i + 223]) ecc_msg = map(to_bin, list(ecc_msg)) final_msg += ''.join(ecc_msg) return final_msg
def test_encode_img(img, wm): h, w = img.shape LL, LH, HL, HH = dwt(img) msg_str = img_to_str(wm) print len(msg_str) msg = msg_to_bin(msg_str) HH = np.ravel(HH) rs = RSCoder(255, 223) #encode image for i in range(0, len(msg), 2): bits = msg[i:i + 2] _hh = bin(int(round(HH[i]))) _hh_index = _hh.index('b') + 1 hh_bin = _hh[:_hh_index] + _hh[_hh_index:].zfill(3) HH[i] = int(hh_bin[:len(hh_bin) - len(bits)] + bits, 2) HH = np.reshape(HH, LL.shape) LH = np.reshape(LH, LL.shape) HL = np.reshape(HL, LL.shape) res = idwt(LL, LH, HL, HH) res = np.around(res) return res
def getprotected(self, data): c = RSCoder(255, 127) return c.decode(data)
def protect(self, data): c = RSCoder(255, 127) return c.encode(data)
#!/usr/bin/env python import os import cv2 import numpy as np import pywt from rs import RSCoder N = 255 K = 223 rs = RSCoder(N, K) def dwt(img): LL, (LH, HL, HH) = pywt.dwt2(data=img, wavelet='haar') return LL, LH, HL, HH def idwt(LL, LH, HL, HH): coeffs = (LL, (LH, HL, HH)) return pywt.idwt2(coeffs, 'haar') ###################################### def round_multiply(HH): HH[np.abs(HH) < 1.0e-10] = 0 return HH * 10.0 def i_round_multiply(HH): return HH / 10.0