修改了尺寸变换导致的图像失真问题,同时简化了部分代码。
本次内容为验证嵌入流程,所以过程不算繁杂,关键参数和小波变换级数后期可作为密钥。
import cv2 import pywt import numpy as np from PIL import Image def inches3(num,times): for i in range(times): num = round((num/2+1)) return num def Icm(Img_path): def maxRC(): Img = cv2.imread(Img_path, 0) R = Img.shape[0] C = Img.shape[1] num = max(R, C) return num num = maxRC() rc_new = inches3(num, 2) # 3-1(3维小波-1维小波) return rc_new Img_path = '100_10.bmp' waterImg_path = 'new.bmp' Img = cv2.imread(Img_path) Img = Img[:, :, [2, 1, 0]] # 调整通道顺序 waterImg = cv2.imread(waterImg_path, 0) # 读取水印灰度图像 rcnew = Icm(Img_path) # 对应1920,此处值为482 waterImg_new = cv2.resize(waterImg, (rcnew, rcnew)) R = Img.shape[0] C = Img.shape[1] RC_new = max(R, C) Img_new = cv2.resize(Img, (RC_new, RC_new)) (r, g, b) = cv2.split(Img_new) # 原图像三级小波变换 coeffs1 = pywt.wavedec2(b, 'db2', level=3) [ca, (ch3, cv3, cd3), (ch2, cv2, cd2), (ch1, cv1, cd1)] = coeffs1 # 水印图像一级小波变换 coeffs2 = pywt.wavedec2(waterImg_new, 'db2', level=1) [cA, (cH1, cV1, cD1)] = coeffs2 # 自定义嵌入系数 a1 = 0.1 a2 = 0.2 a3 = 0.1 a4 = 0.1 # 嵌入部分 ca = ca + cA * a1 ch3 = ch3 + cH1 * a2 cv3 = cv3 + cV1 * a3 cd3 = cd3 + cD1 * a4 newImg = pywt.waverec2([ca, (ch3, cv3, cd3), (ch2, cv2, cd2), (ch1, cv1, cd1)], 'db2') merged = np.ones(Img_new.shape, dtype=np.uint8) merged[:, :, 2] = newImg merged[:, :, 1] = g merged[:, :, 0] = r Img = Image.fromarray(merged) Img = Img.resize((1920, 1080), Image.ANTIALIAS) #重新调整大小,抗锯齿 Img.save('test.png')
原图像为1920*1080 .bmp格式(从视频中抽取)
因为后期将用于视频文件处理,所以代码中的尺寸变换暂时只针对这一固定尺寸。
结果示例:
嵌入后
原图(.bmp格式无法上传,此处为截图)1920*1080
水印图像( 经过Arnold置乱处理)100*100
Arnold阿诺德置乱(猫脸变换)图像盲水印注入预处理(python)
基于dwt(离散小波变换)实现彩色图像水印嵌入部分_1.0