"""
@encoding : utf-8
@Author : Wuguo Tang
@School : CSUFT
@E-mail : 1009592703@qq.com
@File : fhhd.py
@CreateTime : 2021/3/25 14:00
"""
import cv2
import math
import numpy as np
def int2bin8(x):
result = "";
for i in range(8):
y = x & (1)
result += str(y)
x = x >> 1
return result[::-1]
def int2bin16(x):
result = "";
for i in range(16):
y = x & (1)
result += str(y)
x = x >> 1
return result
def fhhd(img, x0, g0, j0, encryptionImg):
x = img.shape[0]
y = img.shape[1]
c = img.shape[2]
g0 = int2bin16(g0)
for s in range(x):
for n in range(y):
for z in range(c):
m = int2bin8(img[s][n][z])
ans = ""
for i in range(8):
ri = int(g0[-1])
qi = int(m[i]) ^ ri
xi = 1 - math.sqrt(abs(2 * x0 - 1))
if qi == 0:
xi = 1 - xi;
x0 = xi
t = int(g0[0]) ^ int(g0[12]) ^ int(g0[15])
g0 = str(t) + g0[0:-1]
ci = math.floor(xi * (2 ** j0)) % 2
ans += str(ci)
re = int(ans, 2)
encryptionImg[s][n][z] = re
def fhhdjm(encryptionImg, x0, g0, j0, decryptionImg):
x = encryptionImg.shape[0]
y = encryptionImg.shape[1]
c = encryptionImg.shape[2]
g0 = int2bin16(g0)
for s in range(x):
for n in range(y):
for z in range(c):
cc = int2bin8(img[s][n][z])
ans = ""
for i in range(8):
xi = 1 - math.sqrt(abs(2 * x0 - 1))
x0 = xi
ssi = math.floor(xi * (2 ** j0)) % 2
qi = 1 - (ssi ^ int(cc[i]))
ri = int(g0[-1])
mi = ri ^ qi
t = int(g0[0]) ^ int(g0[12]) ^ int(g0[15])
g0 = str(t) + g0[0:-1]
ans += str(mi)
re = int(ans, 2)
decryptionImg[s][n][z] = re
if __name__ == "__main__":
img = cv2.imread("E:/9_25.jpg", 1)
cv2.imshow("Origin Img", img)
encryptionImg = np.zeros(img.shape, np.uint8)
fhhd(img, 0.15624562, 164, 10, encryptionImg)
cv2.imwrite("E:/a1.bmp", encryptionImg)
cv2.imshow("Encryption Img", encryptionImg)
img = cv2.imread("E:/a1.bmp", 1)
decryptionImg = np.zeros(img.shape, np.uint8)
fhhdjm(img, 0.15624562, 164, 10, decryptionImg)
cv2.imwrite("E:/a2.bmp", decryptionImg)
cv2.imshow("Decryption Img ", decryptionImg)
cv2.waitKey(-1)