Clear Filters
Clear Filters

Unable to perform assignment because the size of the left side is 128-by-384 and the size of the right side is 128-by-128.

2 views (last 30 days)
oriImage = imread('lena.bmp');
Watermark = imread('new_logo128.bmp');
[M,N]=size(oriImage);
[M1,N1] = size(Watermark);
watermarkImage = zeros(M,N);
for i=1:M/M1
for j = 1:N/N1
watermarkImage((i-1)*M1+1:(i-1)*M1+M1,(j-1)*N1+1:(j-1)*N1+N1)=Watermark(:,:,1);
end
end
figure
subplot(2,3,1);imshow(oriImage);title("original Image")
subplot(2,3,2);imshow(watermarkImage);title("watermark Image")
bitplane=1;
for i=1:M
for j = 1:N
watermarkImage(i,j) = bitset(oriImage(i,j),bitplane,watermarkImage(i,j));
end
end
subplot(2,3,3);imshow(watermarkImage*255);title('watermark image')

Answers (1)

DGM
DGM on 15 May 2024
I'm guessing here, since I don't know what your images are, but it appears that they are both RGB images. In either case, you're misusing size(). If you request K scalar output arguments from size(), the last output is not the size of the Kth dimension. It's the product of the sizes of the Kth dimension and any trailing dimensions. Unless you have a need for such behavior, always discard the last output argument, or don't request scalar outputs from size() that way.
% a 384x512x3 RGB image
A = imread('peppers.png');
% scalar outputs will mislead you unless you know what they mean
[h,w] = size(A)
h = 384
w = 1536
% if you only need the height and width, request and discard the last output
[h,w,~] = size(A)
h = 384
w = 512
I prefer to use vector outputs, but that requires attention as well.
% sz is a variable-length vector, so it's hard to use programmatically
sz = size(A)
sz = 1x3
384 512 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% in newer versions, you can explicitly get the dimensions you need all at once
% that way the vector length is always fixed
sz = size(A,1:2)
sz = 1x2
384 512
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Tags

Community Treasure Hunt

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

Start Hunting!