From ece8f048de1cf8fc4e5b024fbc1bb48ac28fdcc4 Mon Sep 17 00:00:00 2001
From: Kai Schleicher <kai.schleicher@unibas.ch>
Date: Tue, 13 Dec 2022 11:55:05 +0000
Subject: [PATCH] initial commit

---
 remap_labelimage.py | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 remap_labelimage.py

diff --git a/remap_labelimage.py b/remap_labelimage.py
new file mode 100644
index 0000000..3214664
--- /dev/null
+++ b/remap_labelimage.py
@@ -0,0 +1,29 @@
+#@ File inputfile
+
+from ij import IJ
+from inra.ijpb.label import LabelImages
+from ij.measure import ResultsTable
+import os.path
+
+path, ext = os.path.splitext(str(inputfile))
+parent_dir, filename = os.path.split(path)
+
+label_imp = IJ.openImage(str(inputfile))
+IJ.log("remapping labels...")
+original_labels = LabelImages.findAllLabels(label_imp)
+LabelImages.remapLabels(label_imp)
+remapped_labels = LabelImages.findAllLabels(label_imp)
+atlas_map = ResultsTable()
+
+for index, value in enumerate(sorted(original_labels)):
+    atlas_map.setValue("original labels", index, value)
+
+for index, value in enumerate(sorted(remapped_labels)):
+    atlas_map.setValue("remapped labels", index, value)
+
+IJ.log("saving results...")
+IJ.run("Input/Output...", "file=.csv save_column save_row")
+atlas_map.save(parent_dir + "/" + filename + "_labelmap.csv")
+IJ.saveAs(label_imp, "Tiff", parent_dir + "/" + filename + "_remapped.tif")
+label_imp.close()
+IJ.log("done")
-- 
GitLab