如何加快OpenCV融合?
我正在处理一个问题,该问题包括两张图像的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论