Hello,
I am trying to extract SIFT features for an Image. In the process we need to find the local maximum and minimum.
So, I wrote a function which computes them. But it is computationally intensive. Will you please go through my algorithm to reduce time of execution.
The function given above will take in a matrix which is of size, lets say 512X512X4. Basically these are 4 images blurred with different Gaussian of different variances. Now we take the second image (:,:,2), the image above  (:,:,1) and the image below (:,:,3). Take a pixel, get the neighborhood of 3x3 pixels around the current pixel. Take a neighborhood of 3x3 in the image just above and below the current image. Then we need to check whether the current pixel is the maximum or minimum considering all the neighborhoods taken. Then another 3 images are taken (:,:,3)  main image, an image above (:,:,2) and an image below (:,:,4). Now the process is repeated for all the images in the present image (:,:,3).
Following is the function I wrote:
%The "dog_mat" is the matrix of size 512x512x4
%The max_min is the output matrix
function [ max_min ] = get_maxmin( dog_mat )
dog_mat = double(dog_mat);
[r c n] = size(dog_mat);
disp(n);
max_min = zeros(r,c,n2);
%Loop for getting the max and min for the two matrices (:,:,2) and (:,:,3)
for frames = 2:n1
p_up = dog_mat(:,:,frames+1); %The top matrix
p_down = dog_mat(:,:,frames1); %The Bottom MAtrix
p = dog_mat(:,:,frames); %The current matrix
for i = 1:r1
for j = 1:c1
%Getting the proper neighborhood about the required pixel
if (i == 1) && (j == 1)
neighbours = [p(1,2),p(2,2),p(2,1),p_up(1,1),p_up(1,2),p_up(2,1),p_down(2,2),p_down(1,1),p_down(1,2),p_down(2,1),p_down(2,2)];
if (p(i,j) >= max(neighbours)  p(i,j) <= min(neighbours))
max_min(i,j,frames1) = p(i,j);
end
end
if (i > 1) && (i < r) && (j == 1)
neighbours = [p(i1:i+1,j:j+1),p_up(i1:i+1,j:j+1),p_down(i1:i+1,j:j+1)];
if (p(i,j) == max(neighbours)  p(i,j) == min(neighbours))
max_min(i,j,frames1) = p(i,j);
end
end
if (i == 1) && (j > 1) && (j < c)
neighbours = [p(i:i+1,j1:j+1),p_up(i:i+1,j1:j+1),p_down(i:i+1,j1:j+1)];
if (p(i,j) == max(neighbours)  p(i,j) == min(neighbours))
max_min(i,j,frames1) = p(i,j);
end
end
if (i > 1) && (j > 1) && (i < r) && (j < c)
neighbours = [p(i1:i+1,j1:j+1),p_up(i1:i+1,j1:j+1),p_down(i1:i+1,j1:j+1)];
if (p(i,j) >= max(neighbours)  p(i,j) <= min(neighbours))
max_min(i,j,frames1) = p(i,j);
end
end
end
end
end
The problem with this function is it is running for 1020 sec for each "dog_mat". There are three other "dog_mat" which should be analyzed to get the local maxima and minima and the final process is taking 30 seconds which is huge amount.
Will you please help me in writing optimized code? It would be very helpful. Even if you have slightest idea please post it here...
Thank You in advance! :)
