title: Otsu thresholding inside mask
body: <p>I'm working with Python and trying to do Otsu thresholding on an image but only inside the mask (yes, I have an image and a mask image). It means less pixel on the image will be included in the histogram for calculating the Otsu threshold.</p>
  
  <p>I'm currently using the <code>cv2.threshold</code> function without the mask image and have no idea how to do this kind of job. </p>
  
  <pre class="lang-py prettyprint-override"><code>ret, OtsuMat = cv2.threshold(GaborMat, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  </code></pre>
  
  <p>Since this function also incorporates the pixels outside the mask, I think it will give a less accurate threshold.</p>
  
  <p>This is the example of the image and its mask:</p>
  
  <p><a href="https://drive.google.com/drive/folders/1p8JMhncJs19oOWO9RdkWuEADVGqE-gzQ?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/drive/folders/1p8JMhncJs19oOWO9RdkWuEADVGqE-gzQ?usp=sharing</a></p>
  
  <p>Hope there is a OpenCV or other lib function to do it easily (and also with fast computing), but any kind of help will be appreciated.</p>