Error of extraction the text in DWT

조회 수: 4 (최근 30일)
Ei Sandar Tun
Ei Sandar Tun 2021년 1월 19일
답변: Suraj Kumar 2025년 4월 1일
Hello, I am a student and doing the project of image steganography in DWT. And I am having problem of extraction the message. I wish you could help me for the error.
and I think it has error in psnr and mse also. Thanks, I am really appreciate your help.
Below is my coding for embedding and extraction.
% Embedding%
clear all
%close all
%clc
[rawname, rawpath]= uigetfile (('*.jpg*.bmp'),'Select Image');
fullname = [rawpath rawname];
im = imread(fullname);
wname='haar';
[rawname, rawpath]= uigetfile (('*.txt'),'Select Text');
fullname = [rawpath rawname];
msg = fopen(fullname);
data=[];
for(i=1:length(msg))
d=msg(i)+0;
data=[data d];
end
imshow(im);
[cA1,cH1,cV1,cD1] = dwt2(im,wname);
dec1 = [cA1 cH1;
cV1 cD1
];
figure
imshow(uint8(dec1));
M=max(data);
data_norm=data/M;
n=length(data);
[x y]=size(cH1);
cH1(1,1)=-1*n/10;
cH1(1,2)=-1*M/10;
for(i=1:1:ceil(n/2))
cV1(i,y)=data_norm(i);
end
for(i=ceil(n/2)+1:1:n)
cD1(i,y)=data_norm(i);
end
CODED1=idwt2(cA1,cH1,cV1,cD1,wname);
figure
imshow(uint8(CODED1))
[x y]=size(cA1);
imshow(uint8(CODED1))
mse=abs(CODED1-double(im));
mse=mse.*mse;
mse=mean(mean(mse))
psnr= (255*255)/mse;
psnr=10*log10(psnr)
imwrite(uint8(CODED1),'Stego.bmp','bmp');
return
%Decoding%
im=imread('Stego.bmp');
[cA11,cH11,cV11,cD11] = dwt2(CODED1,wname);
data=[]
data_norm=[];
n=ceil(abs(cH11(1,1)*10));
M=ceil(abs(cH11(1,2)*10));
for(i=1:1:ceil(n/2))
data_norm(i)=cV11(i,y);
end
for(i=ceil(n/2)+1:1:n)
data_norm(i)=cD11(i,y);
end
data=ceil(data_norm*M)-1;
msg='';
for(i=1:length(data))
msg=strcat(msg,data(i));
end
msg

답변 (1개)

Suraj Kumar
Suraj Kumar 2025년 4월 1일
Hi Ei Sandar,
To resolve the issues that you are facing with extracting the message using the Discrete Wavelet Transform (DWT) in MATLAB, you can refer to the following steps:
1. You can use the "fread" function in MATLAB to read the text file into a character array, ensuring to capture the message accurately.
fileID = fopen(fullname, 'r');
msg = fread(fileID, '*char')';
fclose(fileID);
data = double(msg);
2. Then you can normalize the message data and embed it into the wavelet coefficients and make sure to store metadata such as message length and maximum value for accurate extraction later.
M = max(data);
data_norm = data / M;
n = length(data);
[x, y] = size(cH1);
cH1(1, 1) = -1 * n / 10;
cH1(1, 2) = -1 * M / 10;
for i = 1:ceil(n/2)
cV1(i, y) = data_norm(i);
end
for i = ceil(n/2)+1:n
cD1(i, y) = data_norm(i);
end
CODED1 = idwt2(cA1, cH1, cV1, cD1, wname);
3. Then you can calculate these metrics to evaluate the quality of the stego image compared to the original.
mse = mean(mean((double(im) - CODED1).^2));
psnr = 10 * log10((255^2) / mse);
fprintf('MSE: %f\n', mse);
fprintf('PSNR: %f\n', psnr);
4. Finally you can use the stored metadata to accurately extract and reconstruct the original message from the stego image.
[cA11, cH11, cV11, cD11] = dwt2(im, wname);
data_norm = [];
n = ceil(abs(cH11(1, 1) * 10));
M = ceil(abs(cH11(1, 2) * 10));
for i = 1:ceil(n/2)
data_norm(i) = cV11(i, y);
end
for i = ceil(n/2)+1:n
data_norm(i) = cD11(i, y);
end
data = ceil(data_norm * M) - 1;
msg = char(data);
disp('Extracted Message:');
disp(msg);
For more information on the functions "fread" or "dwt2" in MATLAB, you can refer to the following documentation links:
Hope this resolves your issue!

카테고리

Help CenterFile Exchange에서 Wavelet Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by