Question

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
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top