Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: Vectorizing imcrop on an image stack
Posted:
Apr 25, 2013 4:13 AM


"Scott" wrote in message <klae14$bvv$1@newscl01ah.mathworks.com>... > I'd like to do the following but make it more efficient by vectorizing it or using cellfun: > > I have a time series of microscopy images that are stored in a 3D matrix (the z axis represents time). Each image has many bacteria in it and I have done some image segmentation so I can extract the centroid or boundingbox or other useful feature that belong to each bacterium using regionprops(). I can track the bacteria over time. > > So effectively I have an imageStack and a 2D cell array (call it bacData) where each entry of the array holds the centroid (for illustration here). > > I'd like to make a new cell array where each bacterium has an entry that contains a 3D matrix of its little area of the image (say 21x21 pixels) over time. If the bacteria just stayed in the same place, then I could just figure out the appropriate coordinates and slice appropriately the whole image stack. But the problem is that they move, so the centroid (and hence the sub image) for a bacterium is going to differ from zslice to zslice. > > This is straightforward to do with for loops, but I would love a solution that is more vectorized. Here is a possible for loop version making use of the centroid: > > cellIms=cell(size(bacData)); > for iBac=1:size(bacData,1) > for iTime=1:size(bacData,2) > lowerLeftCorner = bacData{iBac,iTime}20; > regionToCrop=[lowerLeftCorner 21 21]; > cellIms{iBac,iTime} = imcrop(imageStack(:,:,iTime),regionToCrop); > end; > end; > > > Thanks for any suggestions. > I wonder if you'd gain anything by using the arrayfun/cellfun/bsxfun wrapping of the loops compared to writing the loops explicitly, I'd worry that the overhead in that function call would outweight the gains. I'd guess proper preallocation is the most important thing to do  allocate the entire cropped imagestack.
Interested to hear about the outcome.



