基于DWT的数字图像信息隐藏算法代码,带有高斯噪声攻击,可以运行,载体图像和水印图像经过灰度处理,有提取和嵌入水印算法,用python实现
时间: 2023-06-18 18:04:13 浏览: 112
由于您没有提供具体的数字图像信息隐藏算法,以下是一个基于DWT的数字图像水印嵌入和提取的Python实现示例,其中包括高斯噪声攻击:
```python
import numpy as np
import cv2
from pywt import dwt2, idwt2
def embed_watermark(img_path, watermark_path, alpha):
# 读取载体图像和水印图像
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
watermark = cv2.imread(watermark_path, cv2.IMREAD_GRAYSCALE)
# 对图像进行DWT变换
coeffs = dwt2(img, 'haar')
cA, (cH, cV, cD) = coeffs
# 对水印图像进行DWT变换
w_coeffs = dwt2(watermark, 'haar')
w_cA, (w_cH, w_cV, w_cD) = w_coeffs
# 水印嵌入
new_cA = cA + alpha * w_cA
new_cH = cH + alpha * w_cH
new_cV = cV + alpha * w_cV
new_cD = cD + alpha * w_cD
# 对嵌入后的图像进行IDWT变换
new_coeffs = (new_cA, (new_cH, new_cV, new_cD))
watermarked_img = idwt2(new_coeffs, 'haar')
# 添加高斯噪声攻击
mean = 0
var = 0.1
sigma = var ** 0.5
gauss = np.random.normal(mean, sigma, img.shape)
noisy_img = img + gauss
# 返回嵌入后的图像和添加高斯噪声攻击后的图像
return watermarked_img, noisy_img
def extract_watermark(img_path, alpha, watermarked_img_path):
# 读取嵌有水印的图像
watermarked_img = cv2.imread(watermarked_img_path, cv2.IMREAD_GRAYSCALE)
# 对嵌有水印的图像进行DWT变换
coeffs = dwt2(watermarked_img, 'haar')
cA, (cH, cV, cD) = coeffs
# 提取水印
extracted_cA = (cA - cA.mean()) / alpha
extracted_cH = (cH - cH.mean()) / alpha
extracted_cV = (cV - cV.mean()) / alpha
extracted_cD = (cD - cD.mean()) / alpha
# 对提取的图像进行IDWT变换
extracted_coeffs = (extracted_cA, (extracted_cH, extracted_cV, extracted_cD))
extracted_watermark = idwt2(extracted_coeffs, 'haar')
# 返回提取的水印图像
return extracted_watermark
```
需要注意的是,该示例中使用的是haar小波变换,您可以根据需要更改为其他小波变换。此外,高斯噪声的参数也可以根据需要进行调整。
阅读全文