skoda23 skoda23 -4 years ago 270
C++ Question

How to detect anomalies in opencv (c++) if threshold is not good enought?

I have grayscale images like this:

enter image description here
I want to detect anomalies on this kind of images. On the first image (upper-left) I want to detect three dots, on the second (upper-right) there is a small dot and a "Foggy area" (on the bottom-right), and on the last one, there is also a bit smaller dot somewhere in the middle of the image.

The normal static tresholding does't work ok for me, also Otsu's method is always the best choice. Is there any better, more robust or smarter way to detect anomalies like this? In Matlab I was using something like Frangi Filtering (eigenvalue filtering). Can anybody suggest good processing algorithm to solve anomaly detection on surfaces like this?

EDIT: Added another image with marked anomalies:

enter image description here

Using @Tapio 's tophat filtering and contrast adjustement.
Since @Tapio provide us with great idea how to increase contrast of anomalies on the surfaces like I asked at the begining, I provide all you guys with some of my results. I have and image like this:
enter image description here

Here is my code how I use tophat filtering and contrast adjustement:

kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3), Point(0, 0));
morphologyEx(inputImage, imgFiltered, MORPH_TOPHAT, kernel, Point(0, 0), 3);
imgAdjusted = imgFiltered * 7.2;


The result is here:

enter image description here

There is still question how to detect anomalies that appear on the edges of ROI (bottom white edge problem on the result image). So if anybody have idea how to solve it, just take it! :)

Answer Source

You should take a look at bottom-hat filtering. It's defined as the difference of the original image and the morphological closing of the image and it makes small details such as the ones you are looking for flare out.

first image pair

second image pair

third image pair

I adjusted the contrast to make both images visible. The anomalies are much more pronounced when looking at the intensities and are much easier to segment out.

Let's take a look at the first image:

segmentation accuracy needed

The histogram values don't represent the reality due to scaling caused by the visualization tools I'm using. However the relative distances do. So now the thresholding range is much larger, the target changed from a window to a barn door.

Global thresholding ( intensity > 15 ) :

After global thresholding

Otsu's method worked poorly here. It segmented all the small details to the foreground.

After removing noise by morphological opening :

After morphological opening

I also assumed that the black spots are the anomalies you are interested in. By setting the threshold lower you include more of the surface details. For example the third image does not have any particularly interesting features to my eye, but that's for you to judge. Like m3h0w said, it's a good heuristic to know that if something is hard for your eye to judge it's probably impossible for the computer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download