matlab中图像的PSNR

PSNR of image in matlab

我对原始图像和加密图像之间的峰值信噪比 (PSNR) 感到困惑。据我所知,更高的 PSNR 值意味着更好的图像质量。我试着测试并计算了PSNR值。我在文本隐写术中使用了 LSB 技术。

  1. 我尝试将 100 个字符嵌入到图像中。结果为 69.9696 dB。
  2. 我尝试将 5 个字符嵌入到图像中。结果为 68 dB。

现在,我的想法是:

谁能告诉我或纠正我的错误?

------附加编码------

Str = 'after this, I tried calculate the PSNR value with original image and stego image. 100 character which is read from file is embedded into image, higher PSNR value. 5 character, less PSNR value.';%many character
%Str = 'a';   %one character 

Str=uint8(Str);    %converting to 8 bit numbers for proper calculation
fprintf('%d ', Str);
fprintf('\n');
stringLength = length(Str);


x=imread('lena.bmp');   %reading the image file
x=uint8(x);                %conversion to 8 bit
[x_row,x_col]=size(x);


numPixelsInImage = numel(x);
bitsPerLetter = 7;  % For ASCII, this is 7.
numPixelsNeededForString = stringLength * bitsPerLetter;

binaryAsciiString = dec2bin(Str)'
whos binaryAsciiString
binaryAsciiString = binaryAsciiString(:)'

 stegoImage = x;
 stegoImage(1:numPixelsInImage) = bitset(stegoImage(1:numPixelsInImage), 1, 0);
 oneIndexes = find(binaryAsciiString == '1'); 
 stegoImage(oneIndexes) = bitset(stegoImage(oneIndexes), 1, 1);



 imwrite(uint8(stegoImage),'stego123.bmp') 

fprintf('\nPSNR: %9.7f dB\n\n', psnr(x,stegoImage));

在此之后,我尝试用原始图像和隐写图像计算PSNR值。从文件中读取的 100 个字符嵌入到图像中,PSNR 值更高。 5个字符,减去PSNR值。

这就是我感到困惑的原因。

---这是我的PSNR代码---

function [PSNR,mse]=psnr(X,Y)
% function [PSNR,mse]=psnr(X,Y)
% Peak signal to noise ratio of the difference between images and the
%mean square error
% If the second input Y is missing then the PSNR and MSE of X itself
% becomes the output (as if Y=0).
if nargin<2, D=X;
else
if any(size(X)~=size(Y)), error('The input size is not equal to each other!'); end
D=X-Y;
end
mse=sum(D(:).*D(:))/prod(size(X));
PSNR=10*log10(255^2/mse);

我只是调用PSNR函数,打印原图和stego图像的PSNR值。

我嵌入的许多字符,我得到 51.1687256 分贝。 我嵌入的一个字符,我得到 51.1578686 dB。

能告诉我为什么吗?

您的 pnsr 函数有错误。两个输入都是uint8,这限制了0-255范围内的所有值。这可能是 D=X-Y.

的问题
>> uint8(0) - uint8(1)
0
>> double(0) - double(1)
-1

更改为 D = double(X) - double(Y) 将为您提供长字符串和 1 字母字符串的正确值,分别为 51.1576 dB 和 51.1578 dB。

但是,您的算法不是最优的。在嵌入位之前,将 每个 像素的 LSB 设置为 0。您有效地修改了多达 262144 个像素,而您的消息却短得多。这大大降低了 PSNR,并解释了为什么 7 位和 1536 位长的消息的值如此相似。相反,您应该只修改使用 numPixelsNeededForString 嵌入消息所需的最少像素数。或者更紧凑,

cover = imread('lena512.png');
Str = 'after this, I tried calculate the PSNR value with original image and stego image. 100 character which is read from file is embedded into image, higher PSNR value. 5 character, less PSNR value.';
%Str = Str(1:1);

bits = dec2bin(Str, 7)';
bits = bits(:);

stego = cover;
stego(1:numel(bits)) = bitset(stego(1:numel(bits)), 1, str2num(bits)');

psnr(cover, stego)

这样做将为长字符串提供 73.9530 dB 的 PSNR,为 1 个字母的字符串提供 95.3265 dB。