LoginSignup
0
0

More than 1 year has passed since last update.

napariで選択したポイントの座標を取得して表示する

Posted at

目的

napariで開いた画像中にPointを手動で置き、キーボードで「i」を入力したら、そのPointの座標をコンソールに表示したい。

コード

get_selected_coordinate.py
import glob
import napari
import numpy as np

from skimage import io


dir_name = "./data"
file_names = glob.glob(dir_name + "/*.tif")
stack = np.array([io.imread(file_name) for file_name in file_names])

viewer = napari.view_image(stack)
points_layer = viewer.add_points(ndim=3)

@viewer.bind_key('i')
def print_coordinates(viewer):
    for point in points_layer.data:
        print(f"frame, x, y: {point[0]}, {point[2]}, {point[1]}")

napari.run()

出力

画像内でPointを指定して「i」を押すと、、
image.png

コンソールに座標が表示される
image.png

ポイント

ポイントレイヤーの作成

ndim=3を指定することで、frame番号も取得することができる。

points_layer = viewer.add_points(ndim=3)

関数のキーへのバインド

GUIで「i」を押すと、print_coordinates関数を実行してくれる。

@viewer.bind_key('i')
def print_coordinates(viewer):
    pass

座標の取得

全てのPointの座標をnumpy arrayで返してくれる。

points_layer.data
0
0
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
0
0