如何加快OpenCV融合?

发布于 2025-01-27 05:31:10 字数 1505 浏览 1 评论 0原文

我正在处理一个问题,该问题包括两张图像的alpha混合物。我正在使用基本的OpenCV函数。对于每个图像,我使用 inrange gaussianblur 创建掩码。然后,我使用该口罩将两个图像融合在一起。您可以在下面看到混合功能。

def blend(foreground, background, alpha, batch_size):
    # Convert uint8 to float
    foreground = foreground.astype(float)
    background = background.astype(float)

    # Normalize the alpha mask to keep intensity between 0 and 1
    alpha = alpha.astype(float)/255

    # Multiply the foreground with the alpha matte
    foreground = cv2.multiply(alpha, foreground)

    # Multiply the background with ( 1 - alpha )
    background = cv2.multiply(1.0 - alpha, background)

    # Add the masked foreground and background.
    outImage = cv2.add(foreground, background)

    return outImage

掩码创建功能是:

def create_mask(image):
    # Convert image to int version and generate one channelled mask
    hsv_range_min = (0, 0, 0)
    hsv_range_max = (4, 4, 4)

    mask = cv2.inRange(image, hsv_range_min, hsv_range_max)
    mask = cv2.bitwise_not(mask)
    mask = cv2.GaussianBlur(mask, (0, 0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)
    mask = skimage.exposure.rescale_intensity(mask, in_range=(127.5,255), out_range=(0, 255))

    # Convert mask to its three channelled equivalent
    new_mask = np.zeros_like(image)
    new_mask[:,:,0] = mask
    new_mask[:,:,1] = mask
    new_mask[:,:,2] = mask

    return new_mask

我的问题是如何加快所有这些过程。我认为也许我可以使用批处理处理,但它无法正常工作。

谢谢你的宝贵时间。

I am dealing with a problem that consists alpha blending of two images. I am using basic OpenCV functions. For every image, I create a mask using inRange and GaussianBlur. Then, I use that mask to blend two images. You can see the blend function below.

def blend(foreground, background, alpha, batch_size):
    # Convert uint8 to float
    foreground = foreground.astype(float)
    background = background.astype(float)

    # Normalize the alpha mask to keep intensity between 0 and 1
    alpha = alpha.astype(float)/255

    # Multiply the foreground with the alpha matte
    foreground = cv2.multiply(alpha, foreground)

    # Multiply the background with ( 1 - alpha )
    background = cv2.multiply(1.0 - alpha, background)

    # Add the masked foreground and background.
    outImage = cv2.add(foreground, background)

    return outImage

The mask creation function is:

def create_mask(image):
    # Convert image to int version and generate one channelled mask
    hsv_range_min = (0, 0, 0)
    hsv_range_max = (4, 4, 4)

    mask = cv2.inRange(image, hsv_range_min, hsv_range_max)
    mask = cv2.bitwise_not(mask)
    mask = cv2.GaussianBlur(mask, (0, 0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)
    mask = skimage.exposure.rescale_intensity(mask, in_range=(127.5,255), out_range=(0, 255))

    # Convert mask to its three channelled equivalent
    new_mask = np.zeros_like(image)
    new_mask[:,:,0] = mask
    new_mask[:,:,1] = mask
    new_mask[:,:,2] = mask

    return new_mask

My question is how to speed up all these processes. I thought maybe I can use batch processing but it is not working as I assumed.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文