效果
将彩色图转换为灰度图,并进行 A r n o l d \mathsf{Arnold} Arnold 置乱和还原。
代码
import cv2
import numpy as np
from matplotlib import pyplot as pltdef arnold(img, shuffle_times, a, b):r, c, d = img.shapeimg = img[:, :, 0]p = np.zeros((r, c), np.uint8)for s in range(shuffle_times):for i in range(r):for j in range(c):x = (i + b * j) % ry = (a * i + (a * b + 1) * j) % cp[x, y] = img[i, j]img = np.copy(p)return pdef de_arnold(img, shuffle_times, a, b):r, c = img.shapep = np.zeros((r, c), np.uint8)for s in range(shuffle_times):for i in range(r):for j in range(c):x = ((a * b + 1) * i - b * j) % ry = (- a * i + j) % cp[x, y] = img[i, j]img = np.copy(p)return pImg_path = 'white_bear.jpg'
Img = cv2.imread(Img_path)
Img = Img[:, :, [2, 1, 0]]Img_arnold = arnold(Img, 5, 2, 3)
Img_inverse_arnold = de_arnold(Img_arnold, 5, 2, 3)plt.subplot(1, 3, 1)
plt.title("original", fontsize=12, loc="center")
plt.axis('off')
plt.imshow(Img, cmap='gray')plt.subplot(1, 3, 2)
plt.title("arnold", fontsize=12, loc="center")
plt.axis('off')
plt.imshow(Img_arnold, cmap='gray')plt.subplot(1, 3, 3)
plt.title("de_arnold", fontsize=12, loc="center")
plt.axis('off')
plt.imshow(Img_inverse_arnold, cmap='gray')plt.savefig('test.jpg', dpi=400, bbox_inches='tight')
plt.show()