MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Error using ==> Plus Matrix Dimensions must agree

Asked by shahosj on 25 Jul 2012

Here's the source code:

```    % Add a watermark to process
Q=input(' Enter to put the value of the shrinkage factor (recommended less than 1)：Q＝');
subplot(3,3,1)
imshow(RGB);
title('The original image');
subplot(3,3,2)
imshow(RGB(:,:,3));
title('B component');
subplot(3,3,3)
N=dct2(RGB(:,:,3));
imshow(log(abs(N)),[]),colorbar;
title('B component of the energy distribution');
subplot(3,3,4)
imshow(I);
title(' Grayscale watermark image ');
subplot(3,3,5)
M=dct2(I);
imshow(log(abs(M)),[]),colorbar;
title('Watermark energy distribution');
subplot(3,3,7)
J=M(1:128,1:128);
J(128:464,128:364)=0;
J=rot90(J);
J=rot90(J);
J(465:800,365:600)=0;
J=rot90(J);
J=rot90(J);
N=N+Q*J;
K=idct2(N);
RGB(:,:,3)=K;
imshow(RGB);
title(' Add to watermark images ');
subplot(3,3,8)
I=imcrop(RGB,[1 1 598 798]);
imshow(I);
subplot(3,3,9)
% Watermark extraction process
subplot(3,3,6)
J=RGB1(:,:,3);
X=J(1:799,1:599);
N=dct2(I(:,:,3));
M=dct2(X);
M=(N-M)/Q;
B=idct2(M(337:464,237:364));
Y=mat2gray(B);
imshow(Y);
title('Zoom extracted watermark image')```

After i run it, i got

```??? Error using ==> plus
Matrix dimensions must agree.
```
```Error in ==> ecp3 at 30
N=N+Q*J;
```

Can someone help me? Ive been doing this all night and seem cant find the solution, i appreciate any help. Thank u.

## 1 Comment

Walter Roberson on 29 Jul 2012

## Products

No products are associated with this question.

Answer by Walter Roberson on 25 Jul 2012

N is constructed from the RGB image you read in, but J is built to a fixed size (a subset of the image that is then extended by assigning 0 beyond those boundaries). There is no reason to expect that N and J will be the same size.

shahosj on 26 Jul 2012

Is there any way to make J to be as same size as N? Let say im using image from this http://www.petitcolas.net/fabien/watermarking/image_database/. Maybe u can help elaborate more on this. Thank u. If i have to make extra from J=M(1:128,1:128); to J=M(1:128,1:128,1:128); it will say it exceeds the matrix dimension.

Walter Roberson on 29 Jul 2012
```if size(M,3) < 128; M(1,1,128) = 0; end
```

and then you can access M(1:128,1:128,1:128) for whatever good that will do you. Usually the third dimension of an image is length 3, M(:,:,1) being the Red pane, M(:,:,2) being the Green pane, and M(:,:,3) being the Blue pane.

But that does not really apply here, as your M is not an image: your M is dct2() applied to a jpeg image. If the original image "g12" does not happen to be a grayscale image, it is not clear to me that the dct2() of it will be defined, as the dct2() documentation strongly implies a 2 dimensional output.

There must be any number of ways to make your J the same size as your M, and I could suggest several of them; the problem is to make the resulting J meaningful. For example, if you were to imresize() J to be the same size as M, would that be even slightly useful or would it be just GIGO

Answer by Walter Roberson on 10 Aug 2012