Effacer les filtres
Effacer les filtres

How to correct matrix dimensions and recombine blocks into original image?

2 vues (au cours des 30 derniers jours)
Dear sir, Following is the code I wrote for dividing a color image (512x512) into 4 blocks and watermarking 4 different images into 4 blocks using dwt-svd. It shows an error saying :"Error using + Matrix dimensions must agree." after displaying the 1st original image and 1st watermark image. Kindly correct my mistake and also please tell me how to recombine the watermarked blocks? Regards Yashi Gautam
% host 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) );
i1=img4x4{1,1}; %upper left
[h1_LL,h1_LH,h1_HL,h1_HH]=dwt2(i1,'haar');
img_1=h1_LL;
red_1=img_1(:,:,1);
green_1=img_1(:,:,2);
blue_1=img_1(:,:,3);
[U_img1r1,S_img1r1,V_img1r1]= svd(red_1);
[U_img1g1,S_img1g1,V_img1g1]= svd(green_1);
[U_img1b1,S_img1b1,V_img1b1]= svd(blue_1);
%watermark imagee
w1=imread('fruits.jpg');
wtr_img1=imresize(w1, 1/4);
figure; imshow(wtr_img1);
title('Watermark image');
[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);
[U_img1r2,S_img1r2,V_img1r2]= svd(red1);
[U_img1g2,S_img1g2,V_img1g2]= svd(green1);
[U_img1b2,S_img1b2,V_img1b2]= svd(blue1);
% PSNR calculation
P=PSNRCalc('lena.jpg','fruits.jpg');
fprintf('\n%s \t\t\t\t\t\t\t %7.4f dB\n','The PSNR Value of Noiseless Image is ', P)
% watermarking by SVD
S_wimg1r=S_img1r1+(0.10*S_img1r2);
S_wimg1g=S_img1g1+(0.10*S_img1g2);
S_wimg1b=S_img1b1+(0.10*S_img1b2);
wimg1r = U_img1r1*S_wimg1r*V_img1r1';
wimg1g = U_img1g1*S_wimg1g*V_img1g1';
wimg1b = U_img1b1*S_wimg1b*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');
figure; imshow(uint8(rgb1));
title('Watermarked Image 1');
% 2nd image i2=img4x4{1,1}; %upper left
[h2_LL,h2_LH,h2_HL,h2_HH]=dwt2(i2,'haar');
img_2=h2_LL;
red_2=img_2(:,:,1);
green_2=img_2(:,:,2);
blue_2=img_2(:,:,3);
[U_img2r1,S_img2r1,V_img2r1]= svd(red_2);
[U_img2g1,S_img2g1,V_img2g1]= svd(green_2);
[U_img2b1,S_img2b1,V_img2b1]= svd(blue_2);
%watermark image 2
w2=imread('fruits.jpg');
wtr_img2=imresize(w2, 1/4);
figure; imshow(wtr_img2);
title('Watermark image 2');
[w2_LL,w2_LH,w2_HL,w2_HH]=dwt2(wtr_img2,'haar');
img_wat2=w_LL;
red2=img_wat2(:,:,1);
green2=img_wat2(:,:,2);
blue2=img_wat2(:,:,3);
[U_img2r2,S_img2r2,V_img2r2]= svd(red2);
[U_img2g2,S_img2g2,V_img2g2]= svd(green2);
[U_img2b2,S_img2b2,V_img2b2]= svd(blue2);
% PSNR calculation
P=PSNRCalc('lena.jpg','fruits.jpg');
fprintf('\n%s \t\t\t\t\t\t\t %7.4f dB\n','The PSNR Value of Noiseless Image is ', P)
% watermarking by SVD
S_wimg2r=S_img2r1+(0.10*S_img2r2);
S_wimg2g=S_img2g1+(0.10*S_img2g2);
S_wimg2b=S_img2b1+(0.10*S_img2b2);
wimg2r = U_img2r1*S_wimg2r*V_img2r1';
wimg2g = U_img2g1*S_wimg2g*V_img2g1';
wimg2b = U_img2b1*S_wimg2b*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');
figure; imshow(uint8(rgb2));
title('Watermarked Image 2');
%3rd image
i3=img4x4{1,1}; %upper left
[h3_LL,h3_LH,h3_HL,h3_HH]=dwt2(i3,'haar');
img_3=h3_LL;
red_3=img_3(:,:,1);
green_3=img_3(:,:,2);
blue_3=img_3(:,:,3);
[U_img3r1,S_img3r1,V_img3r1]= svd(red_3);
[U_img3g1,S_img3g1,V_img3g1]= svd(green_3);
[U_img3b1,S_img3b1,V_img3b1]= svd(blue_3);
%watermark image 3
w3=imread('fruits.jpg');
wtr_img3=imresize(w3, 1/4);
figure; imshow(wtr_img3);
title('Watermark image');
[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);
[U_img3r2,S_img3r2,V_img3r2]= svd(red3);
[U_img3g2,S_img3g2,V_img3g2]= svd(green3);
[U_img3b2,S_img3b2,V_img3b2]= svd(blue3);
% PSNR calculation
P=PSNRCalc('lena.jpg','fruits.jpg');
fprintf('\n%s \t\t\t\t\t\t\t %7.4f dB\n','The PSNR Value of Noiseless Image is ', P)
% watermarking by SVD
S_wimg3r=S_img3r1+(0.10*S_img3r2);
S_wimg3g=S_img3g1+(0.10*S_img3g2);
S_wimg3b=S_img3b1+(0.10*S_img3b2);
wimg3r = U_img3r1*S_wimg3r*V_img3r1';
wimg3g = U_img3g1*S_wimg3g*V_img3g1';
wimg3b = U_img3b1*S_wimg3b*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');
figure; imshow(uint8(rgb3));
title('Watermarked Image 3');
%4rth image
i4=img4x4{1,1}; %upper left
[h4_LL,h4_LH,h4_HL,h4_HH]=dwt2(i3,'haar');
img_4=h4_LL;
red_4=img_4(:,:,1);
green_4=img_4(:,:,2);
blue_4=img_4(:,:,3);
[U_img4r1,S_img4r1,V_img4r1]= svd(red_4);
[U_img4g1,S_img4g1,V_img4g1]= svd(green_4);
[U_img4b1,S_img4b1,V_img4b1]= svd(blue_4);
%watermark image 4
w4=imread('fruits.jpg');
wtr_img4=imresize(w4, 1/4);
figure; imshow(wtr_img4);
title('Watermark image');
[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);
[U_img4r2,S_img4r2,V_img4r2]= svd(red4);
[U_img4g2,S_img4g2,V_img4g2]= svd(green4);
[U_img4b2,S_img4b2,V_img4b2]= svd(blue4);
% PSNR calculation
P=PSNRCalc('lena.jpg','fruits.jpg');
fprintf('\n%s \t\t\t\t\t\t\t %7.4f dB\n','The PSNR Value of Noiseless Image is ', P)
% watermarking by SVD
S_wimg4r=S_img4r1+(0.10*S_img4r2);
S_wimg4g=S_img4g1+(0.10*S_img4g2);
S_wimg4b=S_img4b1+(0.10*S_img4b2);
wimg4r = U_img4r1*S_wimg4r*V_img4r1';
wimg4g = U_img4g1*S_wimg4g*V_img4g1';
wimg4b = U_img4b1*S_wimg4b*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');
figure; imshow(uint8(rgb4));
title('Watermarked Image 4');

Réponses (1)

Constantino Carlos Reyes-Aldasoro
It is hard to follow your code without the data, but still the error must be that when you are trying to sum your image and the watermark, the dimensions are not the same, something like this:
a = ones(512,512);
b= ones(511,511);
c=a+b;
Arrays have incompatible sizes for this operation.
Thus, check in which line you have the error and check the dimensions of the variables you have in that line. That will tell you the source of the error and how to fix it.

Community Treasure Hunt

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

Start Hunting!

Translated by