title: Select part of a cv::Mat based on non-zero pixels of another Mat?
body: <p>I am trying to update part of a Mat based on another Mat. For example, I want to select a part of <code>img</code> that is not zero in <code>mask</code> and add a constant value to it. When I try this:</p>
  
  <pre class="lang-c prettyprint-override"><code>Mat mask = imread("some grayscale image with a white area in a black background", IMREAD_GRAYSCALE);
  Mat img = Mat::zeros(mask.rows, mask.cols, CV_8UC1);
  Mat bnry, locations;
  threshold(mask, bnry, 100, 255, THRESH_BINARY);
  findNonZero(bnry, locations);
  img(locations) += 5;
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>Error: Assertion failed ((int)ranges.size() == d)</p>
  </blockquote>
  
  <p><code>img</code> and <code>mask</code> have the same size.</p>
  
  <p>How can I select an area of an image based on another image (mask)?</p>