如何使用OpenCv模糊重新定义 [英] How to blur a rectagle with OpenCv

查看:126
本文介绍了如何使用OpenCv模糊重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使OpenCv中的Mat对象模糊(适用于Android),从而使模糊包含在Rect对象中?我正在做一个人脸模糊应用程序,并且已经尝试过了:

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:

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));
}

如果我注释掉Imgproc.blur部分,则可以通过在面部周围绘制一个矩形来正常工作.但是,当我用这一行运行它时,我在日志中得到以下内容:

If I comment out the Imgproc.blur 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:

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 <= anchor.x && anchor.x < ksize.width && 0 <= anchor.y && anchor.y < ksize.height in function void cv::FilterEngine::init(const cv::Ptr<cv::BaseFilter>&, const cv::Ptr<cv::BaseRowFilter>&, const cv::Ptr<cv::BaseColumnFilter>&, int, int, int, int, int, const Scalar&)

这意味着锚点超出范围,但是我已经查看了

This means that the anchor point is out of bounds, but I have looked up that the (0,0) point for open CV is the upper left point so I don't think it should be going out of bounds.

同样理想的是,我想对该区域进行高斯模糊处理(而不只是模糊处理),但是我也不知道如何在矩形中进行绑定:它总是使整个图像模糊.

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.

链接到ImgProc文档.任何帮助,我们将不胜感激!

Link to ImgProc docs. Any help is greatly appreciated!

推荐答案

好,我想出了办法:

Mat mask = blurred.submat(rect);
Imgproc.GaussianBlur(mask, mask, new Size(55, 55), 55); // or any other processing

然后模糊将具有模糊区域.这是因为submat不会复制模糊的数据,而是引用它,因此,应用模糊处理时,它只会模糊蒙版所引用的模糊部分.

Then blurred will have the blurred region. This is because submat doesn't copy the data in blurred, but rather references it, so when the blur is applied it only blurs the parts in blurred referenced by mask.

这篇关于如何使用OpenCv模糊重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