# -*- coding: utf-8 -*-# @Author : 1cePeakimportmatplotlib.pyplotaspltimportcv2importnumpyasnpfromPILimportImageimg=cv2.imread('flag.png')defarnold_encode(image,shuffle_times,a,b):""" Arnold shuffle for rgb image
Args:
image: input original rgb image
shuffle_times: how many times to shuffle
Returns:
Arnold encode image
"""# 1:创建新图像arnold_image=np.zeros(shape=image.shape)# 2:计算Nh,w=image.shape[0],image.shape[1]N=h# 或N=w# 3:遍历像素坐标变换fortimeinrange(shuffle_times):forori_xinrange(h):forori_yinrange(w):# 按照公式坐标变换new_x=(1*ori_x+b*ori_y)%Nnew_y=(a*ori_x+(a*b+1)*ori_y)%N# 像素赋值print(image[ori_x,ori_y,:])print(arnold_image[new_x,new_y,:])arnold_image[new_x,new_y,:]=image[ori_x,ori_y,:]cv2.imwrite('flag_arnold_encode.png',arnold_image,[int(cv2.IMWRITE_PNG_COMPRESSION),0])returnarnold_imagedefarnold_decode(image,shuffle_times,a,b):""" decode for rgb image that encoded by Arnold
Args:
image: rgb image encoded by Arnold
shuffle_times: how many times to shuffle
Returns:
decode image
"""# 1:创建新图像decode_image=np.zeros(shape=image.shape)# 2:计算Nh,w=image.shape[0],image.shape[1]N=h# 或N=w# 3:遍历像素坐标变换fortimeinrange(shuffle_times):forori_xinrange(h):forori_yinrange(w):# 按照公式坐标变换new_x=((a*b+1)*ori_x+(-b)*ori_y)%Nnew_y=((-a)*ori_x+ori_y)%Ndecode_image[new_x,new_y,:]=image[ori_x,ori_y,:]cv2.imwrite('flag.png',decode_image,[int(cv2.IMWRITE_PNG_COMPRESSION),0])returndecode_image# arnold_encode(img, 1, 2, 3)arnold_decode(img,1,29294,7302244)
defarnold_encode(image,shuffle_times,a,b):""" Arnold shuffle for rgb image
Args:
image: input original rgb image
shuffle_times: how many times to shuffle
Returns:
Arnold encode image
"""# 1:创建新图像arnold_image=np.zeros(shape=image.shape)# 2:计算Nh,w=image.shape[0],image.shape[1]N=h# 或N=w# 3:遍历像素坐标变换fortimeinrange(shuffle_times):forori_xinrange(h):forori_yinrange(w):# 按照公式坐标变换new_x=(1*ori_x+b*ori_y)%Nnew_y=(a*ori_x+(a*b+1)*ori_y)%N# 像素赋值# print(image[ori_x, ori_y, :])# print(arnold_image[new_x, new_y, :])arnold_image[new_x,new_y,:]=image[ori_x,ori_y,:]# 更新坐标image=np.copy(arnold_image)cv2.imwrite('flag_arnold_encode.png',arnold_image,[int(cv2.IMWRITE_PNG_COMPRESSION),0])returnarnold_image