{
  "id": 284620,
  "title": "Python Functions",
  "url": "/competitions/sartorius-cell-instance-segmentation/discussion/284620",
  "author_name": "",
  "post_date": "2021-11-01T18:41:31.056270100Z",
  "votes": 1,
  "comment_count": 1,
  "views": 0,
  "content": "<p>I have seen in this notebook <a href=\"https://www.kaggle.com/remananr/image-rotation-using-opencv\" target=\"_blank\">https://www.kaggle.com/remananr/image-rotation-using-opencv</a> the following:</p>\n<pre><code>def read_mask(imgID, img_size):\n  mask = np.zeros(img_size, dtype=np.uint8)\n  for i in range(len(df[df.id == imgID])):\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask)\n  mask = np.clip(mask, 0,1)  \n  return mask\n</code></pre>\n<p>however I don't comprehend well what np.zeros(img_size, dtype = np.uint8) do, and also np.clip(mask, 0, 1), df[df.id == imgID].annotation.iloc[i] and rle_decode(df[df.id == imgID].annotation.iloc[i], mask). I'd like also to encourage people to solve this type of question for it's helpful as a repository for this theme. </p>",
  "messages": [
    {
      "id": "1567300",
      "postDate": "11/01/2021 18:41:31",
      "content": "<p>I have seen in this notebook <a href=\"https://www.kaggle.com/remananr/image-rotation-using-opencv\" target=\"_blank\">https://www.kaggle.com/remananr/image-rotation-using-opencv</a> the following:</p>\n<pre><code>def read_mask(imgID, img_size):\n  mask = np.zeros(img_size, dtype=np.uint8)\n  for i in range(len(df[df.id == imgID])):\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask)\n  mask = np.clip(mask, 0,1)  \n  return mask\n</code></pre>\n<p>however I don't comprehend well what np.zeros(img_size, dtype = np.uint8) do, and also np.clip(mask, 0, 1), df[df.id == imgID].annotation.iloc[i] and rle_decode(df[df.id == imgID].annotation.iloc[i], mask). I'd like also to encourage people to solve this type of question for it's helpful as a repository for this theme. </p>",
      "rawMarkdown": "I have seen in this notebook https://www.kaggle.com/remananr/image-rotation-using-opencv the following:\n\n```\ndef read_mask(imgID, img_size):\n  mask = np.zeros(img_size, dtype=np.uint8)\n  for i in range(len(df[df.id == imgID])):\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask)\n  mask = np.clip(mask, 0,1)  \n  return mask\n```\n\nhowever I don't comprehend well what np.zeros(img_size, dtype = np.uint8) do, and also np.clip(mask, 0, 1), df[df.id == imgID].annotation.iloc[i] and rle_decode(df[df.id == imgID].annotation.iloc[i], mask). I'd like also to encourage people to solve this type of question for it's helpful as a repository for this theme.",
      "votes": null
    },
    {
      "id": "1567410",
      "postDate": "11/01/2021 20:57:56",
      "content": "<p>I think this turns a Run Length Encoding into a bitmask.</p>\n<p>np.zeros - creates an image (dataframe) that is all zeros. Size of the passed image.</p>\n<p>for each run length encoded entry in the \"df\" dataframe for the passed image:<br>\n    add \"1\" to the mask for the position of the annotation. Uses a function rle_decode to convert from RLE to bitmask.<br>\nlimits the mask to 0 to 1, in case there were overlapping annotations (which we know this contest has).</p>\n<p>def read_mask(imgID, img_size):<br>\n  mask = np.zeros(img_size, dtype=np.uint8)<br>\n  for i in range(len(df[df.id == imgID])): # for each entry in the list of annotations for this ImgID<br>\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask) #add the bitmask for this one annotation to the total mask<br>\n  mask = np.clip(mask, 0,1)  #limit the mask to 0 to 1. Otherwise overlapping masks would give you a range of values.<br>\n  return mask</p>\n<p>Python is not my native language, so I'm sure I've missed some nuances.</p>",
      "rawMarkdown": "I think this turns a Run Length Encoding into a bitmask.\n\nnp.zeros - creates an image (dataframe) that is all zeros. Size of the passed image.\n\nfor each run length encoded entry in the \"df\" dataframe for the passed image:\n    add \"1\" to the mask for the position of the annotation. Uses a function rle_decode to convert from RLE to bitmask.\nlimits the mask to 0 to 1, in case there were overlapping annotations (which we know this contest has).\n\ndef read_mask(imgID, img_size):\n  mask = np.zeros(img_size, dtype=np.uint8)\n  for i in range(len(df[df.id == imgID])): # for each entry in the list of annotations for this ImgID\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask) #add the bitmask for this one annotation to the total mask\n  mask = np.clip(mask, 0,1)  #limit the mask to 0 to 1. Otherwise overlapping masks would give you a range of values.\n  return mask\n\n\nPython is not my native language, so I'm sure I've missed some nuances.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1567410,
      "author_name": "richardepstein",
      "author_url": "",
      "post_date": "11/01/2021 20:57:56",
      "content": "<p>I think this turns a Run Length Encoding into a bitmask.</p>\n<p>np.zeros - creates an image (dataframe) that is all zeros. Size of the passed image.</p>\n<p>for each run length encoded entry in the \"df\" dataframe for the passed image:<br>\n    add \"1\" to the mask for the position of the annotation. Uses a function rle_decode to convert from RLE to bitmask.<br>\nlimits the mask to 0 to 1, in case there were overlapping annotations (which we know this contest has).</p>\n<p>def read_mask(imgID, img_size):<br>\n  mask = np.zeros(img_size, dtype=np.uint8)<br>\n  for i in range(len(df[df.id == imgID])): # for each entry in the list of annotations for this ImgID<br>\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask) #add the bitmask for this one annotation to the total mask<br>\n  mask = np.clip(mask, 0,1)  #limit the mask to 0 to 1. Otherwise overlapping masks would give you a range of values.<br>\n  return mask</p>\n<p>Python is not my native language, so I'm sure I've missed some nuances.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1567300": "I have seen in this notebook https://www.kaggle.com/remananr/image-rotation-using-opencv the following:\n\n```\ndef read_mask(imgID, img_size):\n  mask = np.zeros(img_size, dtype=np.uint8)\n  for i in range(len(df[df.id == imgID])):\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask)\n  mask = np.clip(mask, 0,1)  \n  return mask\n```\n\nhowever I don't comprehend well what np.zeros(img_size, dtype = np.uint8) do, and also np.clip(mask, 0, 1), df[df.id == imgID].annotation.iloc[i] and rle_decode(df[df.id == imgID].annotation.iloc[i], mask). I'd like also to encourage people to solve this type of question for it's helpful as a repository for this theme.",
    "1567410": "I think this turns a Run Length Encoding into a bitmask.\n\nnp.zeros - creates an image (dataframe) that is all zeros. Size of the passed image.\n\nfor each run length encoded entry in the \"df\" dataframe for the passed image:\n    add \"1\" to the mask for the position of the annotation. Uses a function rle_decode to convert from RLE to bitmask.\nlimits the mask to 0 to 1, in case there were overlapping annotations (which we know this contest has).\n\ndef read_mask(imgID, img_size):\n  mask = np.zeros(img_size, dtype=np.uint8)\n  for i in range(len(df[df.id == imgID])): # for each entry in the list of annotations for this ImgID\n    mask += rle_decode(df[df.id == imgID].annotation.iloc[i], mask) #add the bitmask for this one annotation to the total mask\n  mask = np.clip(mask, 0,1)  #limit the mask to 0 to 1. Otherwise overlapping masks would give you a range of values.\n  return mask\n\n\nPython is not my native language, so I'm sure I've missed some nuances."
  },
  "source": "meta"
}