插入在前面的文章 本文主要是提取算法,详细注释看插入算法
import gdal
import numpy
from skimage import data,img_as_float,img_as_ubyte
import cv2
filename = "f:/bs/band_11_255_0.0075.tif"#插入水印后的图片
dataset = gdal.Open(filename)
im_width = dataset.RasterXSize #获取长
im_height = dataset.RasterYSize #获取宽
im_bands = dataset.RasterCount #获取通道数
im_data = dataset.ReadAsArray(0,0,im_width,im_height)
BLOCK = numpy.zeros([32,32])
M = 3808
N = 238
K = 16
I = numpy.zeros([M,M])
I = im_data[2000:,2000:]
filename = "f:/bs/band_1.tif"
dataset = gdal.Open(filename)
im_width = dataset.RasterXSize #获取长
im_height = dataset.RasterYSize #获取宽
im_bands = dataset.RasterCount #获取通道数
P = dataset.ReadAsArray(0,0,im_width,im_height)
P = P[2000:,2000:]W = numpy.zeros([N,N])
for p in range(238):
for q in range (238):
x = p*K
y = q*K
BLOCK1 = P[x:x+K,y:y+K]
BLOCK2 = I[x:x+K,y:y+K]
BLOCK1 = img_as_float(BLOCK1) #转换unit8为float
BLOCK2 = img_as_float(BLOCK2)
BLOCK1 = cv2.dct(BLOCK1)
BLOCK2 = cv2.dct(BLOCK2)
a = BLOCK1[0][0]/BLOCK2[0][0]-1
if a<0:
W[p][q]=1
else :
W[p][q]=-1
W = img_as_ubyte(W)
#转换float为unit8
format = "GTiff"driver = gdal.GetDriverByName(format)
tods = driver.Create("f:/bs/water_wh_1_255_0.0075.bmp",238,238,1,options=["INTERLEAVE = PIXEL"])
tods.WriteRaster(0,0,238,238,W.tostring(),238,238,band_list=[1])
原图:
插入水印后:
水印:
提取出的水印: