是否可以仅使用OpenCV模糊图像的子区域而不是整个图像,以节省一些计算成本?
谢谢!
编辑:重要的一点是,当模糊子区域的边界时,应尽可能多地使用现有的图像内容; 只有当卷积超过原始图像的边界时,才能使用外推法或其他人工边界条件.
Bul*_*ull 15
模糊的整体形象,假设你要覆盖原(就地过滤支持由CV ::高斯模糊),你会碰到这样的
cv::GaussianBlur(image, image, Size(0, 0), 4);
Run Code Online (Sandbox Code Playgroud)
要仅模糊区域,请使用Mat :: operator()(const Rect&roi)来提取区域:
cv::Rect region(x, y, w, h);
cv::GaussianBlur(image(region), image(region), Size(0, 0), 4);
Run Code Online (Sandbox Code Playgroud)
或者,如果您想在单独的图像中输出模糊的输出:
cv::Rect region(x, y, w, h);
cv::Mat blurred_region;
cv::GaussianBlur(image(region), blurred_region, Size(0, 0), 4);
Run Code Online (Sandbox Code Playgroud)
以上使用默认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));
Run Code Online (Sandbox Code Playgroud)
以下是如何在 Python 中执行此操作。这个想法是选择一个 ROI,对其进行模糊处理,然后将其插入回图像中
import cv2
# Read in image
image = cv2.imread('1.png')
# Create ROI coordinates
topLeft = (60, 140)
bottomRight = (340, 250)
x, y = topLeft[0], topLeft[1]
w, h = bottomRight[0] - topLeft[0], bottomRight[1] - topLeft[1]
# Grab ROI with Numpy slicing and blur
ROI = image[y:y+h, x:x+w]
blur = cv2.GaussianBlur(ROI, (51,51), 0)
# Insert ROI back into image
image[y:y+h, x:x+w] = blur
cv2.imshow('blur', blur)
cv2.imshow('image', image)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
之前->之后
