Exemple #1
0
def main(file_name="200px-Lenna.jpg"):
    root_path = pu.get_root_path()
    # file_name = "trees.png"
    file_path = pu.path_join(root_path, pu.INPUT_PATH, file_name)

    color_lena = iu.read_img(file_path, iu.READ_COLOR)

    r_data = 0b11111000001111100000
    g_data = 0b11111111110000000000
    b_data = 0b10101010101010101010

    length = [len(bin(r_data)), len(bin(g_data)), len(bin(b_data))]
    tplt = "{0:<" + str(length[0]) + "}\t{1:<" + str(length[1]) + "}\t{2:<" + str(length[2]) + "}"

    print(tplt.format("r_data(insert):", "g_data(insert):", "b_data(insert):"))
    print(tplt.format(bin(r_data), bin(g_data), bin(b_data)))

    r = ReversibleDataHidingRGBA()
    r.encrypt(color_lena, r_data, g_data, b_data)
    # iu.print_img(r.encrypted)

    # encrypted = iu.read_img(pu.path_join(root_path, "static", "integral_rgba", "image.png"), cv2.IMREAD_UNCHANGED)
    r.decrypt(r.encrypted)
    # iu.print_img(r.decrypted)
    print(tplt.format("r_data(extract):", "g_data(extract):", "b_data(extract):"))
    print(tplt.format(r.r_data, r.g_data, r.b_data))
    print(f"max length of hide data: {r.max_length}")
    print(f"image size: {color_lena.shape}")
    print("file save in /static/integral_rgba!")
Exemple #2
0
 def encrypt(self, img: np.ndarray, r_data=0, g_data=0, b_data=0, pth=__INTEGRAL_PATH):
     b_ = img[:, :, 0]
     g_ = img[:, :, 1]
     r_ = img[:, :, 2]
     b_thread = RDHEncryptedThread(self.b, b_, b_data, pu.path_join(pth, "b"), "b")
     g_thread = RDHEncryptedThread(self.g, g_, g_data, pu.path_join(pth, "g"), "g")
     r_thread = RDHEncryptedThread(self.r, r_, r_data, pu.path_join(pth, "r"), "r")
     b_thread.start()
     g_thread.start()
     r_thread.start()
     b_thread.join()
     g_thread.join()
     r_thread.join()
     # self.b.encrypt(b_, b_data, pth=pth + "/b")
     # self.g.encrypt(g_, g_data, pth=pth + "/g")
     # self.r.encrypt(r_, r_data, pth=pth + "/r")
     self.LM = self.b.LM.astype(np.uint8) * 4 + \
               self.g.LM.astype(np.uint8) * 2 + \
               self.r.LM.astype(np.uint8) * 1
     print("encrypting LM ...")
     self.encrypted_LM = eu.encrypt(self.LM, eu.SECRET_KEY, 256)
     print("LM encrypted!")
     self.encrypted = np.zeros((img.shape[0], img.shape[1], 4), np.uint8)
     self.encrypted[:, :, 0] = self.r.encrypted
     self.encrypted[:, :, 1] = self.g.encrypted
     self.encrypted[:, :, 2] = self.b.encrypted
     self.encrypted[:, :, 3] = self.encrypted_LM
     iu.save_img(self.encrypted, pu.path_join(pu.get_root_path(), pth, "image.png"))
Exemple #3
0
 def decrypt(self, img: np.ndarray, pth="static/integral_rgba"):
     r_ = img[:, :, 0]
     g_ = img[:, :, 1]
     b_ = img[:, :, 2]
     a_ = img[:, :, 3]
     print("decrypting LM ...")
     a_ = eu.decrypt(a_, eu.SECRET_KEY, 256).astype(np.uint8)
     print("LM decrypted!")
     r_LM = a_ % 2
     a_ >>= 1
     g_LM = a_ % 2
     a_ >>= 1
     b_LM = a_ % 2
     r_thread = RDHDecryptedThread(self.r, r_, r_LM, pu.path_join(pth, "r"), "r")
     g_thread = RDHDecryptedThread(self.g, g_, g_LM, pu.path_join(pth, "g"), "g")
     b_thread = RDHDecryptedThread(self.b, b_, b_LM, pu.path_join(pth, "b"), "b")
     b_thread.start()
     g_thread.start()
     r_thread.start()
     b_thread.join()
     g_thread.join()
     r_thread.join()
     # self.r.decrypt(r_, r_LM, False, pth=pth + "/r")
     # self.g.decrypt(g_, g_LM, False, pth=pth + "/g")
     # self.b.decrypt(b_, b_LM, False, pth=pth + "/b")
     r, g, b = self.r.decrypted, self.g.decrypted, self.b.decrypted
     self.decrypted = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
     self.decrypted[:, :, 0] = b
     self.decrypted[:, :, 1] = g
     self.decrypted[:, :, 2] = r
     iu.save_img(self.decrypted, pu.path_join(pu.get_root_path(), pth, "res.png"))
