# Consistent pixel assignments in ClusteringComponents?

Mathematica Asked by Christopher Moore on December 18, 2020

I have a large dataset of SEM micrographs I am trying to binarily segment via ClusteringComponents, and it has seemed to be the most accurate method compared to the other binarization functions, e.g. Morphological Binarize.

However, while the clusters are accurate, it assigns the dark or light pixel groups in essentially random order. However I suspect that it occurs when the contrast is low.

I am able to reproduce this problem with example images. On the first image, the dark pixels are assigned 0, while on the second image, the light pixels are assigned 0.

test=ExampleData[{"TestImage","Man"}]


clustered=ClusteringComponents[test,2,PerformanceGoal->"Quality"];
Image[clustered - 1, "Bit"]


test2=ExampleData[{"TestImage","Tank2"}]


clustered2=ClusteringComponents[test2,2,PerformanceGoal->"Quality"]


How can I get it so it consistently assigns the dark pixel components to either 0 or 1??

OK so I'm gonna post my jerry-rigged solution so far:

1. Use the binarized cluster image to generate a mask of the original image

2. Testing the mean pixel intensity of the masked area vs global mean to check if the black clusters match up with the dark areas

3. Conditionally ColorNegate the values that don't match

clusterColorTestFunction[testImage_] :=

clustered=ClusteringComponents[testImage,2,PerformanceGoal->"Quality"];

globalMean=ImageMeasurements[testImage,"Mean"];

];

clusterColorTestFunction[#]&/@{test,test2}


However, if there is a solution within the Clustering functionality I would love to hear it

Answered by Christopher Moore on December 18, 2020

## Related Questions

### Numerically solving an ODE whose right-hand side involves an integral

3  Asked on May 5, 2021

### Why won’t FormObject execute properly in my notebook?

0  Asked on May 4, 2021 by sykh

### Solve PDE involving implicit function of independent variables

2  Asked on May 4, 2021 by mehrab

### How to declare a list of symbols as Real numbers?

0  Asked on May 4, 2021 by yfs

### NDSolve will try solving the system as differential-algebraic equations but it didn’t get the solution

3  Asked on May 4, 2021 by dcydhb

### How can I show the regions between the grid lines in a RadialAxisPlot in different colours?

2  Asked on May 4, 2021 by shredderroy

### Is FinancialData about to be deprecated?

1  Asked on May 4, 2021

### Solve a system of slope-intercept, ODE, and PDE with spatial and time variable

0  Asked on May 4, 2021 by masmoudi-omar

### Normalize an L(s, d) function for fixed s

2  Asked on May 4, 2021

### Changing pane background color in Animate

1  Asked on May 3, 2021

### Effectivelly using Compile for calculate a Unitary transformation

0  Asked on May 3, 2021 by lucas-freitas

### Find Fit error 4P logistic

1  Asked on May 3, 2021 by user76744

### Exponential function not simplified by FullSimplify?

1  Asked on May 3, 2021 by mimuller

### Plot of a function defined by an integral

1  Asked on May 3, 2021

### Changing LabelingFunction Color

2  Asked on May 3, 2021 by john-swartz

### How to correctly enumerate all the schemes of this cube coloring problem?

2  Asked on May 3, 2021

### How to make a code to find Taylor series symbolic solution to four coupled nonlinear differential equations?

0  Asked on May 3, 2021

### NIntegrate gives large value as result

1  Asked on May 3, 2021 by illlusion5

### DynamicLocation usage

1  Asked on May 3, 2021