Created
November 18, 2020 14:16
-
-
Save romainGuiet/288dfec7fc2534fe7af027f88a971f9b to your computer and use it in GitHub Desktop.
#macro #xml #LabelImg #ZeroCostDL4Mic #BIOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Romain Guiet | |
* EPFL | |
* Nov 2020 | |
* | |
* adapted from : https://raw.githubusercontent.com/bvernay/ImageJ-Macros/master/xml_annotations_tools/roiTOXML.ijm | |
* Bertrand Vernay | |
* Imaging Centre IGBMC | |
* vernayb@igbmc.fr | |
* September 2020 | |
* | |
* Requirement: | |
* - a folder containing a png image and a list of position with categories | |
* | |
* How to use: | |
* - start the macro | |
* - select the image | |
* - once the macro is finished, open the txt file in Fiji and save as .xml | |
* (RG : you can also just change the file format manually) | |
* - the xml file in now readable by LabelImg https://github.com/tzutalin/labelImg | |
* | |
* Improvement: | |
* - why changign txt to xml does not make the xml file readbale. Have to open iin Fiji and save as xml. | |
* | |
*/ | |
#@ File img_path | |
run("Close All"); | |
run("Clear Results"); | |
print("\\Clear"); | |
roiManager("reset"); | |
boxSize = 96; | |
// Open the png file | |
open(img_path); | |
dir = File.getParent(img_path)+File.separator; | |
name = File.nameWithoutExtension; | |
run("Results... ", "open=["+dir+name+"_posList.txt]"); | |
// Get the name of the image containing folder | |
parentDir = File.getParent(dir); | |
folder = substring(dir, lengthOf(parentDir)+1, lengthOf(dir)-1 ); | |
//get the dimensions of the image | |
getDimensions(width, height, channels, slices, frames); | |
// Open the corresponding RoiSet | |
//open(dir+name+"_RoiSet.zip"); | |
// XML format for LabelImg (https://github.com/tzutalin/labelImg) | |
/* | |
<?xml version='1.0'?>" | |
<annotation> | |
<folder></folder> | |
<filename></filename> | |
<path></path> | |
<source> | |
<database></database> | |
</source> | |
<size> | |
<width></width> | |
<height></height> | |
<depth></depth> | |
</size> | |
<segmented></segmented> | |
<object> | |
<name></name> | |
<pose></pose> | |
<truncated></truncated> | |
<difficult></difficult> | |
<bndbox> | |
<xmin></xmin> | |
<ymin></ymin> | |
<xmax></xmax> | |
<ymax></ymax> | |
</bndbox> | |
</object> | |
</annotation> | |
*/ | |
// Builging the text file in log for final xml | |
print("<annotation>"); | |
print("\t<folder>"+folder+"</folder>"); | |
print("\t<filename>"+name+".png</filename>"); | |
print("\t<path>"+dir+name+".png</path>"); | |
print("\t<source>"); | |
print("\t\t<database>Unknown</database>"); | |
print("\t</source>"); | |
print("\t<size>"); | |
print("\t\t<width>"+width+"</width>"); | |
print("\t\t<height>"+height+"</height>"); | |
print("\t\t<depth>1"+"</depth>"); | |
print("\t</size>"); | |
print("\t<segmented>0</segmented>"); | |
roisN = nResults; | |
//print (roisN +" images to export"); | |
for (i = 0 ; i < roisN ; i ++){ | |
type = getResult("Type", i); | |
if (type == 1){ | |
tagName = "alive"; | |
} else if (type == 2){ | |
tagName = "dead"; | |
} | |
x = getResult("X", i); | |
y = getResult("Y", i); | |
makeRectangle(x-boxSize/2, y-boxSize/2, boxSize, boxSize); | |
getBoundingRect(x, y, roiWidth, roiHeight); | |
print("\t<object>"); | |
print("\t\t<name>"+tagName+"</name>"); | |
print("\t\t<pose>Unspecified</pose>"); | |
print("\t\t<truncated>0</truncated>"); | |
print("\t\t<difficult>0</difficult>"); | |
//roiManager("select", i); | |
print("\t\t<bndbox>"); | |
print("\t\t\t<xmin>"+x+"</xmin>"); | |
print("\t\t\t<ymin>"+y+"</ymin>"); | |
print("\t\t\t<xmax>"+(roiWidth+x)+"</xmax>"); | |
print("\t\t\t<ymax>"+(roiHeight+y)+"</ymax>"); | |
print("\t\t</bndbox>"); | |
print("\t</object>"); | |
} | |
print("</annotation>"); | |
// Save log window as ."image name".txt | |
selectWindow("Log"); | |
saveAs("Text", dir+name+".txt"); | |
close(); | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment