MATLAB: How to make an image from extracted Bit planes

watermarking

Hi, I'm new here. And beginner for MATLAB.
I've written a code for watermarking in spatial domain. I've applied the technique of replacing LSB of original image with watermark. Now, my question is, how to save these planes in a single image? It gives me error 'Matrix dimensions must match'… Kindly help…
A=imread('original.png'); C=imread('watermark.bmp');
B1 = bitget(A,1)*2^0; B2 = bitget(A,2)*2^1 ; B3 = bitget(A,3)*2^2 ; B4 = bitget(A,4)*2^3 ; B5 = bitget(A,5)*2^4 ; B6 = bitget(A,6)*2^5 ; B7 = bitget(A,7)*2^6 ; B8 = bitget(A,8)*2^7 ;
figure,subplot(3,3,1),imshow(B1); subplot(3,3,2),imshow(B2); subplot(3,3,3),imshow(B3); subplot(3,3,4),imshow(B4); subplot(3,3,5),imshow(B5); subplot(3,3,6),imshow(B6); subplot(3,3,7),imshow(B7); subplot(3,3,8),imshow(B8);
C1 = bitget(C,1)*2^0; C2 = bitget(C,2)*2^1 ; C3 = bitget(C,3)*2^2 ; C4 = bitget(C,4)*2^3 ; C5 = bitget(C,5)*2^4 ; C6 = bitget(C,6)*2^5 ; C7 = bitget(C,7)*2^6 ; C8 = bitget(C,8)*2^7 ;
figure,subplot(3,3,1),imshow(C1); subplot(3,3,2),imshow(C2); subplot(3,3,3),imshow(C3); subplot(3,3,4),imshow(C4); subplot(3,3,5),imshow(C5); subplot(3,3,6),imshow(C6); subplot(3,3,7),imshow(C7); subplot(3,3,8),imshow(C8);
M1 = bitget(C,8)*2^0; M2 = bitget(A,2)*2^1 ; M3 = bitget(A,3)*2^2 ; M4 = bitget(A,4)*2^3 ; M5 = bitget(A,5)*2^4 ; M6 = bitget(A,6)*2^5 ; M7 = bitget(A,7)*2^6 ; M8 = bitget(A,8)*2^7 ;
figure,subplot(3,3,1),imshow(M1); subplot(3,3,2),imshow(M2); subplot(3,3,3),imshow(M3); subplot(3,3,4),imshow(M4); subplot(3,3,5),imshow(M5); subplot(3,3,6),imshow(M6); subplot(3,3,7),imshow(M7); subplot(3,3,8),imshow(M8);
M=M1+M2+M3+M4+M5+M6+M7+M8; figure,imshow(M),title('Final Image');

Best Answer

It's because your watermark is not the same size as your base image. Instead of your "M=...." line, replace it with this code:
[rowsC columnsC] = size(C)
% Define location of final watermark.
topRow = 10;
leftColumn = 20;
bottomRow = topRow + rowsC - 1;
rightColumn = leftColumn + columnsC - 1;
% Get an image as big as the original
canvass = zeros(size(A), 'uint8');
canvass(topRow:bottomRow, leftColumn:rightColumn) = C8;
% Now they're all the same size so you can add them.
M = canvass +M2+M3+M4+M5+M6+M7+M8;