haardwthaar源码的简单介绍

hacker|
121

文章目录:

matlab里小波变换,使用dwt2(x,'haar')后,如何对4部分图像进行显示

[ca,ch,cl,cd]=dwt2(x,'haar'); %x=imread('lena.bmp');

II=[ca ch;cl cd];

imshow(II,gray(256));

OpenCV源码中真正计算Haar特征的那段在哪

//针对大小为winsize的图,计算所有HaarFeature的rect,存入features返回,即获取所有特征坐标

CvIntHaarFeatures* icvCreateIntHaarFeatures( CvSize winsize,

int mode,

int symmetric )

//训练样本图集

//创建结构CvHaarTrainigData,分配空间,并未填入sample数据

CvHaarTrainigData* icvCreateHaarTrainingData( CvSize winsize, int maxnumsamples )

//计算训练样本集的特征值,调用icvGetTrainingDataCallback

//已知训练样本的积分图,计算特征值valcache、索引idxcache

void icvPrecalculate( CvHaarTrainingData* data, CvIntHaarFeatures* haarFeatures,

int numprecalculated )//numprecalculated:要计算的特征数

//userdata:含训练数据、haar特征

//计算从first开始的num个特征,结果存入mat

//若sampleIdx为null,则计算全部样本,否则计算由sampleIdx指定的样本

void icvGetTrainingDataCallback( CvMat* mat, CvMat* sampleIdx, CvMat*,

int first, int num, void* userdata )

//计算一个样本(积分图为sum和tilted)的一个HaarFeature,并返回该值

CV_INLINE float cvEvalFastHaarFeature( CvFastHaarFeature* feature,

sum_type* sum, sum_type* tilted )

//特征的rect由坐标表示转换为由像素索引表示

//每个haarFeature最多由3个rect组成,但都保留3个的存储空间

void icvConvertToFastHaarFeature( CvTHaarFeature* haarFeature,

CvFastHaarFeature* fastHaarFeature,

int size, int step )

求matlab代码 要求基于DWT的数字水印嵌入 水印为二维码 要求水印嵌入提取 并且压缩 剪切 加噪后再提取

clc

[a type]=myfunction(0.7,0);/a嵌入强度,type攻击类型

x=imread('E:\ori1.bmp');

subplot(2,3,1);

image(x);

title('初始待加入水印图像');

w=imread('E:\water1.bmp');

subplot(2,3,2);

imshow(w);

title('待用水印图像');

w=w/255;

[cA1,cH1,cV1,cD1]=dwt2(x,'haar');% 对原始图像x进行小波分解

[cA2,cH2,cV2,cD2]=dwt2(cA1,'haar');

[cA3,cH3,cV3,cD3]=dwt2(cA2,'haar');

[width,height,cnums]=size(w);% w为水印序列

for i=1:width

for j=1:height

waterdata(i,j)=double(cD3(i,j))+a*double(w(i,j));

end

end

cD3=waterdata;

X3=idwt2(cA3,cH3,cV3,cD3,'db1'); % 小波反变换,重构图像

X2=idwt2(X3,cH2,cV2,cD2,'db1');

Y=idwt2(X2,cH1,cV1,cD1,'db1');

subplot(2,3,3);

image(Y); % 显示水印结果图像

title('加入水印后的图像');

if type==0

A='无攻击'

Y=Y;

end

if type==1

A='放大2倍后的图像'

%(1)放大两倍的操作,(当然提取之前要先缩小两倍)

xxx1=imresize(Y,2,'bicubic');

xxx2=imresize(xxx1,1/2,'bicubic');

Y= double(xxx2);

end

if type==2

%(2)缩1/2

A='缩1/2的图像'

xxx1=imresize(Y,2/4,'bicubic');

xxx2=imresize(xxx1,4/2,'bicubic');

Y= double(xxx2);

end

%(3) 3×3空域低通滤波

if type==3

A='3×3空域低通滤波攻击'

B=(1/9)*ones(3,3);

xxx2=filter2(B,Y);

Y= double(xxx2);

end

%(4) 4领域平均

if type==4

A='4领域平均攻击'

B=[0 1 0;1 0 1;0 1 0]*(1/4);

xxx2=filter2(B,Y);

Y= double(xxx2);

end

%(5) 3*3窗口中值滤波:

if type==5

A='3*3窗口中值滤波攻击'

xxx2=medfilt2(Y);%默认3*3

Y= double(xxx2);

end

%%(6)裁减1/16

if type==6

A='裁减1/16攻击'

for i=128-44:128+45

for j=128-45:128+44

Y(i,j)=0;

end

end

Y=double(Y);

end

%(7) 高斯噪声的情况

if type==7

A='高斯噪声攻击 强度=0.01'

