title: How to extract region of interest with binary mask
body: <p>I have an original Chest X-Ray image (orig.jpg). I did manual segmentation with ITK-SNAP yielding this binary mask image (bmask.jpg):</p>
  
  <p><img src="https://i.stack.imgur.com/8802G.jpg" alt="enter image description here"></p>
  
  <p>To extract the lung region from background I run the following MATLAB code:</p>
  
  <pre><code>clear all;
  clc;
  IR=imread('orig.jpg');
  im=imread('bmask.jpg');
  ROI = IR;
  ROI(im == 1) = 0;
  ROI(im ~= 1) = 1;
  SEG = IR.*ROI;
  figure;
  imshow(SEG);
  imwrite(SEG,'SEG.jpg');
  </code></pre>
  
  <p>The result image:</p>
  
  <p><img src="https://i.stack.imgur.com/u98Aj.jpg" alt="enter image description here"></p>
  
  <p>I figured out since some pixels of binary mask inside the lung regions near the lung boundary has "1" value, the resulting image has some black dots near the lung boundary inside the lung regions. Also, in the resulting image, lung boundary has a zigzag pattern, not a smooth pattern as the binary mask. How can I fix these problems? Can anyone kindly help me?</p>
  
  <p>Thanks.</p>