{
  "id": 47769,
  "title": "Processing masks with transparency",
  "url": "/competitions/data-science-bowl-2018/discussion/47769",
  "author_name": "",
  "post_date": "2018-01-18T16:45:26.105563500Z",
  "votes": null,
  "comment_count": 3,
  "views": 0,
  "content": "<p>This may or may not be useful to others.</p>\n\n<p>The training data masks use the colour white for the 'hole' in the mask. Because the masks and images are .pngs it is straightforward to convert the white areas to be transparent.</p>\n\n<p>I used GraphicsMagick:\ngm mogrify -transparent white -output-directory ..\\transparent_masks *.png</p>\n\n<p>Having created the directory transparent_masks beforehand.\nI tested the output using Gimp and the mask pasted over the image as a layer shows through. So my intention is to preprocess all of the masks to be transparent and then to merge all of the masks for each image as a separate set of layers. </p>\n\n<p>I can then run a training loop for each image, processing the layers with an image library. This should significantly reduce the time to process each image compared to XOR'ing the masks one by one.</p>",
  "messages": [
    {
      "id": "270638",
      "postDate": "01/18/2018 16:45:26",
      "content": "<p>This may or may not be useful to others.</p>\n\n<p>The training data masks use the colour white for the 'hole' in the mask. Because the masks and images are .pngs it is straightforward to convert the white areas to be transparent.</p>\n\n<p>I used GraphicsMagick:\ngm mogrify -transparent white -output-directory ..\\transparent_masks *.png</p>\n\n<p>Having created the directory transparent_masks beforehand.\nI tested the output using Gimp and the mask pasted over the image as a layer shows through. So my intention is to preprocess all of the masks to be transparent and then to merge all of the masks for each image as a separate set of layers. </p>\n\n<p>I can then run a training loop for each image, processing the layers with an image library. This should significantly reduce the time to process each image compared to XOR'ing the masks one by one.</p>",
      "rawMarkdown": "This may or may not be useful to others.\n\nThe training data masks use the colour white for the 'hole' in the mask. Because the masks and images are .pngs it is straightforward to convert the white areas to be transparent.\n\nI used GraphicsMagick:\ngm mogrify -transparent white -output-directory ..\\transparent_masks *.png\n\nHaving created the directory transparent_masks beforehand.\nI tested the output using Gimp and the mask pasted over the image as a layer shows through. So my intention is to preprocess all of the masks to be transparent and then to merge all of the masks for each image as a separate set of layers. \n\nI can then run a training loop for each image, processing the layers with an image library. This should significantly reduce the time to process each image compared to XOR'ing the masks one by one.",
      "votes": null
    },
    {
      "id": "270646",
      "postDate": "01/18/2018 16:58:47",
      "content": "<p>If  I do build as permanent layers then I'll use the APNG format</p>",
      "rawMarkdown": "If  I do build as permanent layers then I'll use the APNG format",
      "votes": null
    },
    {
      "id": "270783",
      "postDate": "01/18/2018 23:02:45",
      "content": "<p>You can also simply add the individual masks together - they are just a numpy array.\nFor ex:</p>\n\n<pre><code>import numpy as np\nfrom skimage.io import imread\ndef masks_combiner(masks_path_list):\n\"\"\"\nparams:\nmasks_path_list: a list of file paths pointing to each mask\n\nreturns a np.array which is the sum of all masks provided.\n\"\"\"\nmasks_list = np.array([imread(m) for m in masks_path_list])\nall_masks_in_one = np.sum(masks_list, axis=0)\n# following assertion makes sure that there are no overlaping pixels in the masks provided\n# comp. organiser said there shouldn't be any overlap.\nassert(np.unique(all_masks_in_one).tolist() == [0,255])\n\nreturn all_masks_in_one.astype(np.uint8)\n</code></pre>",
      "rawMarkdown": "You can also simply add the individual masks together - they are just a numpy array.\nFor ex:\n\n\n    import numpy as np\n    from skimage.io import imread\n    def masks_combiner(masks_path_list):\n    \"\"\"\n    params:\n    masks_path_list: a list of file paths pointing to each mask\n\n    returns a np.array which is the sum of all masks provided.\n    \"\"\"\n    masks_list = np.array([imread(m) for m in masks_path_list])\n    all_masks_in_one = np.sum(masks_list, axis=0)\n    # following assertion makes sure that there are no overlaping pixels in the masks provided\n    # comp. organiser said there shouldn't be any overlap.\n    assert(np.unique(all_masks_in_one).tolist() == [0,255])\n\n    return all_masks_in_one.astype(np.uint8)",
      "votes": null
    },
    {
      "id": "270986",
      "postDate": "01/19/2018 11:40:20",
      "content": "<p>Excellent point.</p>",
      "rawMarkdown": "Excellent point.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 270646,
      "author_name": "bpluly",
      "author_url": "",
      "post_date": "01/18/2018 16:58:47",
      "content": "<p>If  I do build as permanent layers then I'll use the APNG format</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 270783,
      "author_name": "sashikanthdareddy",
      "author_url": "",
      "post_date": "01/18/2018 23:02:45",
      "content": "<p>You can also simply add the individual masks together - they are just a numpy array.\nFor ex:</p>\n\n<pre><code>import numpy as np\nfrom skimage.io import imread\ndef masks_combiner(masks_path_list):\n\"\"\"\nparams:\nmasks_path_list: a list of file paths pointing to each mask\n\nreturns a np.array which is the sum of all masks provided.\n\"\"\"\nmasks_list = np.array([imread(m) for m in masks_path_list])\nall_masks_in_one = np.sum(masks_list, axis=0)\n# following assertion makes sure that there are no overlaping pixels in the masks provided\n# comp. organiser said there shouldn't be any overlap.\nassert(np.unique(all_masks_in_one).tolist() == [0,255])\n\nreturn all_masks_in_one.astype(np.uint8)\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 270986,
          "author_name": "bpluly",
          "author_url": "",
          "post_date": "01/19/2018 11:40:20",
          "content": "<p>Excellent point.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "270638": "This may or may not be useful to others.\n\nThe training data masks use the colour white for the 'hole' in the mask. Because the masks and images are .pngs it is straightforward to convert the white areas to be transparent.\n\nI used GraphicsMagick:\ngm mogrify -transparent white -output-directory ..\\transparent_masks *.png\n\nHaving created the directory transparent_masks beforehand.\nI tested the output using Gimp and the mask pasted over the image as a layer shows through. So my intention is to preprocess all of the masks to be transparent and then to merge all of the masks for each image as a separate set of layers. \n\nI can then run a training loop for each image, processing the layers with an image library. This should significantly reduce the time to process each image compared to XOR'ing the masks one by one.",
    "270646": "If  I do build as permanent layers then I'll use the APNG format",
    "270783": "You can also simply add the individual masks together - they are just a numpy array.\nFor ex:\n\n\n    import numpy as np\n    from skimage.io import imread\n    def masks_combiner(masks_path_list):\n    \"\"\"\n    params:\n    masks_path_list: a list of file paths pointing to each mask\n\n    returns a np.array which is the sum of all masks provided.\n    \"\"\"\n    masks_list = np.array([imread(m) for m in masks_path_list])\n    all_masks_in_one = np.sum(masks_list, axis=0)\n    # following assertion makes sure that there are no overlaping pixels in the masks provided\n    # comp. organiser said there shouldn't be any overlap.\n    assert(np.unique(all_masks_in_one).tolist() == [0,255])\n\n    return all_masks_in_one.astype(np.uint8)",
    "270986": "Excellent point."
  },
  "source": "meta"
}