-5

How to apply sliding window for subtracting two different images in matlab, the window size must be 4X4,

please help me

i want to find similarity value between two different images.if A and B are two 2 images take difference between each 4x4 matrix of each A&B in sliding window manner i tried a code ,i dont know whether it is correct or not

m=imread('index.jpeg');
sal=imread('salt.jpg');
salt=rgb2gray(sal);
ab=rgb2gray(m);
imshow(ab);
imh=size(ab,2);
imw=size(ab,1);
wh=4;
ww=4; 
k=0;
disp(imh),disp(imw);
if 1
for j=1:imh+wh-1

    for i=1:imw+ww-1

        w1=ab(j:j+wh-1,i:i+wh-1,:);

        w2=salt(j:j+wh-1,i:i+wh-1,:);

        w3=w1-w2;

        disp(w3);
        disp('next mat');


    end
    k=k+1;
disp(k);
end

end
sara
  • 13
  • 1
  • 5
  • The question is very unclear. Please elaborate in a lot more detail, and post what you have tried so far. – Zaphod Jul 08 '13 at 09:20
  • "*i dont know whether it is correct or not*": It is correct, if it gives the result you are looking for. If not, it is incorrect. – Schorsch Jul 08 '13 at 14:12
  • When i am run this code i got several 4X4 matrices & following error ??? Index exceeds matrix dimensions. Error in ==> nwin2 at 17 w1=ab(j:j+wh-1,i:i+wh-1,:); – sara Jul 09 '13 at 04:10

1 Answers1

0

The upper bounds of your for-loops are the cause for your troubles.
You specify:

imh=size(ab,2);
imw=size(ab,1);

However, your for-loops have these conditions:

j=1:imh+wh-1

and

i=1:imw+ww-1

So you move past both the 'height' and the 'width' dimension.
Try this instead:

for j=1:imh-wh
    for i=1:imw-ww
        w1=ab(j:j+wh,i:i+wh,:);
        w2=salt(j:j+wh,i:i+wh,:);
        w3=w1-w2;
    end
    k=k+1;
end
Schorsch
  • 7,761
  • 6
  • 39
  • 65