在opencv python中混合的渐变蒙版 [英] Gradient mask blending in opencv python

查看:1663
本文介绍了在opencv python中混合的渐变蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像和圆圈区域。我需要模糊所有,除了圆区。此外,我需要使圆的边框平滑。

输入:



输出(使用掩码在图像redactor中制作,但我认为opencv仅使用位图掩码):



现在我在python中有代码,它不会模糊圆的边界。

  def blur_image(cv_image,radius,center,gaussian_core,sigma_x):
blurred = cv.GaussianBlur(cv_image, gaussian_core,sigma_x)
h,w,d = cv_image.shape
#掩码
circle_mask = np.ones((h,w),cv_image.dtype)
cv.circle( circle_mask,center,radius,(0,0,0), - 1)
circle_not_mask = np.zeros((h,w),cv_image.dtype)
cv.circle(circle_not_mask,center,radius ,(2,2,2), - 1)
#计算
blur_around = cv.bitwise_and(模糊,模糊,掩码= circle_mask)
image_in_circle = cv.bitwise_and(cv_image,cv_image, mask = circle_not_mask)
res = cv.bitwise_or(blur_around,image_in_circle)
返回res

当前版本:


如何模糊圆圈的边框?在输出的例子中,我在程序中使用了渐变掩码。 opencv中有类似的东西吗?

UPDATE 04.03

所以,我试过
它的工作速度比第一个版本慢一点,没有更平滑的边框,但没关系。

结束问题。


I have an image and circle zone. I need to blur all, except for circle zone. Also i need to make border of circle smooth.
The input:

The output(made it in image redactor with mask, but i think opencv is using only bitmap masks):

For now i have code in python, which isn't blurring border of circle.

def blur_image(cv_image, radius, center, gaussian_core, sigma_x):
    blurred = cv.GaussianBlur(cv_image, gaussian_core, sigma_x)
    h, w, d = cv_image.shape
# masks
    circle_mask = np.ones((h, w), cv_image.dtype)
    cv.circle(circle_mask, center, radius, (0, 0, 0), -1)
    circle_not_mask = np.zeros((h, w), cv_image.dtype)
    cv.circle(circle_not_mask, center, radius, (2, 2, 2), -1)
# Computing
    blur_around = cv.bitwise_and(blurred, blurred, mask=circle_mask)
    image_in_circle = cv.bitwise_and(cv_image, cv_image, mask=circle_not_mask)
    res = cv.bitwise_or(blur_around, image_in_circle)
    return res

Current version:
How can i blur the border of circle? In example of output i've used gradient mask in program. Is there something similar in opencv?
UPDATE 04.03
So, i've tried formula from this answered topic and what i have:

Code:

def blend_with_mask_matrix(src1, src2, mask):
    res = src2 * (1 - cv.divide(mask, 255.0)) + src1 * cv.divide(mask, 255.0)
return res

This code should work similar as recent one, but it doesn't. The image in circle is slightly different. It has some problems with color. The question is still open.

解决方案

So the main problem with (mask/255) * blur + (1-mask/255)*another img was operators. They were working only with one channel. Next problem is working with float numbers for "smoothing".
I've changed code of blending with alpha channel to this:
1) i'm taking every channel for source images and mask
2) Performing formula
3) Merging channels

def blend_with_mask_matrix(src1, src2, mask):
    res_channels = []
    for c in range(0, src1.shape[2]):
        a = src1[:, :, c]
        b = src2[:, :, c]
        m = mask[:, :, c]
        res = cv.add(
            cv.multiply(b, cv.divide(np.full_like(m, 255) - m, 255.0, dtype=cv.CV_32F), dtype=cv.CV_32F),
            cv.multiply(a, cv.divide(m, 255.0, dtype=cv.CV_32F), dtype=cv.CV_32F),
           dtype=cv.CV_8U)
        res_channels += [res]
    res = cv.merge(res_channels)
    return res

And as a gradient mask i'm just using blurred circle.

def blur_image(cv_image, radius, center, gaussian_core, sigma_x):
    blurred = cv.GaussianBlur(cv_image, gaussian_core, sigma_x)

    circle_not_mask = np.zeros_like(cv_image)
    cv.circle(circle_not_mask, center, radius, (255, 255, 255), -1)
#Smoothing borders
    cv.GaussianBlur(circle_not_mask, (101, 101), 111, dst=circle_not_mask)
# Computing
    res = blend_with_mask_matrix(cv_image, blurred, circle_not_mask)
    return res

Result:

It is working a bit slower than very first version without smoother borders, but it's ok.
Closing question.

这篇关于在opencv python中混合的渐变蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