Arnold阿诺德置乱(猫脸变换)图像盲水印注入预处理(python)

针对彩色图像实现了arnold置换与置换还原,解决了cv2.imread()读取图像偏色、处理后无结果显示或显示结果为纯色图像等问题。

参考来源:

https://blog.csdn.net/weixin_43924621/article/details/116211424

import cv2
import numpy as np
import matplotlib.image as mpimg

def arnold(img, shuffle_times, a, b):
    r, c, d = img.shape # 高,宽,通道个数
    p = np.zeros(img.shape, np.uint8)
    for times 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

img = mpimg.imread('lena.bmp')
img = img[:, :, [2, 1, 0]] # 解决显色问题
new = arnold(img, 1, 1, 3)
cv2.imshow('picture', new)
cv2.waitKey(0)
cv2.imwrite('new.bmp', new)

 

置换后还原代码:

def de_arnold(img,shuffle_time,a,b):
    r, c, d = img.shape
    dp = np.zeros(img.shape, np.uint8)

    for s in range(shuffle_time): 
        for i in range(r):
            for j in range(c):
                x = ((a * b + 1) * i - b * j) % r
                y = (-a * i + j) % c
                dp[x, y, :] = img[i, j, :]
        img = np.copy(dp) 
    return img

img = mpimg.imread('new.bmp')
img = img[:, :, [2, 1, 0]] 
new = de_arnold(img, 1, 1, 3)
cv2.imshow('picture', new)
cv2.waitKey(0)

 

1. arnold() 和 de_arnold()中 shuffle_time,a,b 三个参数的数值需要对应才能实现图像还原。
2. de_arnold()中的img为运行arnold()后通过cv2.imwrite()保存的结果。
3. 示例图为512*512的lena.bmp,此博客不支持上传.bmp格式的图片。

置乱后的图像(shuffle_time=1,a=1,b=3)

 

还原:

 

后续可在此基础上使用离散小波变换继续对图像进行处理。


 

 

 


 

 

 


 





 

posted on 2022-02-08 21:35  HOr7z  阅读(1297)  评论(0编辑  收藏  举报