Y=imnoise(uint8(round(Y)),'gaussian',0,0.01);

end

%(8) JPEG压缩

if type==8

A='JPEG压缩,强度=10'

imwrite(uint8(round(Y)),'jpeg_n.jpg','jpg','Quality',10);%按压缩因子Quality的比例,将J2压缩到jpeg_n.jpg中。

[Y,map]=imread('jpeg_n.jpg','jpg');

end

%(9) 其他为无攻击

if type8

A='无攻击'

Y=Y;

end

subplot(2,3,4);

image(Y); % 显示攻击后水印结果图像

title(A);

% 水印提取部分

[cA1,cH1,cV1,cD1]=dwt2(x,'haar');% 对原始图像进行三级小波分解

[cA2,cH2,cV2,cD2]=dwt2(cA1,'haar');

[cA3,cH3,cV3,cD3]=dwt2(cA2,'haar');

olddata=cD3;

[ccA1,ccH1,ccV1,ccD1]=dwt2(Y,'haar');% 对含水印图像进行三级小波分解

[ccA2,ccH2,ccV2,ccD2]=dwt2(ccA1,'haar');

[ccA3,ccH3,ccV3,ccD3]=dwt2(ccA2,'haar');

[width,height]=size(ccD3);

newdata=ccD3;

waterdata=zeros(width,height);

for i=1:width

for j=1:height

watermark(i,j)=(double(newdata(i,j))-double(olddata(i,j)))/a; %提取算法

end

end

watermark=watermark*255;

subplot(2,3,5);

imshow(watermark);

title('提取结果');

原图像512*512 水印64*64

函数声明为function [a type]=myfunction(a,type)

什么是HAAR小波

小波(Wavelet)这一术语,顾名思义,“小波”就是小的波形。所谓“小”是指它具有衰减性;而称之为“波”则是指它的波动性,其振幅正负相间的震荡形式。与Fourier变换相比,小波变换是时间(空间)频率的局部化分析,它通过伸缩平移运算对信号(函数)逐步进行多尺度细化,最终达到高频处时间细分,低频处频率细分,能自动适应时频信号分析的要求,从而可聚焦到信号的任意细节,解决了Fourier变换的困难问题,成为继Fourier变换以来在科学方法上的重大突破。有人把小波变换称为“数学显微镜”。

Haar小波是小波的一种,是最简单的正交归一化小波。Haar基本小波函数定义在区间 [0,1]上。

wfilter = 'haar';%选择小波基

[CA,CH,CV,CD] = dwt2(x,wfilter, 'per');%小波变换

CA = (CA=T1) .* CA;%对4个自带分别阈值处理

CH = (CH=T2) .* CH;

CV = (CV=T3) .* CV;

CD = (CD=T4) .* CD;

result = idwt2(CA, CH, CV, CD, wfilter, 'per');%反变换重构图像。

基于Haar小波图象分解与重构研究

wfilter = 'haar';%选择小波基

[CA,CH,CV,CD] = dwt2(x,wfilter, 'per');%小波变换

CA = (CA=T1) .* CA;%对4个自带分别阈值处理

CH = (CH=T2) .* CH;

CV = (CV=T3) .* CV;

CD = (CD=T4) .* CD;

result = idwt2(CA, CH, CV, CD, wfilter, 'per');%反变换重构图像。

小弟毕业设计matlab基于扩频的dwt水印嵌入 求大神给个程序代码啊!

%% ---------------------------------------------------------------------%%

% 基于小波变换的扩频图象数字水印--水印嵌入

%% ---------------------------------------------------------------------%%

start_time=cputime;

k=0.6;

file_name='E:\zy\lena.bmp';

iCover = imread(file_name);

dCover = double(iCover);

dOrigCover = dCover;

Mc=size(iCover,1);

Nc=size(iCover,2);

file_name='E:\zy\copyright.bmp';

iWatermark = imread(file_name);

dWatermark = double(iWatermark);

Mm=size(iWatermark,1);

Nm=size(iWatermark,2);

dWatermarkVector=round(reshape(dWatermark,Mm*Nm,1)./256);

file_name='key.bmp';

key=double(imread(file_name))./256;

rand('state',key);

h = waitbar(0,'Embedding, please wait...');

[cA1,cH1,cV1,cD1] = dwt2(dCover,'haar');

[cA2,cH2,cV2,cD2] = dwt2(cH1,'haar');

[cA3,cH3,cV3,cD3] = dwt2(cV1,'haar');

iLength = length(dWatermarkVector);

for (kk=1:iLength)

pn_sequence_h2=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_v2=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_h3=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_v3=round(2*(rand(Mc/4,Nc/4)-0.5));

if (dWatermarkVector(kk) == 0)

cH2=cH2+k*pn_sequence_h2;

