带有高斯噪声攻击、椒盐噪声攻击、旋转攻击的基于DWT的图像信息隐藏算法代码,用python实现
时间: 2024-05-14 14:19:01 浏览: 201
基于python的图像信息隐藏算法的实现.zip
以下是一个基于DWT的图像信息隐藏算法,它包括高斯噪声攻击、椒盐噪声攻击和旋转攻击的Python代码实现:
```python
import numpy as np
import cv2
from scipy.fftpack import dct, idct
from scipy import signal
import random
def add_gaussian_noise(image, mean=0, var=0.01):
"""
添加高斯噪声
"""
row, col = image.shape
sigma = var ** 0.5
gauss = np.random.normal(mean, sigma, (row, col))
gauss = gauss.reshape(row, col)
noisy = image + gauss
return noisy
def add_salt_pepper_noise(image, salt_vs_pepper=0.5, amount=0.004):
"""
添加椒盐噪声
"""
row, col = image.shape
num_salt = np.ceil(amount * image.size * salt_vs_pepper)
num_pepper = np.ceil(amount * image.size * (1.0 - salt_vs_pepper))
# 添加椒盐噪声
coords_salt = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
image[coords_salt] = 1
coords_pepper = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
image[coords_pepper] = 0
return image
def rotate(image, angle=30, scale=1.0):
"""
旋转图像
"""
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
def dwt2d(img, mode='haar'):
"""
二维离散小波变换
"""
LL, (LH, HL, HH) = pywt.dwt2(img, mode)
return LL, LH, HL, HH
def idwt2d(LL, LH, HL, HH, mode='haar'):
"""
二维离散小波逆变换
"""
img = pywt.idwt2((LL, (LH, HL, HH)), mode)
return img
def embed(img, watermark):
"""
图像信息隐藏
"""
# 将水印展成一维向量
watermark = watermark.flatten()
# 对原始图像进行二维离散小波变换
LL, LH, HL, HH = dwt2d(img)
# 将LH、HL、HH系数转化为一维数组
LH = LH.flatten()
HL = HL.flatten()
HH = HH.flatten()
# 将水印嵌入LH、HL、HH系数的最后一位
for i in range(len(watermark)):
LH[i] = (LH[i] & 254) | watermark[i]
HL[i] = (HL[i] & 254) | watermark[i]
HH[i] = (HH[i] & 254) | watermark[i]
# 将修改后的LH、HL、HH系数重新组合成矩阵
LH = LH.reshape(LL.shape[0] // 2, LL.shape[1] // 2)
HL = HL.reshape(LL.shape[0] // 2, LL.shape[1] // 2)
HH = HH.reshape(LL.shape[0] // 2, LL.shape[1] // 2)
# 将修改后的系数与LL系数组合成二维离散小波变换系数矩阵
coeffs = (LL, (LH, HL, HH))
# 对修改后的系数进行二维离散小波逆变换,得到嵌入水印后的图像
watermarked_img = idwt2d(*coeffs)
return watermarked_img
def extract(img, watermark_shape):
"""
图像信息提取
"""
# 对水印形状进行验证
watermark_size = np.prod(watermark_shape)
assert watermark_size <= img.size, '水印大小超过图像大小'
# 对原始图像进行二维离散小波变换
LL, LH, HL, HH = dwt2d(img)
# 将LH、HL、HH系数转化为一维数组
LH = LH.flatten()
HL = HL.flatten()
HH = HH.flatten()
# 从LH、HL、HH系数的最后一位中提取水印
watermark = np.zeros(watermark_shape, dtype=np.uint8)
for i in range(watermark_size):
watermark[i // watermark_shape[1], i % watermark_shape[1]] = LH[i] & 1
# 返回提取的水印
return watermark
if __name__ == '__main__':
# 读取原始图像
img = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
# 对原始图像进行预处理,添加高斯噪声、椒盐噪声、旋转攻击
img_noise_gaussian = add_gaussian_noise(img)
img_noise_salt_pepper = add_salt_pepper_noise(img)
img_rotate = rotate(img, angle=30, scale=1.0)
# 显示原始图像和预处理后的图像
cv2.imshow('Original', img)
cv2.imshow('Gaussian noise', img_noise_gaussian)
cv2.imshow('Salt & pepper noise', img_noise_salt_pepper)
cv2.imshow('Rotate', img_rotate)
# 嵌入水印
watermark = np.random.randint(0, 2, (128, 128), dtype=np.uint8)
watermarked_img = embed(img, watermark)
watermarked_img_noise_gaussian = embed(img_noise_gaussian, watermark)
watermarked_img_noise_salt_pepper = embed(img_noise_salt_pepper, watermark)
watermarked_img_rotate = embed(img_rotate, watermark)
# 提取水印
extracted_watermark = extract(watermarked_img, watermark_shape=watermark.shape)
extracted_watermark_noise_gaussian = extract(watermarked_img_noise_gaussian, watermark_shape=watermark.shape)
extracted_watermark_noise_salt_pepper = extract(watermarked_img_noise_salt_pepper, watermark_shape=watermark.shape)
extracted_watermark_rotate = extract(watermarked_img_rotate, watermark_shape=watermark.shape)
# 对比原始水印和提取出的水印
print('原始水印:\n', watermark)
print('提取出的水印(原始图像):\n', extracted_watermark)
print('提取出的水印(高斯噪声图像):\n', extracted_watermark_noise_gaussian)
print('提取出的水印(椒盐噪声图像):\n', extracted_watermark_noise_salt_pepper)
print('提取出的水印(旋转攻击图像):\n', extracted_watermark_rotate)
# 显示嵌入水印后的图像和提取出的水印
cv2.imshow('Watermarked (Original)', watermarked_img)
cv2.imshow('Watermarked (Gaussian noise)', watermarked_img_noise_gaussian)
cv2.imshow('Watermarked (Salt & pepper noise)', watermarked_img_noise_salt_pepper)
cv2.imshow('Watermarked (Rotate)', watermarked_img_rotate)
cv2.imshow('Extracted watermark (Original)', extracted_watermark * 255)
cv2.imshow('Extracted watermark (Gaussian noise)', extracted_watermark_noise_gaussian * 255)
cv2.imshow('Extracted watermark (Salt & pepper noise)', extracted_watermark_noise_salt_pepper * 255)
cv2.imshow('Extracted watermark (Rotate)', extracted_watermark_rotate * 255)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个代码中,我们实现了一个基于DWT的图像信息隐藏算法,并对原始图像进行了高斯噪声攻击、椒盐噪声攻击和旋转攻击。我们首先读取原始图像,然后添加三种类型的攻击。我们生成一个随机水印,并将其嵌入到原始图像、高斯噪声图像、椒盐噪声图像和旋转攻击图像中。最后,我们提取出嵌入的水印,并将其与原始水印进行比较。
阅读全文