I am a Mathematica novice working with a stack of 1024 x 1024 WatershedComponents (or MorphologicalComponents) label matrices that have resulted from watershedding timelapse microscopy images. I am trying to automatically assign the labels in a previous WatershedComponents mask (from a prior timepoint) to the mask generated from WatershedComponents at the next timepoint.

One approach I have tried is to calculate how much overlap there is for each component in the mask at the current time point with the masks that occupy the same positions in the matrix at the prior timepoint (i.e., this is basically a crude attempt at tracking the cells from time point to time point). To compute the amount of overlap, I am presently using the following functions below to generate a table of how much overlap there is (how many total positions in the two matrices overlap for each label). However, I am finding this is rather slow (takes about 30 to 40 seconds for matrices containing anywhere from 10 to 30 different labels). Is there a faster way to do this without using Table? Are there alternatives to Position or Intersection that might be faster? Would it help to use Compile? I don't know what is possible but achieving a 10-fold or better increase in performance would be more like what I was hoping for. I know best practice in Mathematica is to avoid using Table when possible. Thanks for any suggestions you can provide.

overlapTable = 
  Table[Length @ Intersection[
      Position[currentMask, currentMaskComponentList[[j]], -1], 
      Position[previousMask, previousMaskComponentList[[i]], -1]], 
    {j, Length[currentMaskComponentList]}, 
    {i, Length[previousMaskComponentList]}]

To clarify what I am trying to do here are some examples of the images after watershedding I am working with:

At timepoint #1 I get this WatershedComponents image:

after using WatershedComponents I get this image at timepoint=1

After joining components together that belong to the same cell and merging the rest of the components to the background (black) I get this final mask with 6 components (5 cells + black background) for timepoint #1:

after joining components together that belong to same cell and merging everything else to background I get this mask for timepoint=1

Now I move to timepoint #2 and get the next WatershedComponents image of the same cells (taken about 5 minutes later). I am trying to automatically label this image to match the labels in the label matrix in the image above with the joined components & black background so I don't have to take the time to join the components again by hand (and relabel the components to match the previous timepoint).

[after using WatershedComponents I get this image at timepoint=2

  • $\begingroup$ Some more information would be helpful. 30-40 s is too long by your standards; are these empirical or are you comparing to another product? (In other words, what's the efficiency goal here?) Additionally, providing a link to some data (not the full data set PLEASE) that can allow others to reproduce your results are likely necessary if you are to get a quality answer. $\endgroup$ – bobthechemist Jul 27 '14 at 13:30
  • $\begingroup$ @bobthechemist: I have now added some example images of the label matrices from two sequential timepoints I am trying to work with. I am not comparing efficiency to other products but more to how long it takes me to join and relabel the matrices by hand. I can do this by hand within a minute or two but when you do this for 100+ frames the time really adds up so speedy automated label matching function would really be helpful here. Thanks for your interest. $\endgroup$ – user13999 Jul 27 '14 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.