Save multipoints in image stack to csv and reopen it

Hi,

I am not very familiar with the differences between selection, ROI, and overlay in ImageJ. My use case is this:

In an image stack, I want to annotate certain features. The xy-positions and the z-position (slice) is saved to a csv file and analysed with a separate script. Additionally, I want to reopen the analysed image together with the csv and recreate the annotations such that they can easily be edited and saved again.

For this, I chose to use the multipoint tool. It has these benefits:

  • Points can be dragged around
  • Points can easily be deleted
  • Points are only shown in their slices

I save the positions of each feature with this macro to a csv file.

function save_results(path, id) {
	run("Clear Results");
	run("Measure");
	saveAs("Results", path);
	close("Results");
    selectImage(id);
    run("Select None");
    // close();
}

function get_path() {
	dir = getDir("image");
	name = getTitle();
	name = File.getNameWithoutExtension(name);
	path = dir + name;
	return path;
}

macro "Save Action Tool - icon:pers.png" {
	id = getImageID();
	path = get_path();
	path = path + "_pers.csv";
	save_results(path, id);
}

But I struggle to recreate the multipoint selection based on this csv file. With the help of Export / Import Multi-point set [ImageJ Documentation Wiki] I created this macro

macro "Open Action Tool - icon:open.png" {
	run("Select None");
	id = getImageID();
	path = get_path();
	path = path + "_pers.csv";
	
	allText = File.openAsString(path);
	text = split(allText, "\n");

	iX = 1; iY = 2; iSlice = 3;
	for (i = 1; i < (text.length); i++){
		line = split(text[i], ",");

		x = parseFloat(line[iX]);
		y = parseFloat(line[iY]);
		slice = parseInt(line[iSlice]);

		setKeyDown("shift"); makePoint(x, y, "hybrid small yellow add");
		Overlay.setPosition(slice);
	}
}

This was the only combination of the commands makePoint and makeSelection I found that displayed the points only in the correct slices. However, now the points are overlays. Therefore, I cannot interact with them as easily as before. Furthermore, when I add new points with the multipoint tool I need to implement a lot of logic to save the new points alongside with the old overlay points into one csv. There must be an easier way to do this. Can you point me to this easier direction?

Hi,
This does not use your csv file , but you could add the multipoint selection(s) to the ROI Manager and save them as a roi or zip file using RoiManager More>> Save… command.
Then you can easily reopen them using RoiManager More>> Open… command.
Jerome

Hi,
thanks a lot for this tip! I managed to do what I want to do with these macros:

function save_results(path, id) {
	roiManager("Add");
	roiManager("Save", path);
	roiManager("Select", 0);
	roiManager("Delete");
	selectImage(id);
}

function get_path() {
	dir = getDir("image");
	name = getTitle();
	name = File.getNameWithoutExtension(name);
	path = dir + name;
	return path;
}

macro "Save Action Tool - icon:pers.png" {
	id = getImageID();
	path = get_path();
	path = path + "_pers.roi";
	save_results(path, id);
}

macro "Open Action Tool - icon:open.png" {
	id = getImageID();
	path = get_path();
	path = path + "_pers.roi";

	roiManager("Open", path);
	roiManager("Select", 0);
	roiManager("Delete");
	selectImage(id);
	}
}

I delete the ROI after saving and opening because then all points that are added after that step are added to one ROI and not different ROIs.

There are also several Python packages to read the roi-files

Best,
Niklas

1 Like