📜 ⬆️ ⬇️

Detection of roundness in the image (for example micrographs)

Hello! In my profession (strictly speaking, the future profession) I am actually a chemist. Relatively recently, an interesting job appeared and the need to work a lot with a digital microscope, take a large number of photos and process them in a certain way. Namely: to find the linear dimensions of the particles (usually a round shape and initially, by eye) and to scrupulously record them in a laboratory journal.
It is not surprising that after the first hundred images I strongly thought about at least some automation of this process, but there was one catch: I knew perfectly well that “object-oriented programming is very good,” but ... But at that time I only owned school TurboPascal, university VB and bydlokoding on PHP in a procedural version. Stumbling through the forums, and taking into account the fact that I manage to work on at least two operating systems in one day (Mac / Windows / Ubuntu exist in euphoric symbiosis), I didn’t think much about it and decided to write in Java.
Omitting the details for about a week, in my spare time, chiseling my head about the object-oriented programming paradigm and sleepless nights with thoughts like “yes, damn, it works,” I will try to briefly describe the simple and fast “algorithm” which was born to me. It should immediately be said that it is suitable only for fairly clear images.
And here, by the way, and a typical representative (or rather, about a tenth of it), which needs to be processed:



It is immediately obvious that the objects are circles, and the first thing that comes to mind (yes, thanks to the university, even chemists know what it is, and it really comes to mind right away) is the Hough transformation . But ... Again "but": objects can run into each other, as well as be completely different from the circle. To be fair, I’ve said that I tried to do this using the Hough conversion and the OpenCV library in Python (Zdorogsky language) under Ubuntu ... The Hough space in this case is too ambiguous (== homogeneous) and a lot of free space is taken as a circle. Even after the pre-selected boundaries in the GIMP and other different techniques.

In general, I decided to write my almost bicycle. So first, we need to highlight the boundaries. Having tried several methods (they are actually not a lot of them, so maybe I tried them all), the most acceptable result was given by the Canny Edge Detector method (I don’t know in Russian, do not hit), and the result was such:
')


After some time of creative torment, I finally realized that the areas of interest are closed. But how to find out that these points form a closed curve, and these are not? I spent a lot of time on thinking about this issue, built some inhuman calculations, trying to apply that mathematical apparatus, which I had been taught not so long ago (and it turned out to be quite qualitative). And one day it hit me ... Fill !!! We need to see how the fill tool works in graphic open source projects !!! So I discovered the Breadth-first search algorithm for myself. Later, I still used for this purpose the ready-made class found on the Internet.
This is how the image looks after the fill:



Yes! We have identified all the objects of interest to us! But only inside them are some strange strokes. Only I began to think about how to solve this issue, and here (right on the same day) I very soon come across an article on Habrahabr about mathematical morphology (which is currently not available to the average user). No sooner said than done!
This is what our image now looks like (the “erode” method, the matrix took a standard 3x3 cross):



Hooray! Done! Now we need to once again use the BFS algorithm, and find the coordinates of the centers and extreme points of our particles.
Total received almost ready to use program for automatic logging :) Because from the microscope comes the usual usb-cord, then you really want to immediately see the image from the camera in the program, immediately photograph and recognize it. But this is the subject of my next sleepless nights.
And here is the final result (clickable):

- 02.22.2011, 22:05 - transferred to the "image processing" blog

Source: https://habr.com/ru/post/114249/


All Articles