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:
Use the binarized cluster image to generate a mask of the original image
Testing the mean pixel intensity of the masked area vs global mean to check if the black clusters match up with the dark areas
Conditionally ColorNegate the values that don't match
clusterColorTestFunction[testImage_] :=
Module[{clustered,mask,globalMean,maskedMean},
clustered=ClusteringComponents[testImage,2,PerformanceGoal->"Quality"];
mask=Image[clustered - 1, "Bit"];
globalMean=ImageMeasurements[testImage,"Mean"];
maskedMean=ImageMeasurements[testImage,"Mean",Masking->mask];
Which[globalMean>maskedMean,ColorNegate[mask],globalMean <maskedMean,mask]
];
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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP