opencv 去除玻璃蒙版,在opencv python中混合的渐变蒙版

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:

WD4Os.jpg

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

mArpL.png

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:

XlO0v.png

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:

SFQVm.png

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:

FXEJA.png

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

Closing question.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值