{
  "id": 386518,
  "title": "Mammography image segmentation",
  "url": "/competitions/rsna-breast-cancer-detection/discussion/386518",
  "author_name": "Denis Muriungi",
  "post_date": "2023-02-13T13:32:04.584000",
  "votes": 0,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Hello guys, I want to perform segmentation on my images so that I will train them using an Unet model ….so I want to create a function to mask theme images…i have already converted my images to png …so I want the function when I call the function should mask the image and visualize the original and the masked image…example of usage of the function.<br>\n<code>mask = create_mask(train_df,sample_img)</code></p>\n<p>This is example of the function I used to mask the images but didn't work pretty well.<br>\n`def create_mask(df: pd.DataFrame, image_name: str='img.png', shape: tuple=(350, 525)):</p>\n<pre><code>masks = np.zeros((shape[0], shape[1], 1), dtype=np.float32)\n\ndf = df[df[\"img_path\"] == image_name]\n\nfor idx, im_name in enumerate(df[\"img_path\"].values):\n\n    label = df.loc[df[\"img_path\"] == im_name, \"cancer\"].values[0]\n\n    mask = cv2.imread(f\"/kaggle/working/output/train/{label}/{im_name}\")\n\n    if mask is None:\n\n        continue\n\n    masks[:, :, 0] = mask[:, :, 0] / 255\n\nreturn masks\n        `\n</code></pre>\n<p>the masks output was dark…I am trying to use the techniques which was used here <a href=\"https://github.com/sneddy/pneumothorax-segmentation/blob/master/unet_pipeline/utils/prepare_png.py\" target=\"_blank\">Image Segmentation</a></p>",
  "messages": [
    {
      "id": 2142751,
      "postDate": "2023-02-13T18:50:52.887Z",
      "content": "<p>Hey Denis, the code isn't showing up properly, but I don't see how that could work. I saw <a href=\"https://www.kaggle.com/paulbacher\" target=\"_blank\">@paulbacher</a> processing notebook. This notebook is excellent btw, and thank you Paul for posting it. it had a masking technique. He used it to help with cropping but I think it works really well.</p>\n<p><a href=\"https://www.kaggle.com/code/paulbacher/custom-preprocessor-rsna-breast-cancer\" target=\"_blank\">https://www.kaggle.com/code/paulbacher/custom-preprocessor-rsna-breast-cancer</a></p>\n<p>Essentially this would be the code where img is your numpy of image shape (h,w)</p>\n<p>threshold = 0<br>\nbin_img = (img &gt; threshold).astype(np.uint8)<br>\ncontours, _ = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)<br>\ncontour = max(contours, key=cv2.contourArea)<br>\nmask = np.zeros(img.shape, np.uint8)<br>\ncv2.drawContours(mask, [contour], -1, 255, cv2.FILLED)</p>",
      "rawMarkdown": "Hey Denis, the code isn't showing up properly, but I don't see how that could work. I saw @paulbacher processing notebook. This notebook is excellent btw, and thank you Paul for posting it. it had a masking technique. He used it to help with cropping but I think it works really well.\n\nhttps://www.kaggle.com/code/paulbacher/custom-preprocessor-rsna-breast-cancer\n\nEssentially this would be the code where img is your numpy of image shape (h,w)\n\nthreshold = 0\nbin_img = (img > threshold).astype(np.uint8)\ncontours, _ = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\ncontour = max(contours, key=cv2.contourArea)\nmask = np.zeros(img.shape, np.uint8)\ncv2.drawContours(mask, [contour], -1, 255, cv2.FILLED)\n\n",
      "votes": 1
    },
    {
      "id": 2142829,
      "postDate": "2023-02-13T20:10:00.493Z",
      "content": "<p>Some key piece might be missing here. 🤔 Have you predicted segmentation masks with some method (e.g., CNN)? And if so, are those masks located in <code>\"/kaggle/working/output/train/{label}/{im_name}\"</code>? The RSNA competition data does not include pixel-wise segmentation masks of the lesions, but you indeed could estimate those from the data with suitable technology.</p>",
      "rawMarkdown": "Some key piece might be missing here. 🤔 Have you predicted segmentation masks with some method (e.g., CNN)? And if so, are those masks located in `\"/kaggle/working/output/train/{label}/{im_name}\"`? The RSNA competition data does not include pixel-wise segmentation masks of the lesions, but you indeed could estimate those from the data with suitable technology."
    },
    {
      "id": 2142367,
      "postDate": "2023-02-13T13:32:04.583Z",
      "content": "<p>Hello guys, I want to perform segmentation on my images so that I will train them using an Unet model ….so I want to create a function to mask theme images…i have already converted my images to png …so I want the function when I call the function should mask the image and visualize the original and the masked image…example of usage of the function.<br>\n<code>mask = create_mask(train_df,sample_img)</code></p>\n<p>This is example of the function I used to mask the images but didn't work pretty well.<br>\n`def create_mask(df: pd.DataFrame, image_name: str='img.png', shape: tuple=(350, 525)):</p>\n<pre><code>masks = np.zeros((shape[0], shape[1], 1), dtype=np.float32)\n\ndf = df[df[\"img_path\"] == image_name]\n\nfor idx, im_name in enumerate(df[\"img_path\"].values):\n\n    label = df.loc[df[\"img_path\"] == im_name, \"cancer\"].values[0]\n\n    mask = cv2.imread(f\"/kaggle/working/output/train/{label}/{im_name}\")\n\n    if mask is None:\n\n        continue\n\n    masks[:, :, 0] = mask[:, :, 0] / 255\n\nreturn masks\n        `\n</code></pre>\n<p>the masks output was dark…I am trying to use the techniques which was used here <a href=\"https://github.com/sneddy/pneumothorax-segmentation/blob/master/unet_pipeline/utils/prepare_png.py\" target=\"_blank\">Image Segmentation</a></p>",
      "rawMarkdown": "Hello guys, I want to perform segmentation on my images so that I will train them using an Unet model ....so I want to create a function to mask theme images...i have already converted my images to png ...so I want the function when I call the function should mask the image and visualize the original and the masked image...example of usage of the function.\n`mask = create_mask(train_df,sample_img)`\n\nThis is example of the function I used to mask the images but didn't work pretty well.\n`def create_mask(df: pd.DataFrame, image_name: str='img.png', shape: tuple=(350, 525)):\n\n    masks = np.zeros((shape[0], shape[1], 1), dtype=np.float32)\n\n    df = df[df[\"img_path\"] == image_name]\n\n    for idx, im_name in enumerate(df[\"img_path\"].values):\n\n        label = df.loc[df[\"img_path\"] == im_name, \"cancer\"].values[0]\n\n        mask = cv2.imread(f\"/kaggle/working/output/train/{label}/{im_name}\")\n\n        if mask is None:\n\n            continue\n\n        masks[:, :, 0] = mask[:, :, 0] / 255\n\n    return masks\n            `\n\nthe masks output was dark...I am trying to use the techniques which was used here [Image Segmentation](https://github.com/sneddy/pneumothorax-segmentation/blob/master/unet_pipeline/utils/prepare_png.py)"
    }
  ],
  "comments": [
    {
      "id": 2142751,
      "author_name": "Michael Bolton",
      "author_url": "",
      "post_date": "2023-02-13T18:50:52.887000",
      "content": "<p>Hey Denis, the code isn't showing up properly, but I don't see how that could work. I saw <a href=\"https://www.kaggle.com/paulbacher\" target=\"_blank\">@paulbacher</a> processing notebook. This notebook is excellent btw, and thank you Paul for posting it. it had a masking technique. He used it to help with cropping but I think it works really well.</p>\n<p><a href=\"https://www.kaggle.com/code/paulbacher/custom-preprocessor-rsna-breast-cancer\" target=\"_blank\">https://www.kaggle.com/code/paulbacher/custom-preprocessor-rsna-breast-cancer</a></p>\n<p>Essentially this would be the code where img is your numpy of image shape (h,w)</p>\n<p>threshold = 0<br>\nbin_img = (img &gt; threshold).astype(np.uint8)<br>\ncontours, _ = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)<br>\ncontour = max(contours, key=cv2.contourArea)<br>\nmask = np.zeros(img.shape, np.uint8)<br>\ncv2.drawContours(mask, [contour], -1, 255, cv2.FILLED)</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 2142829,
      "author_name": "Antti Isosalo",
      "author_url": "",
      "post_date": "2023-02-13T20:10:00.493000",
      "content": "<p>Some key piece might be missing here. 🤔 Have you predicted segmentation masks with some method (e.g., CNN)? And if so, are those masks located in <code>\"/kaggle/working/output/train/{label}/{im_name}\"</code>? The RSNA competition data does not include pixel-wise segmentation masks of the lesions, but you indeed could estimate those from the data with suitable technology.</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2142751": "Hey Denis, the code isn't showing up properly, but I don't see how that could work. I saw @paulbacher processing notebook. This notebook is excellent btw, and thank you Paul for posting it. it had a masking technique. He used it to help with cropping but I think it works really well.\n\nhttps://www.kaggle.com/code/paulbacher/custom-preprocessor-rsna-breast-cancer\n\nEssentially this would be the code where img is your numpy of image shape (h,w)\n\nthreshold = 0\nbin_img = (img > threshold).astype(np.uint8)\ncontours, _ = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\ncontour = max(contours, key=cv2.contourArea)\nmask = np.zeros(img.shape, np.uint8)\ncv2.drawContours(mask, [contour], -1, 255, cv2.FILLED)\n\n",
    "2142829": "Some key piece might be missing here. 🤔 Have you predicted segmentation masks with some method (e.g., CNN)? And if so, are those masks located in `\"/kaggle/working/output/train/{label}/{im_name}\"`? The RSNA competition data does not include pixel-wise segmentation masks of the lesions, but you indeed could estimate those from the data with suitable technology.",
    "2142367": "Hello guys, I want to perform segmentation on my images so that I will train them using an Unet model ....so I want to create a function to mask theme images...i have already converted my images to png ...so I want the function when I call the function should mask the image and visualize the original and the masked image...example of usage of the function.\n`mask = create_mask(train_df,sample_img)`\n\nThis is example of the function I used to mask the images but didn't work pretty well.\n`def create_mask(df: pd.DataFrame, image_name: str='img.png', shape: tuple=(350, 525)):\n\n    masks = np.zeros((shape[0], shape[1], 1), dtype=np.float32)\n\n    df = df[df[\"img_path\"] == image_name]\n\n    for idx, im_name in enumerate(df[\"img_path\"].values):\n\n        label = df.loc[df[\"img_path\"] == im_name, \"cancer\"].values[0]\n\n        mask = cv2.imread(f\"/kaggle/working/output/train/{label}/{im_name}\")\n\n        if mask is None:\n\n            continue\n\n        masks[:, :, 0] = mask[:, :, 0] / 255\n\n    return masks\n            `\n\nthe masks output was dark...I am trying to use the techniques which was used here [Image Segmentation](https://github.com/sneddy/pneumothorax-segmentation/blob/master/unet_pipeline/utils/prepare_png.py)"
  }
}