Im = imread('szy1.jpg'); In = rgb2gray(Im); I1 = bitget(In, 1); one = I1(1:200,1:200); one = im2bw(uint8(one) * 255); figure; subplot(2,3,3) imshow(one); title('第一层图像'); I = In - (min(In(:))); [M,N] = size(I); subplot(2,3,1) imshow(I); title('载体图像'); message=imread('watermark.bmp');[Mw,Nw]=size(message); subplot(2,3,2) imshow(message);title('原始水印'); embed=I; for i=1:Mw for j=1:Nw embed(i,j)=embed(i,j)-mod(embed(i,j),2)+uint8(message(i,j)); end end subplot(2,3,4) imshow(embed);title('含水印图像');drawnow; imwrite(embed,'ok.bmp') I = imread('ok.bmp'); I0 = bitget(I, 1); waterMark = I0(1:32,1:32); waterMark = im2bw(uint8(waterMark) * 255); subplot(2,3,5) imshow(waterMark); title('提取图像'); waterMark = I0(1:200,1:200); waterMark = im2bw(uint8(waterMark) * 255); subplot(2,3,6) imshow(waterMark); title('第一层加水印图像'); I_double = im2double(I); Im_double = im2double(Im); MSE = mean((I_double(:) - Im_double(:)).^2); PSNR = 10 * log10(1/MSE); fprintf('PSNR = %f dB\n', PSNR); NC = sum(waterMark(:) == one(:)) / numel(one); fprintf('NC = %f\n', NC); diff = abs(waterMark - one); BER = sum(diff(:)) / numel(diff); fprintf('BER = %f\n', BER);修改,矩阵维度不一样
时间: 2024-03-22 12:40:56 浏览: 63
如果原始水印图像和第一层加水印的图像的尺寸不同,需要修改提取水印的代码。假设原始水印图像为 $M_1 \times N_1$,第一层加水印的图像为 $M_2 \times N_2$,可以按照下面的代码进行修改:
```
I0 = bitget(I, 1);
waterMark = I0(1:M1,1:N1); % 提取水印
waterMark = im2bw(uint8(waterMark) * 255);
subplot(2,3,5); imshow(waterMark); title('提取图像');
waterMark = I0(1:M2,1:N2); % 提取第一层加水印的水印
waterMark = im2bw(uint8(waterMark) * 255);
subplot(2,3,6); imshow(waterMark); title('第一层加水印图像的水印');
```
其中,$M_1,N_1$ 是原始水印图像的尺寸,$M_2,N_2$ 是第一层加水印的图像的尺寸。
相关问题
Im = imread('szy1.jpg'); In = rgb2gray(Im); I1 = bitget(In, 1); one = I1(1:200,1:200); one = im2bw(uint8(one) * 255); figure; subplot(2,3,3) imshow(one); title('第一层图像'); I = In - (min(In(:))); [M,N] = size(I); subplot(2,3,1) imshow(I); title('载体图像'); message=imread('watermark.bmp');[Mw,Nw]=size(message); subplot(2,3,2) imshow(message);title('原始水印'); embed=I; for i=1:Mw for j=1:Nw embed(i,j)=embed(i,j)-mod(embed(i,j),2)+uint8(message(i,j)); end end subplot(2,3,4) imshow(embed);title('含水印图像');drawnow; imwrite(embed,'ok.bmp') I = imread('ok.bmp'); I0 = bitget(I, 1); waterMark = I0(1:32,1:32); waterMark = im2bw(uint8(waterMark) * 255); subplot(2,3,5) imshow(waterMark); title('提取图像'); waterMark = I0(1:200,1:200); waterMark = im2bw(uint8(waterMark) * 255); subplot(2,3,6) imshow(waterMark); title('第一层加水印图像');计算含水印图像的 PSNR;计算提取水印的 NC和BER
计算含水印图像的PSNR:
```matlab
Im = imread('szy1.jpg');
In = rgb2gray(Im);
I1 = bitget(In, 1);
one = I1(1:200,1:200);
one = im2bw(uint8(one) * 255);
I = In - (min(In(:)));
message = imread('watermark.bmp');
[Mw,Nw]=size(message);
embed=I;
for i=1:Mw
for j=1:Nw
embed(i,j)=embed(i,j)-mod(embed(i,j),2)+uint8(message(i,j));
end
end
imwrite(embed,'ok.bmp')
I = imread('ok.bmp');
[M, N] = size(I);
MSE = sum(sum((I - embed).^2)) / (M * N);
PSNR = 10 * log10(255^2 / MSE)
```
计算提取水印的NC:
```matlab
waterMark = I0(1:32,1:32);
waterMark = im2bw(uint8(waterMark) * 255);
NC = sum(sum(waterMark == message)) / (32 * 32)
```
计算提取水印的BER:
```matlab
waterMark = I0(1:200,1:200);
waterMark = im2bw(uint8(waterMark) * 255);
BER = sum(sum(waterMark ~= one)) / (200 * 200)
```
需要注意的是,上述代码中的变量`I0`没有给出,需要根据实际情况进行修改,以便能够正确运行。
阅读全文