TransWikia.com

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"}]

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

One Answer

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_] := 
    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}
    

enter image description here

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

Answered by Christopher Moore on December 18, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP