Date: 2019.3.24
前言
? ? 數(shù)字水印技術(shù)一般用于版權(quán)認(rèn)證。在實(shí)際使用中,嵌入水印的魯棒性就顯得非常重要。通常會(huì)采用各種方式進(jìn)行攻擊測(cè)試,比如加噪濾波,縮放、旋轉(zhuǎn)、剪切、JPEG壓縮等。本文講述了采用DCT變換的方式進(jìn)行嵌入水印和提取水印,并加入高斯噪聲和均值濾波進(jìn)行攻擊測(cè)試。
1、參考
https://blog.csdn.net/a573233077/article/details/73498650
https://blog.csdn.net/zxc024000/article/details/49429405
https://zhidao.baidu.com/question/418723318.html
http://www.pudn.com/Download/item/id/3349409.html
https://doc.mbalib.com/view/deb57fe43c2b56e02e272596ae5d855b.html
http://blog.sina.com.cn/s/blog_a98e39a20101507s.html
2、Matlab實(shí)現(xiàn)
下面只是嵌入水印部分的代碼,提取部分有需要可以通過博客左側(cè)公告欄中的QQ圖標(biāo)或者直接加QQ(2963033731)聯(lián)系我。
%% 基于DCT的視頻水印技術(shù)Matlab實(shí)現(xiàn)
% Date: 2019.3.22
clc;
clear all;
%mov=aviread('lww.avi'); %讀取視頻
%movie(mov); %顯示視頻內(nèi)容
%% 1.讀取視頻內(nèi)容并顯示
readerobj = VideoReader('longmao.mp4','tag','myreader');
vidFrames = read(readerobj);
numFrames = get(readerobj, 'NumberOfFrames'); %獲取視頻幀數(shù)
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
end
% 創(chuàng)建一個(gè)顯示句柄
hf = figure;
% 設(shè)置自適應(yīng)視頻寬高
set(hf, 'position', [150 150 readerobj.Width readerobj.Height])
%播放視頻內(nèi)容
movie(hf, mov, 1, readerobj.FrameRate);
[filename, filepath] = uigetfile('.bmp', '輸入水印圖像');
watermarkImgFile = strcat(filepath, filename);
watermarkImg = imread(watermarkImgFile);
%% 2.采用DCT方式嵌入水印圖像
for m = 1 : 3 %numFrames %若需要處理全部視頻幀,可以將3修改為numFrames
k=50; %設(shè)置最小相關(guān)系數(shù)差別
blocksize=8; % 設(shè)置嵌入的塊尺寸
%讀取嵌入對(duì)象
data = imresize(mov(m).cdata,[512 512]);%對(duì)處理的視頻圖像進(jìn)行裁剪為512x512,便于處理
data = rgb2gray(data);%灰度化處理
%% 添加高斯噪聲
Img = imnoise(data, 'gaussian',0, 0.01);
figure,
imshow(Img, []),title('高斯噪聲圖像');
imwrite(Img, '高斯噪聲圖像.jpg');
%% 對(duì)高斯噪聲視頻圖像進(jìn)行均值濾波
n = 3; % 3x3模板
A=fspecial('average',n);
FilterImg=filter2(A, Img);
figure, imshow(FilterImg, []),title('均值濾波后的圖像');
cover_object= FilterImg;
FilterImg = uint8(FilterImg);
imwrite(FilterImg, '均值濾波后的圖像.jpg');
%確定嵌入對(duì)象的尺寸
Mc=size(cover_object,1); %高度
Nc=size(cover_object,2); %寬度
%基于嵌入對(duì)象尺寸和和塊尺寸決定最大信息量
max_message=Mc*Nc/(blocksize^2);
%讀取水印圖像(信息)
message=double(watermarkImg);
Mm=size(message,1); %高度
Nm=size(message,2); %寬度
% 調(diào)整信息為向量表示
message=fix(reshape(message,Mm*Nm,1)./2);
% 檢查信息不能太長導(dǎo)致難以嵌入
if (length(message) > max_message)
error('Message too large to fit in Cover Object')
end
% 填充信息量到最大尺寸
message_pad=ones(1,max_message);
message_pad(1:length(message))=message;
% generate shell of watermarked image
watermarked_image=cover_object;
% process the image in blocks
% encodes such that (5,2) > (4,3) when message(kk)=0
% and that (5,2) < (4,3) when message(kk)=1
x=1;
y=1;
h=waitbar(0,'DCT嵌入水印,請(qǐng)等待');
for kk = 1:length(message_pad)
% transform block using DCT
dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
% if message bit is black, (5,2) > (4,3)
if (message_pad(kk) == 0)
% if (5,2) < (4,3) then we need to swap them
if (dct_block(5,2) < dct_block(4,3))
temp=dct_block(4,3);
dct_block(4,3)=dct_block(5,2);
dct_block(5,2)=temp;
end
% if message bit is white, (5,2) < (4,3)
elseif (message_pad(kk) == 1)
% if (5,2) > (4,3) then we need to swap them
if (dct_block(5,2) >= dct_block(4,3))
temp=dct_block(4,3);
dct_block(4,3)=dct_block(5,2);
dct_block(5,2)=temp;
end
end
% now we adjust the two values such that their difference >= k
if dct_block(5,2) > dct_block(4,3)
if dct_block(5,2) - dct_block(4,3) < k
dct_block(5,2)=dct_block(5,2)+(k/2);
dct_block(4,3)=dct_block(4,3)-(k/2);
end
else
if dct_block(4,3) - dct_block(5,2) < k
dct_block(4,3)=dct_block(4,3)+(k/2);
dct_block(5,2)=dct_block(5,2)-(k/2);
end
end
% transform block back into spatial domain
watermarked_image(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);
% move on to next block. At and of row move to next row
if (x+blocksize) >= Nc
x=1;
y=y+blocksize;
else
x=x+blocksize;
end
waitbar(kk/max_message,h);
end
close(h);
% convert to uint8 and write the watermarked image out to a file
watermarked_image_int=uint8(watermarked_image);
% 顯示嵌入水印圖像
imshow(watermarked_image_int,[]),title('嵌入水印圖像')
imwrite(watermarked_image_int, '嵌入水印圖像.jpg');
3、實(shí)驗(yàn)效果圖





4、魯棒性攻擊測(cè)試
主要包括加噪濾波,縮放、旋轉(zhuǎn)、剪切、JPEG壓縮。
5、補(bǔ)充知識(shí)
matlab如何將矩陣保存為圖片?
原文:https://blog.csdn.net/a573233077/article/details/73498650
方法一:
imwrite(mat2gray(matrix), 'matrix.tif');
不管matrix原先是double或者uint8類型,數(shù)據(jù)均被擴(kuò)展到0-255的范圍。
好處是打開圖片后就是需要的效果,缺點(diǎn)是再次load該矩陣時(shí),值不再反應(yīng)原來的數(shù)值,而是0-255區(qū)間的。
方法二:
imwrite(uint8(matrix)), 'matrix.tif' );
缺點(diǎn)是打開圖片后色彩和理想的不同,好處是完整的保存了原來的數(shù)據(jù)。load后可直接使用。
注意,保存時(shí)要是uint8數(shù)據(jù),若是double類型直接保存,則保存的圖片只有0和1數(shù)值。
matlab如何將圖片保存為矩陣呢?
Mat = imread('picture');
THE END!