完整的基于DWT的数字图像信息隐藏算法代码,带有高斯噪声攻击,可以运行,载体图像和水印图像经过灰度处理,有提取和嵌入水印算法,用python实现
时间: 2023-06-18 12:04:13 浏览: 245
以下是基于DWT的数字图像信息隐藏算法的Python实现代码,包含高斯噪声攻击、提取和嵌入水印算法:
```python
import numpy as np
import cv2
from scipy.fftpack import dct, idct
from scipy.stats import norm
def embed_watermark(img_path, wm_path, alpha):
# 读取载体图像和水印图像
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
wm = cv2.imread(wm_path, cv2.IMREAD_GRAYSCALE)
# 对载体图像和水印图像进行离散小波变换
img_w = pywt.wavedec2(img, 'haar', level=1)
wm_w = pywt.wavedec2(wm, 'haar', level=1)
# 将水印图像进行0-1归一化
wm_norm = wm_w[0] / 255
# 对LL子带进行DCT变换
img_LL = img_w[0]
img_LL_dct = dct(img_LL, axis=0)
img_LL_dct = dct(img_LL_dct, axis=1)
# 对DCT系数进行量化
Q = np.array([[16, 11, 10, 16, 24, 40, 51, 61],
[12, 12, 14, 19, 26, 58, 60, 55],
[14, 13, 16, 24, 40, 57, 69, 56],
[14, 17, 22, 29, 51, 87, 80, 62],
[18, 22, 37, 56, 68, 109, 103, 77],
[24, 35, 55, 64, 81, 104, 113, 92],
[49, 64, 78, 87, 103, 121, 120, 101],
[72, 92, 95, 98, 112, 100, 103, 99]])
img_LL_dct = np.round(img_LL_dct / (alpha * Q)) * (alpha * Q)
# 将归一化后的水印嵌入到DCT系数的最低位
wm_embed = np.zeros_like(img_LL_dct)
wm_embed[:, :, 0] = img_LL_dct[:, :, 0]
wm_embed[:, :, 1] = img_LL_dct[:, :, 1]
wm_embed[:, :, 2] = img_LL_dct[:, :, 2]
wm_embed[:, :, 3] = img_LL_dct[:, :, 3]
wm_embed[:, :, 4] = img_LL_dct[:, :, 4]
wm_embed[:, :, 5] = img_LL_dct[:, :, 5]
wm_embed[:, :, 6] = img_LL_dct[:, :, 6]
wm_embed[:, :, 7] = img_LL_dct[:, :, 7]
wm_embed[:, :, 0] += 2 * wm_norm * alpha
wm_embed[:, :, 1] += 2 * wm_norm * alpha
wm_embed[:, :, 2] += 2 * wm_norm * alpha
wm_embed[:, :, 3] += 2 * wm_norm * alpha
wm_embed[:, :, 4] += 2 * wm_norm * alpha
wm_embed[:, :, 5] += 2 * wm_norm * alpha
wm_embed[:, :, 6] += 2 * wm_norm * alpha
wm_embed[:, :, 7] += 2 * wm_norm * alpha
# 对嵌入水印后的DCT系数进行反变换
img_LL_dct_embed = idct(wm_embed, axis=0)
img_LL_dct_embed = idct(img_LL_dct_embed, axis=1)
# 对反变换得到的图像进行量化逆操作
img_LL_dct_embed = np.round(img_LL_dct_embed / Q) * Q
# 对量化逆操作后的图像进行反DCT变换
img_LL_embed = idct(img_LL_dct_embed, axis=0)
img_LL_embed = idct(img_LL_embed, axis=1)
# 将嵌入水印后的LL子带与其他子带组合成新的小波系数
img_w_embed = list(img_w)
img_w_embed[0] = img_LL_embed
img_embed = pywt.waverec2(img_w_embed, 'haar')
# 对嵌入水印后的图像进行高斯噪声攻击
img_embed = np.float32(img_embed)
img_embed_noise = cv2.randn(img_embed, 0, 10)
img_embed_noise = np.clip(img_embed_noise, 0, 255)
img_embed_noise = np.uint8(img_embed_noise)
# 返回嵌入水印后的图像和添加高斯噪声的图像
return img_embed, img_embed_noise
def extract_watermark(img_path, alpha):
# 读取载体图像
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
# 对载体图像进行离散小波变换
img_w = pywt.wavedec2(img, 'haar', level=1)
# 对LL子带进行DCT变换
img_LL = img_w[0]
img_LL_dct = dct(img_LL, axis=0)
img_LL_dct = dct(img_LL_dct, axis=1)
# 对DCT系数进行量化
Q = np.array([[16, 11, 10, 16, 24, 40, 51, 61],
[12, 12, 14, 19, 26, 58, 60, 55],
[14, 13, 16, 24, 40, 57, 69, 56],
[14, 17, 22, 29, 51, 87, 80, 62],
[18, 22, 37, 56, 68, 109, 103, 77],
[24, 35, 55, 64, 81, 104, 113, 92],
[49, 64, 78, 87, 103, 121, 120, 101],
[72, 92, 95, 98, 112, 100, 103, 99]])
img_LL_dct_q = np.round(img_LL_dct / (alpha * Q)) * (alpha * Q)
# 提取嵌入的水印
wm_extract_norm = (img_LL_dct_q[:, :, 0] % (2 * alpha)) / (2 * alpha)
# 将提取的水印还原到0-255范围内
wm_extract = np.uint8(wm_extract_norm * 255)
# 返回提取的水印
return wm_extract
```
注意:在使用该代码时,需要先安装依赖库`numpy`、`scipy`和`opencv-python`。另外,该代码中的`alpha`参数用于控制嵌入水印的强度,需要根据实际情况进行调整。
阅读全文