LoginSignup
1
0

More than 1 year has passed since last update.

ImageJでマルチポイント周辺の画像を保存したい

Posted at

プラグイン名

Collect_Multi_Images

仕様

  • ImageJで開いた画像スタックの各フレームにマルチポイントを追加すると、マルチポイント周辺の画像(入力で指定された大きさ)を指定されたパスのディレクトリに保存する。

入力

  • ディレクトリパス
  • 画像サイズ

出力

  • TIF画像ファイル

コード

Collect_Multi_Images.java
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;


public class Collect_Multi_Images implements PlugIn {

	public void run(String arg) {

        GenericDialog gd = new GenericDialog("Input");
        gd.addDirectoryField("Output Directory: ", arg);
        gd.addNumericField("Size: ", 9);
        gd.showDialog();
        String dir = gd.getNextString();
        int size = (int)gd.getNextNumber();

        ImagePlus imp = IJ.getImage();
        Roi roi = imp.getRoi();
        FloatPolygon p = roi.getFloatPolygon();
        PointRoi pointRoi = (PointRoi)roi;
        for (int i = 0; i < p.npoints; i++) {
            int position = 0;
            position = pointRoi.getPointPosition(i);
            int xc = (int)Math.round(p.xpoints[i]);
            int yc = (int)Math.round(p.ypoints[i]);

            imp.setSlice(position);

            int x0 = xc - (int)(size / 2);
            int y0 = yc - (int)(size / 2);

            Roi rectangleRoi = new Roi(x0, y0, size, size);
            Roi[] rois = {rectangleRoi};
            imp.cropAndSave(rois, dir, "tif");
        }
	}
}

ポイント

Dialogの表示方法と値の取得方法

下記のように記述する。GenericDialogクラスの仕様はこちら

GenericDialog gd = new GenericDialog("Input");
gd.addDirectoryField("Output directory", arg);
gd.showDialog();
gd.getNextString();

Multi-pointで選択した地点のスライス番号(position)とX, Y座標(x, y)の取得方法

下記のように記述する。

ImagePlus imp = IJ.getImage();
Roi roi = imp.getRoi();
FloatPolygon p = roi.getFloatPolygon();
PointRoi pointRoi = (PointRoi)roi;
for (int i = 0; i < p.npoints; i++) {
    int position = 0;
    position = pointRoi.getPointPosition(i);
    int x = (int)Math.round(p.xpoints[i]);
    int y = (int)Math.round(p.ypoints[i]);
    IJ.log("Position, X, Y: " + position + ", " + x + ", " + y);
}

ROIの画像を保存する方法

Roi rectangleRoi = new Roi(x0, y0, size, size);
Roi[] rois = {rectangleRoi};
imp.cropAndSave(rois, dir, "tif");
1
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
1
0