title: How to blur a rectagle with OpenCv
body: <p>Is there any way to blur a Mat object in OpenCv (for Android) so that the blur is contained within a Rect object? I am doing a face blurring application and have tried this:</p>
  
  <pre><code>Mat mat = ...; // is initialized properly to 480*640
  
  ...
  
  Mat blurred = new Mat();
  for (Rect rect : faceDetections.toArray()) {
    int xStart = Math.max(0, rect.x);
    int yStart = Math.max(0, rect.y);
  
    Imgproc.blur(mat, blurred, new Size(rect.width/2, rect.height/2), new Point(xStart, yStart));
  
    Core.rectangle(blurred, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
    new Scalar(0, 255, 0));
  }
  </code></pre>
  
  <p>If I comment out the <code>Imgproc.blur</code> part then it works correctly by drawing a rectagle around the face. However, when I run it with this line I get the following in the logs:</p>
  
  <pre><code>11-07 17:27:54.100: E/AndroidRuntime(25665): Caused by: CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/filter.cpp:182: error: (-215) 0 &lt;= anchor.x &amp;&amp; anchor.x &lt; ksize.width &amp;&amp; 0 &lt;= anchor.y &amp;&amp; anchor.y &lt; ksize.height in function void cv::FilterEngine::init(const cv::Ptr&lt;cv::BaseFilter&gt;&amp;, const cv::Ptr&lt;cv::BaseRowFilter&gt;&amp;, const cv::Ptr&lt;cv::BaseColumnFilter&gt;&amp;, int, int, int, int, int, const Scalar&amp;)
  </code></pre>
  
  <p>This means that the anchor point is out of bounds, but I have looked up that <a href="http://answers.opencv.org/question/35111/origin-pixel-in-the-image-coordinate-system-in/" rel="nofollow">the (0,0) point for open CV is the upper left point</a> so I don't think it should be going out of bounds.</p>
  
  <p>Also ideally I would like to do a gaussian blur (instead of just blur) in the region, but I can't figure out how to bound that in the rectangle either: it always blurs the whole image.</p>
  
  <p><a href="http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html" rel="nofollow">Link to ImgProc docs</a>. Any help is greatly appreciated!</p>