I am getting error in this code at line 13 '', 'Index in position 2 exceeds array bounds (must not exceed 512). Looking for the solution.Thanks in advance
clear all;
M=512;
N=512;
K=8;
A=imread('lena2.jpg');
A=rgb2gray(A);
A=imresize(A,[512,512]);
b=imread('Watermarked_Image.jpg');
for p=1:N
for q=1:N
x=(p-1)*K+1;
y=(q-1)*K+1;
BLOCK1=A(x:x+K-1,y:y+K-1);
BLOCK2=b(x:x+K-1,y:y+K-1);
BLOCK1=idct2(BLOCK1);
BLOCK2=idct2(BLOCK2);
if BLOCK1(1,1)~=0
a=(BLOCK2(1,1)/BLOCK1(1,1))-1;
if a<0
W(p,q)=0;
else
W(p,q)=1;
end
end
end
end
subplot(2,2,1);
imshow(W);
title('the extracted watermark image');
imwrite(W,'w1.jpg','jpg');
'

 Accepted Answer

Image Analyst
Image Analyst on 14 Dec 2020

0 votes

Matrices are indexed (row, column), which is (y, x), NOT (x,y) as you have it.
Also check the limits of your for loop to make sure you don't step outside the image.
Attach your 2 images if you still need help so we can run your code.

9 Comments

this is the watermarked image, thanks for your answer.
this is the original image a
Looks like the same image. You're watermarking the image with itself? Why? If not, then attach the actual watermark image. And why do you think W, the "extracted" watermark, will equal the original watermark???
Anyway, did you fix your indexing?
Yes, watermarked image should looks like the original image and there is another binary watermark in the embedded image. W cannot be 100 percent equal to the original image because after transmitting it some bits or data can be lost or spread. well, I am trying to fix the indexing.
Sorry, I thought that was the watermark image, instead of the watermarked image. With watermarks, you can have the watermark either be slightly or totally visible, or completely hidden.
As you know, but for others (from Wikipedia):
A digital watermark is called imperceptible if the original cover signal and the marked signal are perceptually indistinguishable.
A digital watermark is called perceptible if its presence in the marked signal is noticeable (e.g. Digital On-screen Graphics like a Network Logo, Content Bug, Codes, Opaque images). On videos and images, some are made transparent/translucent for convenience for consumers due to the fact that they block portion of the view; therefore degrading it.
So what does your watermark image look like, and what is its size? You posted the cover image and the watermarked image but not the watermark image. I think usually they're smaller than the original/cover image.
hey , size is very smaller it is 32x32 and cover image is 512x512. it means that we have 1024 bits to insert in the cover data of 4096 bits(dct transform).
OK.
For what it's worth, I'm attaching a different method of watermarking. Perhaps it will help you see how you can fix your DCT code.
well thanks for your help. I changed thre code and also changed the criteria of insertion from multiplicative to additive formula. But in the end I don't know why am I getting flipped image in extraction . I am thinking to apply flip function but really don't know why it is actually flipping? Plus if I am putting threshold or you can say alpha value less than and greater to 1 then it is not working properly but if put lets say 30 then it is working fine. I cannot put the code online here due to some reasons. But any kind suggestions will be helpful to me.
Not sure without delving into it much more than I have time for. I notice that your're using x and y and a lot of novices confuse x and y with row and column. They do NOT correspond like that.
Matrixes are indexed M(row, column) which is M(y, x), NOT M(x,y) so make sure all that is correct. I saw for W you're using p as the first index which is derived from x, so that's not right in general but would be okay for a perfectly square matrix. And you're using x as the first index for A instead of y.

Sign in to comment.

More Answers (1)

Error is in this line of code:
BLOCK1=A(x:x+K-1,y:y+K-1);
"Index in position 2 exceeds array bounds (must not exceed 512)"
Position 2 corresponds to your second index: y:y+K-1
Apparently A is a mx512 array. You define y as y=(q-1)*K+1; At its largest value (q=512, K=8), y is 511*8+1, which is >> 512.
You likely need to rethink your approach.

1 Comment

thanks for your answer, yes it is going outside the array.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!