# \[?\] Avoid boundaries disappearing between cells during segmentation?

**URL:** <https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569>\
**Category:** Questions\
**Tags:** image-processing\
**Created:** [May 21, 2017, 8:00pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569 "2017-05-21T20:00:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alihashmi87](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/alihashmi87/32/29383_2.png) [@alihashmi87](https://community.wolfram.com/u/alihashmi87)\
**Post date:** [May 21, 2017, 8:00pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/1 "2017-05-21T20:00:47Z")

</div>

I have asked the question on MSE as well: [https://mathematica.stackexchange.com/questions/146550](https://mathematica.stackexchange.com/questions/146550)

I have the following image that i segment into respective cells using the function below:

```
segmentImage[binarizedMask_?ImageQ] := 
Module[{seg, areas, indexMaxarea, maxArea},
seg = MorphologicalComponents@*ColorNegate@Dilation[binarizedMask, 1];
areas = ComponentMeasurements[seg, "Area"];
{indexMaxarea, maxArea} = First@MaximalBy[areas, Last] /. Rule -> List;
If[maxArea > 20000, ArrayComponents[seg, Length@areas, indexMaxarea -> 0], seg]];

```

 ![epitheliacelltracker.png](https://global.discourse-cdn.com/wolfram/original/2X/7/7626f1238d1ba6b1a10160a64a437569f78627df.png)

The result of the segmentation `img2 = segmentImage[img]//Colorize` is shown below:

 ![epitheliacelltracker2.png](https://global.discourse-cdn.com/wolfram/original/3X/a/e/ae38b30144c32c0256055f2bf63493e8b54ed2b5.png)

The problem that I am facing is that the boundaries between the cells are completely lost. I cannot recover any information of the neighbours or the neighbourcount:

```
ComponentMeasurements[img2,"NeighborCount"]
(* {1 -> 0, 2 -> 0, 3 -> 0, 4 -> 0, 5 -> 0, 6 -> 0, 7 -> 0, 8 -> 0, 
9 -> 0, 10 -> 0, 11 -> 0, 12 -> 0, 13 -> 0, 14 -> 0, 15 -> 0, 
16 -> 0, 17 -> 0, 18 -> 0, 19 -> 0, 20 -> 0, 21 -> 0, 22 -> 0, 
23 -> 0, 24 -> 0, 25 -> 0, 26 -> 0, 27 -> 0, 28 -> 0, 29 -> 0, 
30 -> 0, 31 -> 0, 32 -> 0, 33 -> 0} *)

```

This should not be the case since in the original image the cells are sharing boundaries. Is there a way to recover the neighbours or better way to do segmentation so that I do not lose boundaries between the cells?

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [May 21, 2017, 10:01pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/2 "2017-05-21T22:01:49Z")

</div>

Hi Ali,

try this:

```
mask = Dilation[Binarize@img2, 2];
img3Data = WatershedComponents[ColorNegate@img2, Method -> "Basins"] ImageData[mask];

```

which means:

 ![graylevels.png](https://global.discourse-cdn.com/wolfram/original/3X/f/5/f537e741ba5e66b71cf78783529163ed7cf256a3.png)

With this one can do things like

```
ComponentMeasurements[img3Data, "NeighborCount"]

```

Hope that helps, regards – Henrik  
[1]: [http://community.wolfram.com//c/portal/getImageAttachment?filename=graylevels.png&userId=32203](http://community.wolfram.com//c/portal/getImageAttachment?filename=graylevels.png&userId=32203)

---

<div class="post-metadata">

**Author:** ![alihashmi87](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/alihashmi87/32/29383_2.png) [@alihashmi87](https://community.wolfram.com/u/alihashmi87)\
**Post date:** [May 21, 2017, 10:08pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/3 "2017-05-21T22:08:50Z")

</div>

Thanks this looks very neat. We also figured another way on MSE !

---

<div class="post-metadata">

**Author:** ![alihashmi87](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/alihashmi87/32/29383_2.png) [@alihashmi87](https://community.wolfram.com/u/alihashmi87)\
**Post date:** [May 21, 2017, 10:09pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/4 "2017-05-21T22:09:53Z")

</div>

Btw in your last line of code is the ImageData[mask] being multiplied ?

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [May 21, 2017, 11:13pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/5 "2017-05-21T23:13:10Z")

</div>

Yes, a multiplication with an “overall mask” is needed because of the result of `WatershedComponents`.

> We also figured another way on MSE !

And - what is the solution ???

---

<div class="post-metadata">

**Author:** ![alihashmi87](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/alihashmi87/32/29383_2.png) [@alihashmi87](https://community.wolfram.com/u/alihashmi87)\
**Post date:** [May 21, 2017, 11:21pm UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/6 "2017-05-21T23:21:41Z")

</div>

`ComponentMeasurements[Dilation[segmentImage[img],2]//Colorize,"NeighborCount"]` does not work, however, `ComponentMeasurements[Dilation[segmentImage[img],2],"NeighborCount"]` i.e. without `Colorize` works. Not sure why

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [May 22, 2017, 11:24am UTC](https://community.wolfram.com/t/avoid-boundaries-disappearing-between-cells-during-segmentation/10569/7 "2017-05-22T11:24:00Z")

</div>

Hi Ali,

one problem might be that your images - as far as I can tell when I copy them from your post - contain an alpha channel. Furthermore I doubt that using `Dilation` solves the problem, because this way new thin areas (the overlaps) are created:

```
(* removing alpha channel: *)
img2 = RemoveAlphaChannel@img2;
(* applying Dilation[]: *)    
img3 = ColorConvert[Dilation[img2, 2], "Grayscale"];
(* converting image to array of indexes: *)    
grayLevels = Union@Flatten[ImageData[img3]];
grayRules = MapIndexed[#1 -> First[#2] - 1 &, grayLevels];
img3Data = ImageData[img3] /. grayRules;

```

Now

```
ComponentMeasurements[img3Data, "NeighborCount"]

```

seems to give a reasonable output, but

 ![graylevels1.png](https://global.discourse-cdn.com/wolfram/original/3X/8/d/8d715f63b6859be1d8591d1bb4be235c9dd80fc7.png)

demonstrates my point. Or am I misunderstanding the problem?

Regards – Henrik