Exemple #4
0
def main(file_name):
    a = ReversibleDataHiding()
    root_path = pu.get_root_path()
    file_path = pu.path_join(root_path, pu.INPUT_PATH, file_name)

    gray_lena = iu.read_img(file_path, iu.READ_GRAY)
    # changed_gray_lena = gray_lena.copy()
    # changed_gray_lena[100, 100] = 0

    # a.encrypt(gray_lena, 0b11111000001111100000)
    # a.decrypt()

    # print("1")
    print("encrypting!")
    print("insert data: 0b11111000001111100000")
    a.encrypt(gray_lena, 0b11111000001111100000)
    # print("2")
    print("decrypting!")
    a.decrypt()
    # print("3")
    # a.encrypt(changed_gray_lena, 0b11111000001111100000, pth="static/test/changed")
    # print("4")
    # a.decrypt(pth="static/test/changed")
    print("extract data: ", end="")
    print(a.r.data)
    print(f"hide data max length: {a.e.max_length()}")
    print(f"image size: {gray_lena.shape}")
    # print(a.r.data)
    print("file save in /static/integral!")
Exemple #5
0
 def __init__(self):
     self.r = None
     self.data_length = 0
     self.LM = None
     self.encrypted = None
     self.e = None
     self.decrypted = None
     self.root_path = pu.get_root_path()
Exemple #6
0
 def save(img, name):
     """
     :param pth: 存储的路径
     :return:
     """
     root_path = pu.get_root_path()
     out = pu.path_join(root_path, pu.REC_PATH)
     pth = pu.path_join(out, name)
     iu.save_img(img, pth)
Exemple #7
0
def __test(file):
    e = Encryptor(file, Encryptor.predict_method1)
    e.decomposition()
    e.predict()
    e.recomposition()
    print(e.max_length())
    e.data_hider(0b11000100000110001111111011100001000001100011111110111000010000011000111111101110000100000110001111111011)
    e.encryption()
    root_path = pu.get_root_path()
    out = pu.path_join(root_path, pu.OUTPUT_PATH)
    e.save(out)
    # lm = dec.decryptioner(e.encrypted_LM, dec.secretKey, 256)
    # diff = e.LM.astype(np.int) - lm.astype(np.int)
    # i = dec.decryptioner(e.encrypted_I, dec.secretKey, 256)
    # diff_I = i - e.res_img
    # print(np.sum(abs(diff)))
    # print(np.sum(abs(diff_I)))
    # iu.print_imgs(e.ans.astype(np.uint8), e.encrypted_LM.astype(np.uint8))
    pass
Exemple #8
0
    g1 = img1[:, :, 1]
    r1 = img1[:, :, 2]
    b2 = img2[:, :, 0]
    g2 = img2[:, :, 1]
    r2 = img2[:, :, 2]
    r = np.array([0, 0, 0])
    g = np.array([0, 0, 0])
    b = np.array([0, 0, 0])
    r[0] = ssim(r1, r2)
    r[1] = mse(r1, r2)
    r[2] = psnr(r1, r2)
    g[0] = ssim(g1, g2)
    g[1] = mse(g1, g2)
    g[2] = psnr(g1, g2)
    b[0] = ssim(b1, b2)
    b[1] = mse(b1, b2)
    b[2] = psnr(b1, b2)
    return np.mean(r + g + b)


if __name__ == "__main__":
    root_path = pu.get_root_path()
    img1 = iu.read_img(pu.path_join(root_path, "static/input/timg.jpeg"),
                       iu.READ_GRAY)
    img2 = iu.read_img(
        pu.path_join(root_path, "static/test/unchanged/res.png"), iu.READ_GRAY)

    print(ssim(img1, img2))
    print(mse(img1, img2))
    print(psnr(img1, img2))