I've got an image in matlab defined on a real scale (0,1) and a mask defined on a integer scale.
Example
mask = [ 1 1 1 3 4 ;
1 1 1 2 4 ;
1 1 2 2 2 ]
img = [ 0.1 0.1 0.2 0.2 0.3 ;
0.1 0.1 0.2 0.3 0.3 ;
0.1 0.1 0.3 0.3 0.3 ]
and for each region in the mask (i.e. 1,2,3,4) I want to compute a certain feature (say, the mean) on the corresponding image's intensities.
The algorithm I've used is
for i = labels
region = img(mask==i);
feature(i) = mean(region);
end
Now, this algorithm is pretty slow for images of size 300x400x500, and a cardinality of the labels set > 40000 (which, btw, is exactly my case).
Any suggestion on how to speed up my code?