使用OpenCV进行高斯模糊处理:仅对图像的子区域进行模糊处理? [英] Gaussian blurring with OpenCV: only blurring a subregion of an image?

查看:1096
本文介绍了使用OpenCV进行高斯模糊处理:仅对图像的子区域进行模糊处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用OpenCV对图像的某个子区域而不是整个图像进行模糊处理,以节省一些计算成本?

Is it possible to only blur a subregion of an image, instead of the whole image with OpenCV, to save some computational cost?

编辑:重要的一点是,在模糊子区域的边界时,应尽可能使用现有的图像内容.仅当卷积超过原始图像的边界时,才可以使用外推法或其他人工边界条件.

EDIT: One important point is that when blurring the boundary of the subregion, one should use the existing image content as much as possible; only when the convolution exceeds the boundary of the original image, an extrapolation or other artificial border conditions can be used.

推荐答案

要模糊整个图像,假设您想覆盖原始图像(cv::GaussianBlur ),您将看到类似的内容

To blur the whole image, assuming you want to overwrite the original (In-place filtering is supported by cv::GaussianBlur), you will have something like

 cv::GaussianBlur(image, image, Size(0, 0), 4);

要仅模糊区域,请使用 Mat :: operator()(const Rect& roi)提取区域:

To blur just a region use Mat::operator()(const Rect& roi) to extract the region:

 cv::Rect region(x, y, w, h);
 cv::GaussianBlur(image(region), image(region), Size(0, 0), 4);

或者如果要在单独的图像中显示模糊的输出,则:

Or if you want the blurred output in a separate image:

 cv::Rect region(x, y, w, h);
 cv::Mat blurred_region;
 cv::GaussianBlur(image(region), blurred_region, Size(0, 0), 4);

上面使用默认的BORDER_CONSTANT选项,当进行模糊处理时,它仅假定图像之外的所有内容均为0. 我不确定区域边缘的像素会做什么.您可以强制其忽略区域之外的像素(BORDER_CONSTANT | BORDER_ISOLATE).因此,它认为它可能确实使用了该区域之外的像素.您需要将上面的结果与以下内容进行比较:

The above uses the default BORDER_CONSTANT option that just assumes everything outside the image is 0 when doing the blurring. I am not sure what it does with pixels at the edge of a region. You can force it to ignore pixels outside the region (BORDER_CONSTANT|BORDER_ISOLATE). SO it think it probably does use the pixels outside the region. You need to compare the results from above with:

 const int bsize = 10;
 cv::Rect region(x, y, w, h);
 cv::Rect padded_region(x - bsize, y - bsize, w + 2 * bsize, h + 2 * bsize)
 cv::Mat blurred_padded_region;
 cv::GaussianBlur(image(padded_region), blurred_padded_region, Size(0, 0), 4);

 cv::Mat blurred_region = blurred_padded_region(cv::Rect(bsize, bsize, w, h));
 // and you can then copy that back into the original image if you want: 
 blurred_region.copyTo(image(region));

这篇关于使用OpenCV进行高斯模糊处理:仅对图像的子区域进行模糊处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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