I'm learning MATLAB (still trying to "think in vectors") and have the following piece of code:
stats = regionprops(bwconncomp(mask), 'all');
% find the object nearest our nucleus
smallest_dist = flintmax;
location = [cell_x, cell_y];
for j = 1:numel(stats)
stat = stats(j);
dist = pdist([location;[stat.Centroid]], 'euclidean');
if dist < smallest_dist
regions{channel} = stat;
smallest_dist = dist;
end
end
All that is doing is searching through a struct array for the region which has a location (centroid) closest to my known cell's X/Y. I'm sure this is not canonical and that it could be improved.