【图像隐写】基于matlab GUI LSB图像隐写【含Matlab源码 713期】

462 阅读6分钟

一、简介

1 LSB算法简介
LSB全称为 Least Significant Bit(最低有效位),是一种简单而有效的数据隐藏技术。LSB隐写的基本方法是用欲嵌入的秘密信息取代载体图像的最低比特位,原来的图像的高位平面与代表秘密信息的最低平面组成含隐蔽信息的新图像。
在这里插入图片描述
灰度化的图像为单通道格式存储像素,每个像素值在0~255内,而像素的位平面则是对应二进制的像素的各个位。以上图为例,某个像素的值为78,其二进制01001110,从左到右位权依次降低,最左边为最高有效位(MSB,其位权为 2 7 2^72
7
),最右边位最低有效位(LSB,位权为2 0 2^02
0
)。把每个像素的相同位抽取出来组成一个新的平面,就是所谓的图的位平面。而LSB隐写算法,如其名字,是在LSB也就是最低位平面进行信息嵌入/隐藏。

需要注意的一点是,LSB嵌入的时候,载体图像格式应该为灰度图格式

以著名的Lena图为例,一下是灰度图Lena原图:
在这里插入图片描述
下面是其各个位平面图,从左到右、从上到下位平面依次降低:
在这里插入图片描述
可以看到,位平面越高包含的原图像信息越多,对图像的灰度值贡献越大,并且相邻比特的相关性也越强,反之则相反。LSB最低位平面基本上不包含图像信息了,类似随机的噪点/噪声,因此,可以在此处填入水印/秘密信息。

嵌入示意图如下:
在这里插入图片描述
选取不同位平面嵌入时,LSB算法的保真度:
在这里插入图片描述2 算法原理
通俗来讲我们看到的图片都是由一个个小的像素点来构成的,所有像素点摆在一起,构成一个大方块,这个大方块就是我们所见的图像。灰度图像(也就是我们平时所说的黑白图像)是由一层像素点组成的,而彩色图像是由三层这样的灰度图像组成的。这里拿灰度图像举例,我们之所以能在图像上看到黑色和白色,是因为每个像素点的像素值不同。0表示纯黑,255表示纯白,灰色就是由这两个数字之间的值构成。越靠近0越黑,越靠近255越白。那为什么是0和255呢?因为计算机是二进制,它会用8个比特来表示一个像素点(也可以用更多的比特,这样图像的颜色分级就越多,同时图像也会占用更大的空间,但是普通人的眼睛并不能辨认这么多的颜色,除非你异于常人),所以最大值是255,最小是0。lsb就是基于2进制这一特点来隐藏信息的,因为人眼并不是很精密的颜色或亮度的感知器,所以把像素灰度上下微调1是不会被人眼察觉的,也就是修改8位二进制码中最小的一位。当我们把图片每个像素的最后一位按照我们的想法改变,使他表现为我们想要的信息,但用户却不能看出,也不会影响图片的内容。这就是lsb数字水印。
3 LSB算法的基本特点:
LSB是一种大容量的数据隐藏算法
LSB的鲁棒性相对较差(当stego图像遇到信号处理,比如:加噪声,有损压缩等,在提取嵌入信息时会丢失)
4 常见LSB算法的嵌入方法:
秘密信息在最低位平面连续嵌入至结束,余下部分不作任何处理(典型软件MandelSteg)
秘密信息在最低位平面连续嵌入至结束,余下部分随机化处理(也称沙化处理,典型软件PGMStealth)
秘密信息在最低位平面和次低位平面连续嵌入,并且是同时嵌入最低位平面和次低位平面
秘密信息在最低位平面嵌入,等最低位平面嵌入完全嵌入之后,再嵌入次低位平面
秘密信息在最低位平面随机嵌入
以上五种方式,当嵌入容量不同时,鲁棒性不同

二、源代码

function varargout = shuiyin(varargin)
% SHUIYIN MATLAB code for shuiyin.fig
%      SHUIYIN, by itself, creates a new SHUIYIN or raises the existing
%      singleton*.
%
%      H = SHUIYIN returns the handle to a new SHUIYIN or the handle to
%      the existing singleton*.
%
%      SHUIYIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SHUIYIN.M with the given input arguments.
%
%      SHUIYIN('Property','Value',...) creates a new SHUIYIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before shuiyin_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to shuiyin_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help shuiyin

% Last Modified by GUIDE v2.5 12-Dec-2019 21:53:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @shuiyin_OpeningFcn, ...
                   'gui_OutputFcn',  @shuiyin_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before shuiyin is made visible.
function shuiyin_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to shuiyin (see VARARGIN)

% Choose default command line output for shuiyin
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes shuiyin wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = shuiyin_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename1, pathname1] = uigetfile('*.jpg', '选择水印图片');
global pathfile1;
global pathfre1;
pathfile1=fullfile(pathname1, filename1);
pathfre1 = pathname1;
I0=rgb2gray(imread(pathfile1));
mrows=256;
ncols=256;
input=imresize(I0,[mrows ncols]);
axes(handles.axes1);axis off
imshow(input);
title('原始图像');

handles.input=input;
guidata(hObject, handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename2, pathname2] = uigetfile('*.jpg', '选择水印图片');
global pathfile2;
global pathfre2;
pathfile2=fullfile(pathname2, filename2);
pathfre2 = pathname2;
M0=rgb2gray(imread(pathfile2));
axes(handles.axes2);axis off
mrows=256;
ncols=256;
water=imresize(M0,[mrows ncols]);
imshow(water);
title('水印'); 
f=water;
I=handles.input;
for k=1:92    %迭代次数为T=192for x=1:256    %图像的大小为256×256
        for y=1:256
            x1=x+y;
            y1=x+2*y;
            if x1>256
                x1=mod(x1,256);
            end
            if y1>256
                y1=mod(y1,256);
            end
            if x1==0
                x1=256;
            end
            if y1==0
                y1=256;
            end
            f_ar(x1,y1)=f(x,y);
        end
    end
    f=f_ar;
end

%*********************嵌入水印*********************
I_1_0=bitset(I,1,0);
J=bitset(I_1_0,1,f_ar);

%*********************提取水印*********************
f_get=bitget(J,1);

%*************解密************
fg=mat2gray(double(f_get)); %double型必须用mat2gray进行显示

for k=1:100                 %迭代次数为192for x=1:256             %图像的大小为256×256
        for y=1:256
            x1=x+y;
            y1=x+2*y;
            if x1>256
                x1=mod(x1,256);
            end
            if y1>256
                y1=mod(y1,256);
            end
            if x1==0
                x1=256;
            end
            if y1==0
                y1=256;
            end
            f_re(x1,y1)=fg(x,y);
        end
    end

三、运行结果

在这里插入图片描述

四、备注

版本:2014a
完整代码或代写加1564658423