Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Vectorizing imcrop on an image stack

Subject: Vectorizing imcrop on an image stack

From: Scott

Date: 25 Apr, 2013 05:13:08

Message: 1 of 3

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.

Subject: Vectorizing imcrop on an image stack

From: Scott

Date: 25 Apr, 2013 05:17:07

Message: 2 of 3

I realize I left the above function as a cell array. I'd then ideally want to stack all the values for a single bacterium into a 3D matrix so I could use reshape and commands like that.

Subject: Vectorizing imcrop on an image stack

From: Bjorn Gustavsson

Date: 25 Apr, 2013 08:13:08

Message: 3 of 3

"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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us