数字水印 | Arnold 变换的 Python 代码实现(灰度图版)

效果

在这里插入图片描述

将彩色图转换为灰度图,并进行 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()


  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值