是否可以使用 OpenCV 仅模糊图像的一个子区域,而不是整个图像,以节省一些计算成本?
EDIT:重要的一点是,在模糊分区的边界时,尽量使用已有的图片内容;只有当卷积超出原始图像的边界时,才可以使用外推法或其他人工边界条件。
最佳答案
要模糊整个图像,假设您想覆盖原始图像(cv::GaussianBlur 支持就地过滤),您将得到类似
cv::GaussianBlur(image, image, Size(0, 0), 4);
要模糊一个区域使用 Mat::operator()(const Rect& roi)提取区域:
cv::Rect region(x, y, w, h);
cv::GaussianBlur(image(region), image(region), Size(0, 0), 4);
或者如果你想在单独的图像中输出模糊:
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)。所以它认为它可能确实使用了区域外的像素。您需要将上面的结果与以下内容进行比较:
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));
关于image - 使用 OpenCV 进行高斯模糊 : only blurring a subregion of an image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24195138/