仅模糊图像的子区域

仅模糊图像的子区域

本文介绍了使用 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:很重要的一点是,在对子区域的边界进行模糊处理时,应尽可能使用现有的图像内容;只有当卷积超出原图的边界时,才可以使用外推法或其他人工边界条件.

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 进行高斯模糊:仅模糊图像的子区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:19