How can this code explain the Error in psnr?

조회 수: 1 (최근 30일)
yashi gautam
yashi gautam 2016년 3월 7일
편집: DGM 2023년 5월 12일
Sir, I have calculated psnr for rgb colored watermarked image to be 30 something. When i use a similar method on YCrCb image. the psnr comes out to be 10 something by varying only the Y component of the image. Can u guide me the error in my code? here is my code..
% peppers image
org_img= imread('lena.jpg');
figure;
imshow(org_img);
title('Original image');
% division imto blocks
img4x4 = mat2cell( org_img, size(org_img,1)/2 * ones(1,2), size(org_img,2)/2 * ones(1,2), size(org_img,3) );
%upper left
[h1_LL,h1_LH,h1_HL,h1_HH]=dwt2(img4x4{1,1},'haar');
img_1=h1_LL;
red_1=img_1(:,:,1);
green_1=img_1(:,:,2);
blue_1=img_1(:,:,3);
Y_1 = 0.299 * red_1 + 0.587 * green_1 + 0.114 * blue_1;
Cb_1 = 0.596 * red_1 - 0.275 * green_1 - 0.321 * blue_1;
Cr_1 = 0.212 * red_1 - 0.523 * green_1 - 0.311 * blue_1;
[U_img1r1,S_img1r1,V_img1r1]= svd(Y_1);
[U_img1g1,S_img1g1,V_img1g1]= svd(Cb_1);
[U_img1b1,S_img1b1,V_img1b1]= svd(Cr_1);
%watermark imagee
w1=imread('fruits.jpg');
wtr_img1=imresize(w1, 1/2);
figure;
[w1_LL,w1_LH,w1_HL,w1_HH]=dwt2(wtr_img1,'haar');
img_wat1=w1_LL;
red1=img_wat1(:,:,1);
green1=img_wat1(:,:,2);
blue1=img_wat1(:,:,3);
Y1 = 0.299 * red1 + 0.587 * green1 + 0.114 * blue1;
Cb1 = 0.596 * red1 - 0.275 * green1 - 0.321 * blue1;
Cr1 = 0.212 * red1 - 0.523 * green1 - 0.311 * blue1;
[U_img1r2,S_img1r2,V_img1r2]= svd(Y1);
[U_img1g2,S_img1g2,V_img1g2]= svd(Cb1);
[U_img1b2,S_img1b2,V_img1b2]= svd(Cr1);
% watermarking by SVD
S_wimg1r=S_img1r1+(0.05*S_img1r2);
wimg1r = U_img1r1*S_wimg1r*V_img1r1';
wimg1g = U_img1g1*S_img1g1*V_img1g1';
wimg1b = U_img1b1*S_img1b1*V_img1b1';
wimg1=cat(3,wimg1r,wimg1g,wimg1b);
newimage1_LL=wimg1;
%output
rgb1=idwt2(newimage1_LL,h1_LH,h1_HL,h1_HH,'haar');
imwrite(uint8(rgb1),'Watermarked1.jpg');
img4x4{1,1} =rgb1;
% 2nd image
%upper right
[h2_LL,h2_LH,h2_HL,h2_HH]=dwt2(img4x4{1,2},'haar');
img_2=h2_LL;
red_2=img_2(:,:,1);
green_2=img_2(:,:,2);
blue_2=img_2(:,:,3);
Y_2 = 0.299 * red_2 + 0.587 * green_2 + 0.114 * blue_2;
Cb_2 = 0.596 * red_2 - 0.275 * green_2 - 0.321 * blue_2;
Cr_2 = 0.212 * red_2 - 0.523 * green_2 - 0.311 * blue_2;
[U_img2r1,S_img2r1,V_img2r1]= svd(Y_2);
[U_img2g1,S_img2g1,V_img2g1]= svd(Cb_2);
[U_img2b1,S_img2b1,V_img2b1]= svd(Cr_2);
%watermark image 2
w2=imread('Baboon.jpe');
wtr_img2=imresize(w2, 1/2);
figure;
[w2_LL,w2_LH,w2_HL,w2_HH]=dwt2(wtr_img2,'haar');
img_wat2=w2_LL;
red2=img_wat2(:,:,1);
green2=img_wat2(:,:,2);
blue2=img_wat2(:,:,3);
Y2 = 0.299 * red2 + 0.587 * green2 + 0.114 * blue2;
Cb2 = 0.596 * red2 - 0.275 * green2 - 0.321 * blue2;
Cr2 = 0.212 * red2 - 0.523 * green2 - 0.311 * blue2;
[U_img2r2,S_img2r2,V_img2r2]= svd(Y2);
[U_img2g2,S_img2g2,V_img2g2]= svd(Cb2);
[U_img2b2,S_img2b2,V_img2b2]= svd(Cr2);
% PSNR calculation
% watermarking by SVD
S_wimg2r=S_img2r1+(0.05*S_img2r2);
wimg2r = U_img2r1*S_wimg2r*V_img2r1';
wimg2g = U_img2g1*S_img2g1*V_img2g1';
wimg2b = U_img2b1*S_img2b1*V_img2b1';
wimg2=cat(3,wimg2r,wimg2g,wimg2b);
newimage2_LL=wimg2;
%output
rgb2=idwt2(newimage2_LL,h2_LH,h2_HL,h2_HH,'haar');
imwrite(uint8(rgb2),'Watermarked2.jpg');
img4x4{1,2} = rgb2;
%3rd image
%lower left
[h3_LL,h3_LH,h3_HL,h3_HH]=dwt2(img4x4{2,1},'haar');
img_3=h3_LL;
red_3=img_3(:,:,1);
green_3=img_3(:,:,2);
blue_3=img_3(:,:,3);
Y_3 = 0.299 * red_3 + 0.587 * green_3 + 0.114 * blue_3;
Cb_3 = 0.596 * red_3 - 0.275 * green_3 - 0.321 * blue_3;
Cr_3 = 0.212 * red_3 - 0.523 * green_3 - 0.311 * blue_3;
[U_img3r1,S_img3r1,V_img3r1]= svd(Y_3);
[U_img3g1,S_img3g1,V_img3g1]= svd(Cb_3);
[U_img3b1,S_img3b1,V_img3b1]= svd(Cr_3);
%watermark image 3
w3=imread('peppers.jpg');
wtr_img3=imresize(w3, 1/2);
figure;
[w3_LL,w3_LH,w3_HL,w3_HH]=dwt2(wtr_img3,'haar');
img_wat3=w3_LL;
red3=img_wat3(:,:,1);
green3=img_wat3(:,:,2);
blue3=img_wat3(:,:,3);
Y3 = 0.299 * red3 + 0.587 * green3 + 0.114 * blue3;
Cb3 = 0.596 * red3 - 0.275 * green3 - 0.321 * blue3;
Cr3 = 0.212 * red3 - 0.523 * green3 - 0.311 * blue3;
[U_img3r2,S_img3r2,V_img3r2]= svd(Y3);
[U_img3g2,S_img3g2,V_img3g2]= svd(Cb3);
[U_img3b2,S_img3b2,V_img3b2]= svd(Cr3);
% watermarking by SVD
S_wimg3r=S_img3r1+(0.05*S_img3r2);
wimg3r = U_img3r1*S_wimg3r*V_img3r1';
wimg3g = U_img3g1*S_img3g1*V_img3g1';
wimg3b = U_img3b1*S_img3b1*V_img3b1';
wimg3=cat(3,wimg3r,wimg3g,wimg3b);
newimage3_LL=wimg3;
%output
rgb3=idwt2(newimage3_LL,h3_LH,h3_HL,h3_HH,'haar');
imwrite(uint8(rgb3),'Watermarked3.jpg');
img4x4{2,1} = rgb3;
%4rth image
%lower right
[h4_LL,h4_LH,h4_HL,h4_HH]=dwt2(img4x4{2,2},'haar');
img_4=h4_LL;
red_4=img_4(:,:,1);
green_4=img_4(:,:,2);
blue_4=img_4(:,:,3);
Y_4 = 0.299 * red_4 + 0.587 * green_4 + 0.114 * blue_4;
Cb_4 = 0.596 * red_4 - 0.275 * green_4 - 0.321 * blue_4;
Cr_4 = 0.212 * red_4 - 0.523 * green_4 - 0.311 * blue_4;
[U_img4r1,S_img4r1,V_img4r1]= svd(Y_4);
[U_img4g1,S_img4g1,V_img4g1]= svd(Cb_4);
[U_img4b1,S_img4b1,V_img4b1]= svd(Cr_4);
%watermark image 4
w4=imread('F16.jpe');
wtr_img4=imresize(w4, 1/2);
figure;
[w4_LL,w4_LH,w4_HL,w4_HH]=dwt2(wtr_img4,'haar');
img_wat4=w4_LL;
red4=img_wat4(:,:,1);
green4=img_wat4(:,:,2);
blue4=img_wat4(:,:,3);
Y4 = 0.299 * red4 + 0.587 * green4 + 0.114 * blue4;
Cb4 = 0.596 * red4 - 0.275 * green4 - 0.321 * blue4;
Cr4 = 0.212 * red4 - 0.523 * green4 - 0.311 * blue4;
[U_img4r2,S_img4r2,V_img4r2]= svd(Y4);
[U_img4g2,S_img4g2,V_img4g2]= svd(Cb4);
[U_img4b2,S_img4b2,V_img4b2]= svd(Cr4);
% watermarking by SVD
S_wimg4r=S_img4r1+(0.05*S_img4r2);
wimg4r = U_img4r1*S_wimg4r*V_img4r1';
wimg4g = U_img4g1*S_img4g1*V_img4g1';
wimg4b = U_img4b1*S_img4b1*V_img4b1';
wimg4=cat(3,wimg4r,wimg4g,wimg4b);
newimage4_LL=wimg4;
%output
rgb4=idwt2(newimage4_LL,h4_LH,h4_HL,h4_HH,'haar');
imwrite(uint8(rgb4),'Watermarked4.jpg');
img4x4{2,2} = rgb4;
%reconstruction of watermarked image
recomb_img= cell2mat(img4x4);
imwrite(uint8(recomb_img),'Recombined.jpg');
%PSNR of whole image
% PSNR calculation
image1= imread('lena.jpg');
image2= imread('Recombined.jpg');
figure(1);
imshow(image1); title('Original image');
figure(2);
imshow(image2); title('Watermarked image');
red_img= image2(:,:,1);
green_img= image2(:,:,2);
blue_img= image2(:,:,3);
Y_img = 0.299 * red_img + 0.587 * green_img + 0.114 * blue_img;
Cb_img = 0.596 * red_img - 0.275 * green_img - 0.321 * blue_img;
Cr_img = 0.212 * red_img - 0.523 * green_img - 0.311 * blue_img;
figure(3);
imwrite(uint8(Y_img), 'Y_comp.jpg');
imshow(Y_img);
title('Y component');
figure(4);
imshow(Cb_img);
title('Cb component');
imwrite(uint8(green_img), 'Cb_comp.jpg');
figure(5);
imshow(Cr_img);
title('Cr component');
imwrite(uint8(blue_img), 'Cr_comp.jpg');
[row,col] = size(image1)
size_host = row*col;
o_double = double(image1);
w_double = double(image2);
s=0;
for j = 1:size_host; % the size of the original image
s = s+(w_double(j) - o_double(j))^2 ;
end
mes=s/size_host;
psnr =10*log10((255)^2/mes);
rmse= sqrt(mes);
display 'Value of',psnr
display 'Value of',mes
display 'Value of', rmse

