title: How can I mask a polygon in an image in MATLAB?
body: <p>I have drawn a polygon on an image and now want to mask it. I just want to see the region inside the polygon and have everything outside be black.</p>
  
  <p>Here is my code for drawing the polygon on the image:</p>
  
  <pre><code>i = imread('Vlc1.1.png');
  pos = [170 350 290 230 430 230 600 350 170 350];
  S = insertShape(i,'Polygon',pos);
  imshow(S);
  </code></pre>
  
  <p>And here's the resulting image:</p>
  
  <p><a href="https://i.stack.imgur.com/AZdb3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AZdb3.jpg" alt="enter image description here"></a></p>
  
  <p>How can I set everything outside the polygon to black?</p>