title: Convert Annotated Image to Binary Mask Image using Polygon Co-ordinate
body: <p>I am trying to Convert Annotated image to Binary mask Image using cordinates present in json file.<br />
  Image is Annotated using VGG annotation.</p>
  <p>Below are the Actual image, Json data, and the result i want.</p>
  <p><a href="https://i.stack.imgur.com/uG2F6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uG2F6.jpg" alt="Image" /></a></p>
  <p>Here are the Cordinates of the above image</p>
  <pre><code>{&quot;ILSVRC2012_test_00000181.jpg28497&quot;:{&quot;filename&quot;:&quot;ILSVRC2012_test_00000181.jpg&quot;,&quot;size&quot;:28497,&quot;regions&quot;:[{&quot;shape_attributes&quot;:{&quot;name&quot;:&quot;polygon&quot;,&quot;all_points_x&quot;:[55,63,82,103,116,137,140,153,155,160,160,169,199,211,227,236,242,250,255,265,268,278,282,290,303,315,321,329,326,332,337,336,330,324,321,317,317,319,309,285,275,264,262,236,223,207,196,190,183,176,176,190,196,187,158,145,118,94,76,83,94,111,101,87,88,105,79,55],&quot;all_points_y&quot;:[102,95,87,69,62,58,58,64,69,76,79,77,80,81,78,77,79,84,90,104,108,113,114,129,147,168,204,252,264,286,302,305,299,288,295,316,327,340,343,345,338,340,346,348,340,337,323,314,310,308,305,301,329,337,331,316,325,330,319,306,289,282,229,159,128,98,98,104]},&quot;region_attributes&quot;:{&quot;name&quot;:&quot;not_defined&quot;,&quot;type&quot;:&quot;unknown&quot;,&quot;image_quality&quot;:{&quot;good&quot;:true,&quot;frontal&quot;:true,&quot;good_illumination&quot;:true},&quot;animal&quot;:&quot;pigeon&quot;}}],&quot;file_attributes&quot;:{&quot;caption&quot;:&quot;&quot;,&quot;public_domain&quot;:&quot;no&quot;,&quot;image_url&quot;:&quot;&quot;}}}
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/pbBM3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pbBM3.png" alt="I want to Convert the above image in Binary Mask like beolw using that json Cordinate " /></a></p>
  <p>Or does any knows any tool which helps to create dataset like this.</p>
  <p>Thankyou</p>
  <p><strong>EDIT</strong></p>
  <pre><code>import cv2
  import numpy as np
  
  img = cv2.imread(&quot;input.jpg&quot;)
  
  area = np.array([
            [55, 102], [63, 95], [82, 87], [103, 69],[116,62 ], [137,58 ],[140,58 ], [153, 64],
            [155,69 ], [160, 76],[160,79 ], [169,77 ],[199, 80], [211,81 ],[227,78 ], [236, 77],
            [242,79 ], [250, 84],[255, 90], [265, 104],[268, 108], [278, 113],[282, 114], [290, 129],
            [303, 147], [315, 168],[321, 204], [329, 252],[326, 264], [332, 286],[337, 302], [336, 305],
            [330, 299], [324, 288],[321,295 ], [317, 316],[317, 327], [319,340],[309, 343], [285, 345],
            [275, 338], [264, 340],[262, 346], [236, 348],[223, 340], [207, 337],[196, 323], [190, 314],
            [183, 310], [176, 308],[176, 305], [190, 301],[196, 329], [187, 337],[158, 331], [145, 316],
            [118, 325], [94, 330],[76, 319], [83, 306],[94, 289], [111, 282],[101, 229], [87, 159],
            [88,128 ], [105, 98],[79, 98],[55, 104]
           ]  )
  
  filled = cv2.fillPoly(img, pts = [area], color =(255,255,255))
  cv2.imwrite(&quot;mask_pen.jpg&quot;,filled)
  
  </code></pre>
  <p>Also used a naive method to make evreything black except 255</p>
  <pre><code>img = cv2.imread('mask_pen.jpg')
  img[img != 255] = 0
  </code></pre>
  <p>which gives results like <a href="https://imgur.com/93mAve2" rel="nofollow noreferrer">this</a></p>