Efficiency Optimization in Correlation Problem

I developed a part of my program that I need to optimize as much as possible. I will give a bit of context first:
  • The starting point is an image (img) lets say 448x448. This image contains several light spots and I want to find the centroid position of each one.
  • Each spot lies on a subaperture of the image of lets say of 32x32 pixels. So we can divide the image (img), into multiple sub-images (subI) of size 32x32.
  • The centroid of the light spot at each subaperture is obtained by calculating the correlation of a model light spot (template) to every single position possible withing the sub-image.
Here is the code I've made so far:
[m,n] = size(img);
centroids.X = zeros(m/gridSize,n/gridSize);
centroids.Y = zeros(m/gridSize,n/gridSize);
template = fspecial('gaussian', [7 7], 3); %Spot light model
template = template/max(template(:)); %Normalize to maximum
comparations = (gridSize-size(template,1) + 1); %Number of times to move the template around
%First two loops are two run over all the subapertures positions
for k = 0:size(centroids.X,1)-1
for l = 0:size(centroids.X,2)-1
%Now we get the sub images corresponding to each subaperture
%of the image where we want to find the centroid
lim = [k*gridSize+1,l*gridSize+1];
subI = imcrop(img,[lim(2),lim(1),gridSize-1,gridSize-1]);
correlations = zeros(comparations,comparations);
%In this two loops we compute the correlation coefficient
%between the template and each possible position in subI
for m = 1:comparations
for n = 1:comparations
correlations(m,n) = corr2(template,subI(m:m+7-1,n:n+7-1));
end
end
[~,maxCorr] = max(abs(correlations(:)));%We get the maximum value
[row,col] = ind2sub(size(correlation),maxCorr);%Position of the maximum
%Position of the maximum in the full image
rowAbs = row + gridSize*k + 3;
colAbs = col + gridSize*l + 3;
%Store the position as the centroid for the subaperture
centroids.X(k+1,l+1) = colAbs;
centroids.Y(k+1,l+1) = rowAbs;
end
end
The code works but it runs pretty slow as the number of iterations and comparitions is quite high... Any idea of how could I modify it to go faster?

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Modify Image Colors 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by