Gauri Deshpande
Gauri Deshpande

Reputation: 3

How to speed up the sliding window function in matlab for an image

I have an image on which I have to slide 81*81 window so as to find the the intensity distance(difference) between center pixel and each of the pixels present in surrounding 81*81 window, also I have to find out the location distance i.e.the difference between the center pixel co-ordinates and co-ordinates of the pixels present in surrounding window. If I use for loops then I'll have to use 4 nested for loops for this operation which takes almost a whole day for a single image. I want to store 81*81 intensity differences for each pixel present in an image and likewise for location distance, which tends to create a 4D variable. can anyone suggest me some efficient way of doing this. Here is how I have written this code:

I = imread('House.tiff');
YCbCr = rgb2ycbcr(I);
YCbCr = double(YCbCr);
YCbCr = imresize(YCbCr,[200 200]);
[m n v] = size(YCbCr);
Y = YCbCr(:,:,1); Cb = YCbCr(:,:,2);  Cr =YCbCr(:,:,3);
Y1 = padarray(Y,[20,20]);
Cb1 = padarray(Cb,[20,20]); 
Cr1 = padarray(Cr,[20,20]); 
window_size = 41;
p = (window_size-1)/2;
Dl = zeros(m,n,41,41);  Df = zeros(m,n,41,41);
for x = 1:1:m
    for y = 1:1:n
        for a = -p:1:p
            for b = -p:1:p
 Df(x,y,a+p+1,b+p+1)= abs(Y(x,y)-Y1(x+a+p,y+b+p))+ abs(Cb(x,y)- Cb1(x+a+p,y+b+p))+ abs(Cr(x,y)- Cr1(x+a+p,y+b+p));%% intensity distance
Dl(x,y,a+p+1,b+p+1) = max(abs((x+p)-(x+a+p)),abs((y+p)-(y+b+p)));%% location distance
            end
        end
    end
end

Upvotes: 0

Views: 301

Answers (2)

16per9
16per9

Reputation: 502

For that 81 x 81 sliding window, you probably have one X and Y "initial" point. Assuming that, create a new temporary window cropping the initial image between those centroids in a 80x80 window.

new_image = image(X-40:X+40,Y-40:Y+40);

this will create you the new temporary image in a fast way.

To store the value of the pixels all the way

for i=1:length(images)
currentImage=image(i,1);
new_image(i)=currentImage(X-40:X+40,Y-40:Y+40);
end

Upvotes: 0

Hugh Nolan
Hugh Nolan

Reputation: 2519

a) Your location difference matrix is always the same - it doesn't depend on Y. Just create a single 81x81 instance of it.

b) Why do you need to store all of these outputs? The long amount of time spend could easily be because of the RAM usage of such a large matrix means you are reading and writing to/from disk (pagefile) rather than RAM, which will slow things down a lot. It's probably better to go optimising at the "next stage" of this algorithm, reducing computations on what you are actually doing with this matrix. Depending on your next stage, using imfilter might be the way to go.

Upvotes: 0

Related Questions