3
$\begingroup$

How to detect defects in the image? The code doesn't need to find all small defects, but I would like to reliably detect the 3 largest defects.

Microscope image Original microscope image

My current solution, but it is missing the larger defects

img = Import["MIC.png"]
lf = LaplacianGaussianFilter[img, 1]
laf = LocalAdaptiveBinarize[lf, 5, {1, 0, 0.04}]
mc = MorphologicalComponents[laf, CornerNeighbors -> False] // Colorize
SelectComponents[mc, {"Area", "Count", 
  "Elongation"}, #1 > 10 && #2 > -50 && #3 < 0.7 &]
$\endgroup$
1
  • $\begingroup$ It's hard to provide a reliable solution when only one example is given ... Anyhow, have you tried experimenting with morphological operations like Closing? $\endgroup$
    – Domen
    Aug 7, 2023 at 13:38

1 Answer 1

7
$\begingroup$

First I only take the part of the image where the majority of the defects reside:

bubblyPart = ImageTake[img, {90, All}, {375, 500}]
bubblyPixels = ImageData[bubblyPart];

enter image description here

Then I only look at the 2nd channel of the RGB values. This appeared to work best for finding the defects (the third channel works very well for some defects, but not all):

channel2 = Image[bubblyPixels[[All, All, 2]]]

enter image description here

I then apply your exact method in your question to highlight the defects (I just changed some of the selection criteria in SelectComponents) and this appears to work sufficiently well:

lf = LaplacianGaussianFilter[channel2, 1];
laf = LocalAdaptiveBinarize[lf, 5, {1, 0, 0.04}];
mc = MorphologicalComponents[laf, CornerNeighbors -> False] // 
   Colorize;
SelectComponents[mc, {"Area", "Count", 
  "Elongation"}, #1 > 0 && #2 > 12 && #3 < 0.7 &]

enter image description here

This does miss some of the smaller defects, but you may able to mess with the parameters in LocalAdaptiveBinarize and SelectComponents components to get better defect detection. It is also highlighting the edge in the bottom right of the image, but you can just remove the defects highlighted on the bottom right of the image since it looks like the defects are always either on the top or left side of this part of the image.

If it's not always going to be true that the defects are on the left or top for all cases, this will be a little tougher, but you can increase the minimum allowed "Count" to remove the bottom right edge at the expense of missing some smaller defects:

lf = LaplacianGaussianFilter[channel2, 1];
laf = LocalAdaptiveBinarize[lf, 5, {1, 0, 0.04}];
mc = MorphologicalComponents[laf, CornerNeighbors -> False] // 
   Colorize;
SelectComponents[mc, {"Area", "Count", 
  "Elongation"}, #1 > 0 && #2 > 19 && #3 < 0.7 &]

enter image description here

Add-on: I really need to work on my reading comprehension skills it seems. I see you said in the question you only want to reliably find the 3 largest defects. We can Increase the minimum "Count" req in SelectComponents and apply this to the second channel of the image to easily get the 3 largest defects:

img2 = ColorSeparate[img][[2]];
lf = LaplacianGaussianFilter[img2, 1];
laf = LocalAdaptiveBinarize[lf, 5, {1, 0, 0.04}];
mc = MorphologicalComponents[laf, CornerNeighbors -> False] // 
   Colorize;
sc = SelectComponents[
   mc, {"Area", "Count", 
    "Elongation"}, #1 > 10 && #2 > 50 && #3 < 0.7 &];
sc = RemoveBackground[sc // Binarize];
ImageCompose[img, sc]

enter image description here

You will have to check with other examples to make sure #2 > 50 in SelectComponents works in general however.

Another add-on If you are imaging something very regular (like the T shaped thing in the background is always the same) you could take an image of that with no defects to develop a template that we could ImageSubtract from the image with defects; this could improve defect detection.

$\endgroup$
2
  • $\begingroup$ Sorry for late response. Thanks for the answer. It woks in most cases. Since i am taking images of the same structure all the time i will try to use ImageSubtract as suggested. $\endgroup$ Sep 11, 2023 at 10:52
  • $\begingroup$ @JozefPulko Actually after reading the documentation of ImageDifference, I think It would be more appropriate than ImageSubtract $\endgroup$
    – ydd
    Sep 11, 2023 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.