效果
将彩色图转换为灰度图,并进行 A r n o l d \mathsf{Arnold} Arnold 置乱和还原。
代码
import cv2
import numpy as np
from matplotlib import pyplot as plt
def arnold(img, shuffle_times, a, b):
r, c, d = img.shape
img = 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) % r
y = (a * i + (a * b + 1) * j) % c
p[x, y] = img[i, j]
img = np.copy(p)
return p
def de_arnold(img, shuffle_times, a, b):
r, c = img.shape
p = 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) % r
y = (- a * i + j) % c
p[x, y] = img[i, j]
img = np.copy(p)
return p
Img_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()