Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Vectorizing imcrop on an image stack
Replies: 2   Last Post: Apr 25, 2013 4:13 AM

 Messages: [ Previous | Next ]
 Bjorn Gustavsson Posts: 430 Registered: 12/7/04
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 z-slice to z-slice.
>
> 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 pre-allocation is the most important thing to do - allocate the entire cropped image-stack.

Interested to hear about the outcome.

Date Subject Author
4/25/13 Scott
4/25/13 Scott
4/25/13 Bjorn Gustavsson