【发布时间】:2019-09-27 11:10:36
【问题描述】:
我需要模糊图像的特定区域。我得到了图像和一个蒙版,描绘了图像中需要模糊的区域。它可以工作,但比预期的要慢一些,因为我需要模糊几十张图像。
这是我使用的代码:
def soft_blur_with_mask(image: np.ndarray, mask: np.ndarray) -> np.ndarray:
assert len(mask.shape) == 2, mask.shape
# Create a blurred copy of the original image. This can take up to 1-2 seconds today, because the image is big (~5-10 Megapixels)
blurred_image = cv2.GaussianBlur(image, (221, 221), sigmaX=20, sigmaY=20)
image_height, image_width = image.shape[:2]
mask = cv2.resize(mask.astype(np.uint8), (image_width, image_height), interpolation=cv2.INTER_NEAREST)
# Blurring the mask itself to get a softer mask with no firm edges
mask = cv2.GaussianBlur(mask.astype(np.float32), (11, 11), 10, 10)[:, :, None]
mask = mask/255.0
# Take the blurred image where the mask it positive, and the original image where the image is original
return (mask * blurred_image + (1.0 - mask) * image).clip(0, 255.0).astype(np.uint8)
【问题讨论】:
-
你能给我们一些号码吗?它有多慢,你期望它有多快。由于图像的值空间最后是 uint8,因此在 uint8 类型上执行 cv2.GaussianBlur 而不是可能很慢的 float32 将是一个优势。 OpenCV 函数正在使用优化的代码(例如 SSE),它在小数据类型(uint8)上比在大数据类型上更快。检查编译 OpenCV 时是否启用了 SSE。 (我相信默认情况下应该是)
标签: python numpy opencv image-processing blur