cV2=cV2+k*pn_sequence_v2;

cH3=cH3+k*pn_sequence_h3;

cV3=cV3+k*pn_sequence_v3;

end

waitbar(kk/iLength,h);

end

close(h);

cH1 = idwt2(cA2,cH2,cV2,cD2,'haar',[Mc/2,Nc/2]);

cV1 = idwt2(cA3,cH3,cV3,cD3,'haar',[Mc/2,Nc/2]);

dEmbed = idwt2(cA1,cH1,cV1,cD1,'haar',[Mc,Nc]);

uEmbed = uint8(dEmbed);

imwrite(uEmbed,'dwt.bmp','bmp');

PSNR = psnr(dOrigCover,dEmbed);

elapsed_time=cputime-start_time

figure(1),imshow(iCover,[]);

title('Original Image');

figure(2),imshow(uEmbed,[]);

title(strcat('Watermarked Image',',PSNR=',num2str(PSNR)));

%% END------------------------------------------------------------------%%

%% ---------------------------------------------------------------------%%

% 基于小波变换的扩频图象数字水印--水印检测

%% ---------------------------------------------------------------------%%

start_time=cputime;

k=0.6;

file_name='~dwt.bmp';

iEmbed = imread(file_name);

dEmbed = double(iEmbed);

Mc=size(iEmbed,1);

Nc=size(iEmbed,2);

file_name='copyright.bmp';

iWatermark = imread(file_name);

dWatermark = double(iWatermark);

ddWatermark=dWatermark;

Mw=size(iWatermark,1);

Nw=size(iWatermark,2);

file_name='key.bmp';

key=double(imread(file_name))./256;

rand('state',key);

MessageVector=ones(1,Mw*Nw);

h = waitbar(0,'Detecting, please wait...');

[cA1,cH1,cV1,cD1] = dwt2(dEmbed,'haar');

[cA2,cH2,cV2,cD2] = dwt2(cH1,'haar');

[cA3,cH3,cV3,cD3] = dwt2(cV1,'haar');

iLength = length(MessageVector);

for (kk=1:iLength)

pn_sequence_h2=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_v2=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_h3=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_v3=round(2*(rand(Mc/4,Nc/4)-0.5));

correlation_h2(kk)=corr2(cH2,pn_sequence_h2);

correlation_v2(kk)=corr2(cV2,pn_sequence_v2);

correlation_h3(kk)=corr2(cH3,pn_sequence_h3);

correlation_v3(kk)=corr2(cV3,pn_sequence_v3);

correlation(kk)=(correlation_h2(kk)+correlation_h3(kk)+correlation_v2(kk)+correlation_v3(kk))/4;

waitbar(kk/iLength,h);

end

for (kk=1:iLength)

if (correlation(kk) mean(correlation))

MessageVector(kk)=0;

end

end

close(h);

DDetect=reshape(255*MessageVector,Mw,Nw);

Detect = uint8(DDetect);

imwrite(Detect,'watermark jiancai.bmp','bmp');

PSNR = psnr(ddWatermark,DDetect);

elapsed_time=cputime-start_time

CN=corr2(ddWatermark,DDetect);

figure(1),imshow(iWatermark,[]);

title('Original Watermark');

figure(2),imshow(Detect,[])

title(strcat('Detected Watermark',',PSNR=',num2str(PSNR),'CN=',num2str(CN)));

%% END------------------------------------------------------------------%%

% Function:计算PSNR

function PSNR = psnr(ImageA,ImageB)

if (size(ImageA,1) = size(ImageB,1)) or (size(ImageA,2) = size(ImageB,2))

error('ImageA ImageB');

dPSNR = 0;

return ;

end

M = size(ImageA,1);

N = size(ImageA,2);

d = 0 ;

for i = 1:M

for j = 1:N

d = d + (ImageA(i,j) - ImageB(i,j)).^2 ;

end

end

dPSNR = -10*log10(d/(255*255*M*N)) ;

return

3条大神的评论

  • avatar
    访客 2022-07-15 下午 07:43:42

    -----------------------------------%%%% ---------------------------------------------------------------------%%% 基于小波变换的扩频图象数字水印--水印检测%% ---

  • avatar
    访客 2022-07-15 下午 07:07:27

    double(xxx2);end%(5) 3*3窗口中值滤波:if type==5A='3*3窗口中值滤波攻击'xxx2=medfilt2(Y);%默认3*3Y= double(xxx2);end%%(6)裁减1/16if type==6A='裁减1/16攻击'for i=128-44

  • avatar
    访客 2022-07-15 上午 10:35:21

    tor);for (kk=1:iLength) pn_sequence_h2=round(2*(rand(Mc/4,Nc/4)-0.5)); pn_sequence_v2=

发表评论