답변 (1개)

DGM
DGM 2023년 5월 12일
편집: DGM 2023년 5월 12일
I can't run this myself without the wavelet toolbox, and it's a wall of numbered variables and missing input files, so I'm not terribly inclined to dig deep. That said, I blindly replaced all the missing files with peppers.png and ran it in the forum editor.
Let's just say I'm relatively surprised that you'd get a PSNR as high as 10 with 84% of all pixels being pushed way out of gamut and conspicuously truncated. You can tell how far OOG things were just from visual inspection of how deep the luma inversions are.
There are three things to point out here:
This is not YCbCr. This is YIQ. I don't know why everyone insists on using all these terms interchangably. They are related, and the differences might be inconsequential so long as they're considered, but confounding language does not benefit communication. In this specific case, the difference really doesn't matter.
Everything is being pushed out of gamut. Why? In what direction? I don't have the time or bandwidth to discern that via the forum editor. To what degree? A big degree. Like I said, over 80% of pixels were pushed far OOG in the sample I used. What's that mean? That means that after conversion back to RGB, casting using uint8() truncates supramaximal and subminimal RGB values, resulting in points which are no longer at the intended positions in luma or chroma. They're at some wildly different place. See this answer for an illustration of what happens when things get truncated.
Even if points weren't pushed out of gamut, and even if no changes were made to the image, you can usually expect some small error contribution from the conversions alone. See the comments in this thread and the answer about lossless conversion using YCoCg.

Community Treasure Hunt

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

Start Hunting!

Translated by