4
3

More than 3 years have passed since last update.

細胞のセグメンテーションをやってみた(HPA-Cell-Segmentation)

Last updated at Posted at 2021-02-19

経緯

最近、Kaggleをちょこちょこやってます。Human Protain Atlasというコンペに参加しているのですが、そこで細胞のセグメンテーションタスクがあり、そのときに試した、細胞のセグメンテーションをやってくれるリポジトリの使い方を共有します。HPA-Cell-Segmentationというリポジトリです。

環境

  • Ubuntu 20.04
  • Python 3.8.5(Virtualenv)

手順

まずは下の手順で環境を作成します。

$ git clone https://github.com/CellProfiling/HPA-Cell-Segmentation.git
$ cd HPA-Cell-Segmentation
$ sh install.sh
// 下のコマンドでエラーが出なければOKです
$ python -c 'import hpacellseg'

サンプルコードがリポジトリにあったので、それを元に試してみました。imagesに細胞の微小管、核、小胞体の画像のセットを渡してあげます。データセットはこちらから入手しました。

import hpacellseg.cellsegmentator as cellsegmentator
import matplotlib.pyplot as plt
from hpacellseg.utils import label_cell, label_nuclei
# Assuming that there are images in the current folder with the
# following names.
images = [["test/0a12663b-71ff-49e2-9fd9-b9a1bba5a9b4_red.png"],
        ["test/0a12663b-71ff-49e2-9fd9-b9a1bba5a9b4_blue.png"],
        ["test/0a12663b-71ff-49e2-9fd9-b9a1bba5a9b4_yellow.png"]]
NUC_MODEL = "./nuclei-model.pth"
CELL_MODEL = "./cell-model.pth"
segmentator = cellsegmentator.CellSegmentator(
    NUC_MODEL,
    CELL_MODEL,
    scale_factor=0.25,
    device="cuda",
    padding=False,
    multi_channel_model=True,
)

# For nuclei
nuc_segmentations = segmentator.pred_nuclei(images[1])

# For full cells
cell_segmentations = segmentator.pred_cells(images)

# post-processing
nuclei_mask = label_nuclei(nuc_segmentations[0])
nuclei_mask, cell_mask = label_cell(nuc_segmentations[0], cell_segmentations[0])
plt.imshow(nuclei_mask)
plt.show()
plt.imshow(cell_mask)
plt.show()

結果

入力画像の微小管の画像はこちらです。
0a12663b-71ff-49e2-9fd9-b9a1bba5a9b4_red.png

入力画像の核の画像はこちらです。
0a12663b-71ff-49e2-9fd9-b9a1bba5a9b4_blue.png

入力画像の小胞体の画像はこちらです。
0a12663b-71ff-49e2-9fd9-b9a1bba5a9b4_yellow.png

この3つの画像を入力として、先程紹介したコードでセグメンテーションした画像は下のようになります。まずは細胞全体です。
Figure_2.png

次は核をセグメンテーションした結果です。
Figure_1.png

まとめ

こんな感じで簡単に細胞をセグメンテーションできました。Kaggle面白くなってきました。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3