{
  "id": 229186,
  "title": "Medical image labeling guide with JSON output",
  "url": "/competitions/hubmap-kidney-segmentation/discussion/229186",
  "author_name": "",
  "post_date": "2021-03-28T18:43:16.968491900Z",
  "votes": 14,
  "comment_count": 5,
  "views": 0,
  "content": "<p>I created this post to share the most efficient way to annotate medical images. You need to download a free and open source app called QuPath to create the annotations. Using the script below, QuPath will generate a JSON file which can be used in python. The YouTube video shows how to use the app and the script.</p>\n<p>To be consistent with the competition dataset, you need to create only one class in QuPath called <code>1</code> and only annotate the glomeruli. The python code automatically sets the background label to <code>0</code>.</p>\n<p>In the video tutorial I create two masks with different tools for demonstration purposes; any of them work fine.</p>\n<p><strong>My QuPath Tutorial on YouTube:</strong><br>\n<a href=\"https://youtu.be/ujEf_5c85CE\" target=\"_blank\">https://youtu.be/ujEf_5c85CE</a><br>\n<a href=\"https://youtu.be/ujEf_5c85CE\" target=\"_blank\"><img src=\"https://i.postimg.cc/bsG44S4z/Screen-Shot-2021-03-28-at-2-37-26-PM.png\"></a></p>\n<p><strong>Notebook for converting JSON file into mask</strong><br>\n<a href=\"https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath\" target=\"_blank\">https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath</a></p>\n<p><strong>Download QuPath:</strong><br>\n<a href=\"https://qupath.github.io\" target=\"_blank\">https://qupath.github.io</a></p>\n<p><strong>Script for exporting labels to JSON file:</strong></p>\n<pre><code>def annotations = getAnnotationObjects()\nboolean prettyPrint = true\ndef gson = GsonTools.getInstance(prettyPrint)\n//println gson.toJson(annotations)\n\n// Get the imageData &amp; server\ndef imageData = QPEx.getCurrentImageData()\ndef server = imageData.getServer()\n\nString path = server.getPath()\n\n// automatic output filename, otherwise set explicitly\nrt_path = \"/Users/[REDACTED]/Desktop/HuBMAP - Hacking the Kidney/\"\n//fname = \"test_image\"\n//println path[path.lastIndexOf(':')+1..-5]\n\n//outfname = rt_path+path[path.lastIndexOf(':')+93..-5]+\".json\"\noutfname = rt_path+\"test_mask.json\"\n//println outfname\n\nFile file = new File(outfname)\nfile.withWriter('UTF-8') {\n    gson.toJson(annotations,it)\n}\n</code></pre>",
  "messages": [
    {
      "id": "1255393",
      "postDate": "03/28/2021 18:43:16",
      "content": "<p>I created this post to share the most efficient way to annotate medical images. You need to download a free and open source app called QuPath to create the annotations. Using the script below, QuPath will generate a JSON file which can be used in python. The YouTube video shows how to use the app and the script.</p>\n<p>To be consistent with the competition dataset, you need to create only one class in QuPath called <code>1</code> and only annotate the glomeruli. The python code automatically sets the background label to <code>0</code>.</p>\n<p>In the video tutorial I create two masks with different tools for demonstration purposes; any of them work fine.</p>\n<p><strong>My QuPath Tutorial on YouTube:</strong><br>\n<a href=\"https://youtu.be/ujEf_5c85CE\" target=\"_blank\">https://youtu.be/ujEf_5c85CE</a><br>\n<a href=\"https://youtu.be/ujEf_5c85CE\" target=\"_blank\"><img src=\"https://i.postimg.cc/bsG44S4z/Screen-Shot-2021-03-28-at-2-37-26-PM.png\"></a></p>\n<p><strong>Notebook for converting JSON file into mask</strong><br>\n<a href=\"https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath\" target=\"_blank\">https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath</a></p>\n<p><strong>Download QuPath:</strong><br>\n<a href=\"https://qupath.github.io\" target=\"_blank\">https://qupath.github.io</a></p>\n<p><strong>Script for exporting labels to JSON file:</strong></p>\n<pre><code>def annotations = getAnnotationObjects()\nboolean prettyPrint = true\ndef gson = GsonTools.getInstance(prettyPrint)\n//println gson.toJson(annotations)\n\n// Get the imageData &amp; server\ndef imageData = QPEx.getCurrentImageData()\ndef server = imageData.getServer()\n\nString path = server.getPath()\n\n// automatic output filename, otherwise set explicitly\nrt_path = \"/Users/[REDACTED]/Desktop/HuBMAP - Hacking the Kidney/\"\n//fname = \"test_image\"\n//println path[path.lastIndexOf(':')+1..-5]\n\n//outfname = rt_path+path[path.lastIndexOf(':')+93..-5]+\".json\"\noutfname = rt_path+\"test_mask.json\"\n//println outfname\n\nFile file = new File(outfname)\nfile.withWriter('UTF-8') {\n    gson.toJson(annotations,it)\n}\n</code></pre>",
      "rawMarkdown": "I created this post to share the most efficient way to annotate medical images. You need to download a free and open source app called QuPath to create the annotations. Using the script below, QuPath will generate a JSON file which can be used in python. The YouTube video shows how to use the app and the script.\n\nTo be consistent with the competition dataset, you need to create only one class in QuPath called `1` and only annotate the glomeruli. The python code automatically sets the background label to `0`.\n\nIn the video tutorial I create two masks with different tools for demonstration purposes; any of them work fine.\n\n**My QuPath Tutorial on YouTube:**\n[https://youtu.be/ujEf_5c85CE](https://youtu.be/ujEf_5c85CE)\n<a href='https://youtu.be/ujEf_5c85CE' target='_blank'><img src='https://i.postimg.cc/bsG44S4z/Screen-Shot-2021-03-28-at-2-37-26-PM.png' border='0'/></a>\n\n**Notebook for converting JSON file into mask**\n[https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath](https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath)\n\n**Download QuPath:**\n[https://qupath.github.io](https://qupath.github.io)\n\n**Script for exporting labels to JSON file:**\n\n```\ndef annotations = getAnnotationObjects()\nboolean prettyPrint = true\ndef gson = GsonTools.getInstance(prettyPrint)\n//println gson.toJson(annotations)\n   \n// Get the imageData & server\ndef imageData = QPEx.getCurrentImageData()\ndef server = imageData.getServer()\n\nString path = server.getPath()\n    \n// automatic output filename, otherwise set explicitly\nrt_path = \"/Users/[REDACTED]/Desktop/HuBMAP - Hacking the Kidney/\"\n//fname = \"test_image\"\n//println path[path.lastIndexOf(':')+1..-5]\n   \n//outfname = rt_path+path[path.lastIndexOf(':')+93..-5]+\".json\"\noutfname = rt_path+\"test_mask.json\"\n//println outfname\n   \nFile file = new File(outfname)\nfile.withWriter('UTF-8') {\n    gson.toJson(annotations,it)\n}\n```",
      "votes": null
    },
    {
      "id": "1265282",
      "postDate": "04/06/2021 18:58:28",
      "content": "<p>Great post!</p>",
      "rawMarkdown": "Great post!",
      "votes": null
    },
    {
      "id": "1267928",
      "postDate": "04/09/2021 00:51:54",
      "content": "<p>Thx for sharing!!!</p>",
      "rawMarkdown": "Thx for sharing!!!",
      "votes": null
    },
    {
      "id": "1276730",
      "postDate": "04/17/2021 22:27:07",
      "content": "<p>Do you know how to load json file to Qupath? Thanks!</p>",
      "rawMarkdown": "Do you know how to load json file to Qupath? Thanks!",
      "votes": null
    },
    {
      "id": "1276752",
      "postDate": "04/17/2021 23:15:55",
      "content": "<p>The easiest way would be to save the mask as a grayscale tiff and import it into QuPath. Then create a mask using the wand tool and subtraction.</p>\n<p>I'm sure the QuPath python API has a quicker way to reverse the script above but I don't have time to read through it.</p>",
      "rawMarkdown": "The easiest way would be to save the mask as a grayscale tiff and import it into QuPath. Then create a mask using the wand tool and subtraction.\n\nI'm sure the QuPath python API has a quicker way to reverse the script above but I don't have time to read through it.",
      "votes": null
    },
    {
      "id": "1287668",
      "postDate": "04/29/2021 09:46:10",
      "content": "<p>Cheers mate. Very helpful post!</p>",
      "rawMarkdown": "Cheers mate. Very helpful post!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1265282,
      "author_name": "lililycai",
      "author_url": "",
      "post_date": "04/06/2021 18:58:28",
      "content": "<p>Great post!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1267928,
      "author_name": "lrchan",
      "author_url": "",
      "post_date": "04/09/2021 00:51:54",
      "content": "<p>Thx for sharing!!!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1276730,
      "author_name": "lililycai",
      "author_url": "",
      "post_date": "04/17/2021 22:27:07",
      "content": "<p>Do you know how to load json file to Qupath? Thanks!</p>",
      "votes": null,
      "replies": [
        {
          "id": 1276752,
          "author_name": "erikdali",
          "author_url": "",
          "post_date": "04/17/2021 23:15:55",
          "content": "<p>The easiest way would be to save the mask as a grayscale tiff and import it into QuPath. Then create a mask using the wand tool and subtraction.</p>\n<p>I'm sure the QuPath python API has a quicker way to reverse the script above but I don't have time to read through it.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1287668,
      "author_name": "legisam",
      "author_url": "",
      "post_date": "04/29/2021 09:46:10",
      "content": "<p>Cheers mate. Very helpful post!</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1255393": "I created this post to share the most efficient way to annotate medical images. You need to download a free and open source app called QuPath to create the annotations. Using the script below, QuPath will generate a JSON file which can be used in python. The YouTube video shows how to use the app and the script.\n\nTo be consistent with the competition dataset, you need to create only one class in QuPath called `1` and only annotate the glomeruli. The python code automatically sets the background label to `0`.\n\nIn the video tutorial I create two masks with different tools for demonstration purposes; any of them work fine.\n\n**My QuPath Tutorial on YouTube:**\n[https://youtu.be/ujEf_5c85CE](https://youtu.be/ujEf_5c85CE)\n<a href='https://youtu.be/ujEf_5c85CE' target='_blank'><img src='https://i.postimg.cc/bsG44S4z/Screen-Shot-2021-03-28-at-2-37-26-PM.png' border='0'/></a>\n\n**Notebook for converting JSON file into mask**\n[https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath](https://www.kaggle.com/erikdali/biomedical-labeling-with-qupath)\n\n**Download QuPath:**\n[https://qupath.github.io](https://qupath.github.io)\n\n**Script for exporting labels to JSON file:**\n\n```\ndef annotations = getAnnotationObjects()\nboolean prettyPrint = true\ndef gson = GsonTools.getInstance(prettyPrint)\n//println gson.toJson(annotations)\n   \n// Get the imageData & server\ndef imageData = QPEx.getCurrentImageData()\ndef server = imageData.getServer()\n\nString path = server.getPath()\n    \n// automatic output filename, otherwise set explicitly\nrt_path = \"/Users/[REDACTED]/Desktop/HuBMAP - Hacking the Kidney/\"\n//fname = \"test_image\"\n//println path[path.lastIndexOf(':')+1..-5]\n   \n//outfname = rt_path+path[path.lastIndexOf(':')+93..-5]+\".json\"\noutfname = rt_path+\"test_mask.json\"\n//println outfname\n   \nFile file = new File(outfname)\nfile.withWriter('UTF-8') {\n    gson.toJson(annotations,it)\n}\n```",
    "1265282": "Great post!",
    "1267928": "Thx for sharing!!!",
    "1276730": "Do you know how to load json file to Qupath? Thanks!",
    "1276752": "The easiest way would be to save the mask as a grayscale tiff and import it into QuPath. Then create a mask using the wand tool and subtraction.\n\nI'm sure the QuPath python API has a quicker way to reverse the script above but I don't have time to read through it.",
    "1287668": "Cheers mate. Very helpful post!"
  },
  "source": "meta"
}