# what scheme to use to segment this aggregate of cells in such a poor illumination

Due to issues with the microscope I have the following image which is inherently dark. At the center of the image is an aggregate (a cluster of cells). I can get a binarized mask using the mask draw capability in mathematica. But how can it be automated? What scheme should I use to segment it. I have not worked with such poorly illuminated image (Figure 1) before. GradientOrientationFilter? If you play with brightness contrast of the image you can find the aggregate As per @SquareOne's recommendation: LocalAdaptiveBinarize seems to work pretty well. still not a complete solution. • To automize it I think more Information are needed E.g. how should a Software now that the bright spot in the top left are not the cells. The best way would be to fix the microscope. But if you come up with additional Information about expected size of the cell, app. Position... . Then I could think of an iterative scheme to find the Cluster. – Eisbär Mar 1 '17 at 7:41
• See LocalAdaptiveBinarize – SquareOne Mar 1 '17 at 15:18
• @SquareOne LocalAdaptiveBinarize seems to work pretty well. I have the picture posted in the question and credited you with it. Do you have a strategy to extract a Morphological Perimeter of the cluster inside in the image after the adaptive binarize – Ali Hashmi Mar 1 '17 at 18:31

## 1 Answer

If I assume that the cells always have similar size (~100-200 pixel diameter), I think I can do this.

First step: Load the image

img = ColorConvert[Import["https://i.stack.imgur.com/pFvT7.png"],
"Grayscale"]


I'm going to use the model

• that your image is the inhomogeneous brightness due to the microscope times the "actual" image content (including the cell borders)
• that the inhomogeneous brightness is varies very slowly and thus can be approximated using GaussianFilter[img,50]

so $img / GaussianFilter[img,50]$ is a good approximation of the "actual" image content:

eps = 10^-10; (* add a small value to the denominator to prevent division by zero *)
equalBrightness =
Image[Rescale[
ImageData[img]/(GaussianFilter[ImageData[img], 50] + eps)]] Next, I'll want the gradient of this preprocessed image:

gradient =
GaussianFilter[ImageData[equalBrightness], 10, #] & /@
IdentityMatrix;


...as an array of complex numbers, for convenience:

gradientC = {1, I}.gradient;


I'm going to use template matching to find "something roughly circular" in this gradient image. The template will simply consist of complex numbers pointing "outwards" from the center, times some window function. That's the gradients we're looking for:

template = Array[
(#1 + I*#2)*HammingWindow[#1]*HammingWindow[#2] &, {256,
256}, {{-.5, .5}, {-.5, .5}}];

ListVectorPlot[ReIm[template]] Export["F:\\imgs\\so2.png", %]


The correlation between this template and the gradients found above is highest in the cell's center. (I have no idea how reliable this is. You'll have to check with other images, and play with filter sizes, preprocessing functions above):

corr = Image[
Rescale[Re[ListCorrelate[template^2, Conjugate@gradientC^2]]]] Finding the point of highest correlation is easy:

maxPos = PixelValuePositions[corr, "Max"][] + Dimensions[template]/2;
HighlightImage[equalBrightness, {maxPos}] Now we can use this position to create a marker mask for WatershedComponents:

markers =
Binarize@Rasterize[
Graphics[{White, Point[maxPos], Circle[maxPos, 256]},
PlotRange -> {{0, 0}, ImageDimensions[img]}\[Transpose],
ImageSize -> ImageDimensions[img], Background -> Black]] This mask basically says: I'm looking for two components. One component contains the center point, the other component contains all points on the circle radius. Other than that, choose the components so that they best "fit" to the gradient strengths:

components =
WatershedComponents[Image[Rescale[Abs[gradientC]]], markers];

HighlightImage[equalBrightness, Image[UnitStep[-components]]] This is the simplest way I can come up with to get a mask from a center a point and an estimate of the radius. If you want more control over the mask border, you'll probably have to implement a more advanced algorithm, like adaptive contours

• thanks this looks brilliant and promising. i will continue from where you left. +1 for your approach – Ali Hashmi Mar 1 '17 at 20:21
• Great technique. Could you give a reference to read in for the method of using the Gradient and the correlation to find the cell? – Eisbär Mar 3 '17 at 11:21
• @Eisbär: I don't really have one. The basic idea is "rotate the gradients in complex space so that gradients pointing to the center add up while other gradients cancel each other out". I think I've got the idea from the article linked in this question, but they're using the phase angle to encode radius, not orientation. In this case, the shape doesn't have constant radius, so looking at the orientation makes more sense. – Niki Estner Mar 3 '17 at 11:52
• (But honestly, there have been a lot of "detect a circle" questions here on MMA and elsewhere, so I might have gotten the idea from somewhere else and simply forgotten it.) – Niki Estner Mar 3 '17 at 11:53
• Thanks, I lack Knowledge on this Topics, so your answer gives a lot to Digest. I will look for those articles. – Eisbär Mar 3 '17 at 12:00