Classical segmentation

Spread the love

Machine learning techniques (like CellPose) are nice, but the disadvantage is that they need to be trained. Classical methods like intensity thresholding and watershed segmentation have only a few parameters, which can easily be hand-picked. Therefore, if CellPose doesn’t work for your data, there’s a chance that classical segmentation is the best solution to your problem.

For classicial segmentation, there are already good tutorials on the web, so I’m not going to explain everything here. Instead, I’m going to briefly discuss two existing tutorials, and then it’s up to you to follow them.

Adaptive thresholding using OpenCV

Image by OpenCV

In this tutorial, we are going to perform thresholding, which is separating the foreground from the background. In fluorescence images, this is very common, and I often use this algorithm.

First, install OpenCV through the Anaconda Prompt. (If you’re not familiar with Anaconda, start here.) Open a new environment, and install OpenCV. You can do this by running the following two commands:

conda create --name opencvtest opencv
conda activate opencvtest

The first command will create a new Anaconda environment named opencvtest (feel free to choose a different name), with one package: OpenCV. The second command will then activate the environment. From then on, you can run Python code in that environment. (For example, create a file named “test.py” in the folder where your command line is running, and then run the command python test.py.)

Then, you can follow the tutorial, and try the various image thresholding methods.

Coin segmentation using OpenCV

Image by OpenCV

Ok, cells aren’t coins, but the general principles apply. In this tutorial, coins are overlapping, and the authors show how to segment them.

First, follow the installation instructions for the other OpenCV tutorial above, then then follow this tutorial:

In my experience, watersheds don’t work that well on more complicated images. If you can’t get it work, you might have to turn your attention to machine learning methods.

Thresholding and segmentation using Scikit-image

Scikit-image, another Python library for image manipulation, is a real goldmine. It’s an alternative to OpenCV, although nothing is stopping you from using both in the same script. (All Python image libraries work with numpy arrays, so you can mix them.) The authors have created a lot of tutorials for image segmentation, although some will be less appropriate for images of cells. The tutorials are also a little bit less hand-holding compared to the OpenCV ones, which is why I put the OpenCV tutorials first.

A nice bonus of scikit-image is that it includes the regionprops function, which allows you to easily measure various properties of your segmentated regions, like their size, shape and intensity.

Installation of Scikit-image into an existing environment (like into above opencvtest environment) works as follows:

conda install scikit-image

Then, once it’s installed you can find the tutorials here:

Some good examples are:

On that page, there are also some longer tutorials, like these: