{
  "id": 430749,
  "title": "How misalignment of label occures?",
  "url": "/competitions/google-research-identify-contrails-reduce-global-warming/discussion/430749",
  "author_name": "Bilzard",
  "post_date": "2023-08-11T01:14:42.854000",
  "votes": 31,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Some participants already reports, this competition's labels are misaligned.<br>\nIn this discussion, I tried to explain possible cause of misalignment.</p>\n<p>In short, the misalignment was caused by inconsistent definition of label's coordinates.<br>\nIn OpenCV's <code>fillPoly()</code> function, polygon's coordinates are assumed to be aligned to pixel's <strong>center position</strong>. On the other hand, this competition's labels were likely aligned to <strong>the upper-left corner</strong>, considering the situation (fig. 1).</p>\n<p>If bit mask conversion was applied to contrail labels which is aligned to upper-left corner, the result bit mask should be shifted (0.5, 0.5) pixels to the lower right <strong>fig. 2</strong>.</p>\n<p>Thus, if we make label from polygon to bit masks, <strong>we should care the alignment of coordinates in pixels</strong>.</p>\n<p><strong>figure 1</strong><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fa6d4a37d070d2841bc0a35d62d13c0b2%2Fgrid.png?generation=1691715978129777&amp;alt=media\" alt=\"\"></p>\n<p><strong>figure 2</strong><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F70a500946bbea42fd2fc8d6d0669d754%2Ffillpoly.png?generation=1691716324268602&amp;alt=media\" alt=\"\"></p>\n<hr>\n<p>below is code to reproduce this result:</p>\n<pre><code> numpy  np\n cv2\n matplotlib.pyplot  plt\n matplotlib.patches  patches\n\nheight, width = , \nmask = np.zeros((height, width), dtype=np.uint8)\npts = np.array([[, ], [, ], [, ], [, ]], np.float32)\npts_corrected = pts + \n\npts_reshaped = pts.reshape((-, , ))\npts_reshaped = pts_reshaped.astype(np.int32)\n\ncv2.fillPoly(mask, [pts_reshaped], color=(,))\n\nfig, ax = plt.subplots()\nax.imshow(mask, cmap=, extent=[, width, height, ])\npolygon = patches.Polygon(pts, edgecolor=, facecolor=, label=)\npolygon_corrected = patches.Polygon(\n    pts_corrected, edgecolor=, facecolor=, label=\n)\nax.add_patch(polygon)\nax.add_patch(polygon_corrected)\nax.legend()\nax.(title=)\nfig.savefig(, bbox_inches=)\n\nplt.show()\n</code></pre>",
  "messages": [
    {
      "id": 2384423,
      "postDate": "2023-08-11T01:14:42.853Z",
      "content": "<p>Some participants already reports, this competition's labels are misaligned.<br>\nIn this discussion, I tried to explain possible cause of misalignment.</p>\n<p>In short, the misalignment was caused by inconsistent definition of label's coordinates.<br>\nIn OpenCV's <code>fillPoly()</code> function, polygon's coordinates are assumed to be aligned to pixel's <strong>center position</strong>. On the other hand, this competition's labels were likely aligned to <strong>the upper-left corner</strong>, considering the situation (fig. 1).</p>\n<p>If bit mask conversion was applied to contrail labels which is aligned to upper-left corner, the result bit mask should be shifted (0.5, 0.5) pixels to the lower right <strong>fig. 2</strong>.</p>\n<p>Thus, if we make label from polygon to bit masks, <strong>we should care the alignment of coordinates in pixels</strong>.</p>\n<p><strong>figure 1</strong><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fa6d4a37d070d2841bc0a35d62d13c0b2%2Fgrid.png?generation=1691715978129777&amp;alt=media\" alt=\"\"></p>\n<p><strong>figure 2</strong><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F70a500946bbea42fd2fc8d6d0669d754%2Ffillpoly.png?generation=1691716324268602&amp;alt=media\" alt=\"\"></p>\n<hr>\n<p>below is code to reproduce this result:</p>\n<pre><code> numpy  np\n cv2\n matplotlib.pyplot  plt\n matplotlib.patches  patches\n\nheight, width = , \nmask = np.zeros((height, width), dtype=np.uint8)\npts = np.array([[, ], [, ], [, ], [, ]], np.float32)\npts_corrected = pts + \n\npts_reshaped = pts.reshape((-, , ))\npts_reshaped = pts_reshaped.astype(np.int32)\n\ncv2.fillPoly(mask, [pts_reshaped], color=(,))\n\nfig, ax = plt.subplots()\nax.imshow(mask, cmap=, extent=[, width, height, ])\npolygon = patches.Polygon(pts, edgecolor=, facecolor=, label=)\npolygon_corrected = patches.Polygon(\n    pts_corrected, edgecolor=, facecolor=, label=\n)\nax.add_patch(polygon)\nax.add_patch(polygon_corrected)\nax.legend()\nax.(title=)\nfig.savefig(, bbox_inches=)\n\nplt.show()\n</code></pre>",
      "rawMarkdown": "Some participants already reports, this competition's labels are misaligned.\nIn this discussion, I tried to explain possible cause of misalignment.\n\nIn short, the misalignment was caused by inconsistent definition of label's coordinates.\nIn OpenCV's `fillPoly()` function, polygon's coordinates are assumed to be aligned to pixel's **center position**. On the other hand, this competition's labels were likely aligned to **the upper-left corner**, considering the situation (fig. 1).\n\nIf bit mask conversion was applied to contrail labels which is aligned to upper-left corner, the result bit mask should be shifted (0.5, 0.5) pixels to the lower right **fig. 2**.\n\nThus, if we make label from polygon to bit masks, **we should care the alignment of coordinates in pixels**.\n\n\n**figure 1**\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fa6d4a37d070d2841bc0a35d62d13c0b2%2Fgrid.png?generation=1691715978129777&alt=media)\n\n**figure 2**\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F70a500946bbea42fd2fc8d6d0669d754%2Ffillpoly.png?generation=1691716324268602&alt=media)\n\n---\nbelow is code to reproduce this result:\n\n```python\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nheight, width = 8, 8\nmask = np.zeros((height, width), dtype=np.uint8)\npts = np.array([[1, 5], [2, 3], [6, 4], [3, 1]], np.float32)\npts_corrected = pts + 0.5\n\npts_reshaped = pts.reshape((-1, 1, 2))\npts_reshaped = pts_reshaped.astype(np.int32)\n\ncv2.fillPoly(mask, [pts_reshaped], color=(255,))\n\nfig, ax = plt.subplots()\nax.imshow(mask, cmap=\"gray\", extent=[0, width, height, 0])\npolygon = patches.Polygon(pts, edgecolor=\"red\", facecolor=\"none\", label=\"poligon\")\npolygon_corrected = patches.Polygon(\n    pts_corrected, edgecolor=\"blue\", facecolor=\"none\", label=\"poligon + (0.5, 0.5)\"\n)\nax.add_patch(polygon)\nax.add_patch(polygon_corrected)\nax.legend()\nax.set(title=\"Result of cv2.fillPoly()\")\nfig.savefig(\"fillpoly.png\", bbox_inches=\"tight\")\n\nplt.show()\n```",
      "votes": 31
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2384423": "Some participants already reports, this competition's labels are misaligned.\nIn this discussion, I tried to explain possible cause of misalignment.\n\nIn short, the misalignment was caused by inconsistent definition of label's coordinates.\nIn OpenCV's `fillPoly()` function, polygon's coordinates are assumed to be aligned to pixel's **center position**. On the other hand, this competition's labels were likely aligned to **the upper-left corner**, considering the situation (fig. 1).\n\nIf bit mask conversion was applied to contrail labels which is aligned to upper-left corner, the result bit mask should be shifted (0.5, 0.5) pixels to the lower right **fig. 2**.\n\nThus, if we make label from polygon to bit masks, **we should care the alignment of coordinates in pixels**.\n\n\n**figure 1**\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fa6d4a37d070d2841bc0a35d62d13c0b2%2Fgrid.png?generation=1691715978129777&alt=media)\n\n**figure 2**\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F70a500946bbea42fd2fc8d6d0669d754%2Ffillpoly.png?generation=1691716324268602&alt=media)\n\n---\nbelow is code to reproduce this result:\n\n```python\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nheight, width = 8, 8\nmask = np.zeros((height, width), dtype=np.uint8)\npts = np.array([[1, 5], [2, 3], [6, 4], [3, 1]], np.float32)\npts_corrected = pts + 0.5\n\npts_reshaped = pts.reshape((-1, 1, 2))\npts_reshaped = pts_reshaped.astype(np.int32)\n\ncv2.fillPoly(mask, [pts_reshaped], color=(255,))\n\nfig, ax = plt.subplots()\nax.imshow(mask, cmap=\"gray\", extent=[0, width, height, 0])\npolygon = patches.Polygon(pts, edgecolor=\"red\", facecolor=\"none\", label=\"poligon\")\npolygon_corrected = patches.Polygon(\n    pts_corrected, edgecolor=\"blue\", facecolor=\"none\", label=\"poligon + (0.5, 0.5)\"\n)\nax.add_patch(polygon)\nax.add_patch(polygon_corrected)\nax.legend()\nax.set(title=\"Result of cv2.fillPoly()\")\nfig.savefig(\"fillpoly.png\", bbox_inches=\"tight\")\n\nplt.show()\n```"
  }
}