内容对应之前嵌入部分的代码。
import cv2 import pywt from PIL import Image originalImg_path = '100_10.bmp' processedImg_path = 'test.png' originalImg = cv2.imread(originalImg_path) originalImg = originalImg[:, :, [2, 1, 0]] processedImg = cv2.imread(processedImg_path) processedImg = processedImg[:, :, [2, 1, 0]] R = originalImg.shape[0] C = originalImg.shape[1] RC_new = max(R, C) originalImg = cv2.resize(originalImg, (RC_new, RC_new)) processedImg = cv2.resize(processedImg, (RC_new, RC_new)) (r1, g1, b1) = cv2.split(originalImg) (r2, g2, b2) = cv2.split(processedImg) coeffs1 = pywt.wavedec2(b1, 'db2', level=3) [ca, (ch3, cv3, cd3), (ch2, cv2, cd2), (ch1, cv1, cd1)] = coeffs1 coeffs2 = pywt.wavedec2(b2, 'db2', level=3) [cA, (cH3, cV3, cD3), (cH2, cV2, cD2), (cH1, cV1, cD1)] = coeffs2 #系数需与嵌入部分一一对应 CA1 = (cA - ca) * 10 CH1 = (cH3 - ch3) * 5 CV1 = (cV3 - cv3) * 10 CD1 = (cD3 - cd3) * 10 waterImg = pywt.waverec2([CA1, (CH1, CV1, CD1)], 'db2') waterImg = Image.fromarray(waterImg) waterImg = waterImg.resize((100, 100), Image.ANTIALIAS) waterImg = waterImg.convert('RGB') waterImg.save('watermark.bmp')
嵌入和提取部分都确保了最终图像尺寸及色彩不变。
提取部分需要与原图进行对比才能得出最终结果,之后会继续改进算法,使得不经过原图也能进行提取。
水印嵌入后
水印提取
经图像增强后
图像增强部分的代码之后,会根据小波原理进行重写,这里用到的是直方图均衡化。