{
  "id": 122942,
  "title": "Mask cars in a better way",
  "url": "/competitions/pku-autonomous-driving/discussion/122942",
  "author_name": "",
  "post_date": "2019-12-23T19:49:34.002761600Z",
  "votes": 5,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I found some of the discussions to have a masking method by introducing an alpha over the mask however the car was quite visible even after introducing the alpha and I thought that it would be better to have a complete mask. In other words, the white color is completely used as a mask without any transparency introduced in it.\nBelow is the code of my implementation.\nPlease correct me if I am wrong or if you can provide a more efficient (vectorized) implementation. </p>\n\n<p>```python</p>\n\n<h1>Imported Libraries</h1>\n\n<p>import numpy as np\nimport matplotlib.pyplot as plt\nimport copy\n<code>\n</code>python</p>\n\n<h1>Processing</h1>\n\n<p>img1=plt.imread('../input/pku-autonomous-driving/train_masks/'+train.iloc[0].ImageId+'.jpg')</p>\n\n<p>img=copy.deepcopy(img1)</p>\n\n<p>img=np.dstack((img, np.zeros((len(img), len(img[0])))))\nfor i in range(len(img1)):\n    for j in range(len(img1[i])):\n        if img1[i][j][0]&gt;200:\n            img[i][j]=np.append(img1[i][j],1)\n<code>\n</code></p>\n\n<h1>Code to display</h1>\n\n<p>plt.figure(figsize=(15,10))</p>\n\n<p>plt.grid(False)</p>\n\n<p>plt.imshow(plt.imread('../input/pku-autonomous-driving/train_images/'+train.iloc[0].ImageId+'.jpg'))</p>\n\n<p>plt.imshow(img)\n```\nPS: I added a fourth dimension as transparency in my img numpy array.</p>\n\n<p>Before:\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2F6ccbc720ec5902f187070cc95e391214%2Fhello1.png?generation=1577129786913842&amp;alt=media\" alt=\"\">\nAfter the processing:\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2Fb5b3b7dba339b7995097569e5c89de60%2Fhello.png?generation=1577129882575237&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": "701702",
      "postDate": "12/23/2019 19:49:34",
      "content": "<p>I found some of the discussions to have a masking method by introducing an alpha over the mask however the car was quite visible even after introducing the alpha and I thought that it would be better to have a complete mask. In other words, the white color is completely used as a mask without any transparency introduced in it.\nBelow is the code of my implementation.\nPlease correct me if I am wrong or if you can provide a more efficient (vectorized) implementation. </p>\n\n<p>```python</p>\n\n<h1>Imported Libraries</h1>\n\n<p>import numpy as np\nimport matplotlib.pyplot as plt\nimport copy\n<code>\n</code>python</p>\n\n<h1>Processing</h1>\n\n<p>img1=plt.imread('../input/pku-autonomous-driving/train_masks/'+train.iloc[0].ImageId+'.jpg')</p>\n\n<p>img=copy.deepcopy(img1)</p>\n\n<p>img=np.dstack((img, np.zeros((len(img), len(img[0])))))\nfor i in range(len(img1)):\n    for j in range(len(img1[i])):\n        if img1[i][j][0]&gt;200:\n            img[i][j]=np.append(img1[i][j],1)\n<code>\n</code></p>\n\n<h1>Code to display</h1>\n\n<p>plt.figure(figsize=(15,10))</p>\n\n<p>plt.grid(False)</p>\n\n<p>plt.imshow(plt.imread('../input/pku-autonomous-driving/train_images/'+train.iloc[0].ImageId+'.jpg'))</p>\n\n<p>plt.imshow(img)\n```\nPS: I added a fourth dimension as transparency in my img numpy array.</p>\n\n<p>Before:\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2F6ccbc720ec5902f187070cc95e391214%2Fhello1.png?generation=1577129786913842&amp;alt=media\" alt=\"\">\nAfter the processing:\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2Fb5b3b7dba339b7995097569e5c89de60%2Fhello.png?generation=1577129882575237&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "I found some of the discussions to have a masking method by introducing an alpha over the mask however the car was quite visible even after introducing the alpha and I thought that it would be better to have a complete mask. In other words, the white color is completely used as a mask without any transparency introduced in it.\nBelow is the code of my implementation.\nPlease correct me if I am wrong or if you can provide a more efficient (vectorized) implementation. \n\n```python\n#Imported Libraries\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport copy\n```\n```python\n# Processing\n\nimg1=plt.imread('../input/pku-autonomous-driving/train_masks/'+train.iloc[0].ImageId+'.jpg')\n\nimg=copy.deepcopy(img1)\n\nimg=np.dstack((img, np.zeros((len(img), len(img[0])))))\nfor i in range(len(img1)):\n    for j in range(len(img1[i])):\n        if img1[i][j][0]&gt;200:\n            img[i][j]=np.append(img1[i][j],1)\n```\n```\n# Code to display\n\nplt.figure(figsize=(15,10))\n\nplt.grid(False)\n\nplt.imshow(plt.imread('../input/pku-autonomous-driving/train_images/'+train.iloc[0].ImageId+'.jpg'))\n\nplt.imshow(img)\n```\nPS: I added a fourth dimension as transparency in my img numpy array.\n\nBefore:\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2F6ccbc720ec5902f187070cc95e391214%2Fhello1.png?generation=1577129786913842&amp;alt=media)\nAfter the processing:\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2Fb5b3b7dba339b7995097569e5c89de60%2Fhello.png?generation=1577129882575237&amp;alt=media)",
      "votes": null
    },
    {
      "id": "702829",
      "postDate": "12/25/2019 08:21:58",
      "content": "<p>You also can try this way.  <a href=\"/ashishohri\">@ashishohri</a> \nFrom : <a href=\"https://www.kaggle.com/amit9484/save-images-with-mask\">https://www.kaggle.com/amit9484/save-images-with-mask</a></p>\n\n<p>```\ndef CreateMaskImages(imageName):\n    trainimage = cv2.imread(PATH  + \"/train_images/\" + imageName + '.jpg')\n    imagemask = cv2.imread(PATH + \"/train_masks/\" + imageName + \".jpg\",0)\n    try:\n        imagemaskinv = cv2.bitwise_not(imagemask)\n        res = cv2.bitwise_and(trainimage,trainimage,mask = imagemaskinv)</p>\n\n<pre><code>    # cut upper half,because it doesn't contain cars.\n    res = res[res.shape[0] // 2:]\n    return res\nexcept:\n    trainimage = trainimage[trainimage.shape[0] // 2:]\n    return trainimage\n</code></pre>\n\n<p>```</p>",
      "rawMarkdown": "You also can try this way.  @ashishohri \nFrom : https://www.kaggle.com/amit9484/save-images-with-mask\n\n```\ndef CreateMaskImages(imageName):\n    trainimage = cv2.imread(PATH  + \"/train_images/\" + imageName + '.jpg')\n    imagemask = cv2.imread(PATH + \"/train_masks/\" + imageName + \".jpg\",0)\n    try:\n        imagemaskinv = cv2.bitwise_not(imagemask)\n        res = cv2.bitwise_and(trainimage,trainimage,mask = imagemaskinv)\n        \n        # cut upper half,because it doesn't contain cars.\n        res = res[res.shape[0] // 2:]\n        return res\n    except:\n        trainimage = trainimage[trainimage.shape[0] // 2:]\n        return trainimage\n```",
      "votes": null
    },
    {
      "id": "705169",
      "postDate": "12/28/2019 15:22:55",
      "content": "<p>Thanks! Surely will check it out 👍 </p>",
      "rawMarkdown": "Thanks! Surely will check it out 👍",
      "votes": null
    },
    {
      "id": "711744",
      "postDate": "01/06/2020 14:10:38",
      "content": "<p>This might be a little bit easiler</p>\n\n<p><code>image = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))</code>\n<code>mask = np.array(lplt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))</code>\n<code>masks = np.array([mask,mask,mask]).transpose(1,2,0)</code>\n<code>masked_image = np.where(masks&gt;image, masks, image)</code></p>",
      "rawMarkdown": "This might be a little bit easiler\n\n`image = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))`\n`mask = np.array(lplt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))`\n`masks = np.array([mask,mask,mask]).transpose(1,2,0)`\n`masked_image = np.where(masks&gt;image, masks, image)`",
      "votes": null
    },
    {
      "id": "716464",
      "postDate": "01/11/2020 18:21:02",
      "content": "<p>Thanks but I think the third line needs to be removed as displayed below:\n<code>\nimage = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))\nmask = np.array(plt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))\nmasked_image = np.where(mask&gt;image, mask, image)\n</code></p>",
      "rawMarkdown": "Thanks but I think the third line needs to be removed as displayed below:\n```\nimage = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))\nmask = np.array(plt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))\nmasked_image = np.where(mask&gt;image, mask, image)\n```",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 702829,
      "author_name": "diegojohnson",
      "author_url": "",
      "post_date": "12/25/2019 08:21:58",
      "content": "<p>You also can try this way.  <a href=\"/ashishohri\">@ashishohri</a> \nFrom : <a href=\"https://www.kaggle.com/amit9484/save-images-with-mask\">https://www.kaggle.com/amit9484/save-images-with-mask</a></p>\n\n<p>```\ndef CreateMaskImages(imageName):\n    trainimage = cv2.imread(PATH  + \"/train_images/\" + imageName + '.jpg')\n    imagemask = cv2.imread(PATH + \"/train_masks/\" + imageName + \".jpg\",0)\n    try:\n        imagemaskinv = cv2.bitwise_not(imagemask)\n        res = cv2.bitwise_and(trainimage,trainimage,mask = imagemaskinv)</p>\n\n<pre><code>    # cut upper half,because it doesn't contain cars.\n    res = res[res.shape[0] // 2:]\n    return res\nexcept:\n    trainimage = trainimage[trainimage.shape[0] // 2:]\n    return trainimage\n</code></pre>\n\n<p>```</p>",
      "votes": null,
      "replies": [
        {
          "id": 705169,
          "author_name": "ashishohri",
          "author_url": "",
          "post_date": "12/28/2019 15:22:55",
          "content": "<p>Thanks! Surely will check it out 👍 </p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 711744,
      "author_name": "xiejialun",
      "author_url": "",
      "post_date": "01/06/2020 14:10:38",
      "content": "<p>This might be a little bit easiler</p>\n\n<p><code>image = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))</code>\n<code>mask = np.array(lplt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))</code>\n<code>masks = np.array([mask,mask,mask]).transpose(1,2,0)</code>\n<code>masked_image = np.where(masks&gt;image, masks, image)</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 716464,
          "author_name": "ashishohri",
          "author_url": "",
          "post_date": "01/11/2020 18:21:02",
          "content": "<p>Thanks but I think the third line needs to be removed as displayed below:\n<code>\nimage = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))\nmask = np.array(plt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))\nmasked_image = np.where(mask&gt;image, mask, image)\n</code></p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "701702": "I found some of the discussions to have a masking method by introducing an alpha over the mask however the car was quite visible even after introducing the alpha and I thought that it would be better to have a complete mask. In other words, the white color is completely used as a mask without any transparency introduced in it.\nBelow is the code of my implementation.\nPlease correct me if I am wrong or if you can provide a more efficient (vectorized) implementation. \n\n```python\n#Imported Libraries\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport copy\n```\n```python\n# Processing\n\nimg1=plt.imread('../input/pku-autonomous-driving/train_masks/'+train.iloc[0].ImageId+'.jpg')\n\nimg=copy.deepcopy(img1)\n\nimg=np.dstack((img, np.zeros((len(img), len(img[0])))))\nfor i in range(len(img1)):\n    for j in range(len(img1[i])):\n        if img1[i][j][0]&gt;200:\n            img[i][j]=np.append(img1[i][j],1)\n```\n```\n# Code to display\n\nplt.figure(figsize=(15,10))\n\nplt.grid(False)\n\nplt.imshow(plt.imread('../input/pku-autonomous-driving/train_images/'+train.iloc[0].ImageId+'.jpg'))\n\nplt.imshow(img)\n```\nPS: I added a fourth dimension as transparency in my img numpy array.\n\nBefore:\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2F6ccbc720ec5902f187070cc95e391214%2Fhello1.png?generation=1577129786913842&amp;alt=media)\nAfter the processing:\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2048090%2Fb5b3b7dba339b7995097569e5c89de60%2Fhello.png?generation=1577129882575237&amp;alt=media)",
    "702829": "You also can try this way.  @ashishohri \nFrom : https://www.kaggle.com/amit9484/save-images-with-mask\n\n```\ndef CreateMaskImages(imageName):\n    trainimage = cv2.imread(PATH  + \"/train_images/\" + imageName + '.jpg')\n    imagemask = cv2.imread(PATH + \"/train_masks/\" + imageName + \".jpg\",0)\n    try:\n        imagemaskinv = cv2.bitwise_not(imagemask)\n        res = cv2.bitwise_and(trainimage,trainimage,mask = imagemaskinv)\n        \n        # cut upper half,because it doesn't contain cars.\n        res = res[res.shape[0] // 2:]\n        return res\n    except:\n        trainimage = trainimage[trainimage.shape[0] // 2:]\n        return trainimage\n```",
    "705169": "Thanks! Surely will check it out 👍",
    "711744": "This might be a little bit easiler\n\n`image = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))`\n`mask = np.array(lplt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))`\n`masks = np.array([mask,mask,mask]).transpose(1,2,0)`\n`masked_image = np.where(masks&gt;image, masks, image)`",
    "716464": "Thanks but I think the third line needs to be removed as displayed below:\n```\nimage = np.array(plt.imread(os.path.join(image_load_dir, imageid+'.jpg')))\nmask = np.array(plt.imread(os.path.join(mask_load_dir, imageid+'.jpg')))\nmasked_image = np.where(mask&gt;image, mask, image)\n```"
  },
  "source": "meta"
}